python学生成績管理システムの実現

11156 ワード

学生成績管理システム、権限付き著者:wfバージョン:最終版面オブジェクト、ログ付き、異常処理付き、パスワード付き暗号化初期データベース100個のデータを最初に実行するとデータベースとログファイルが生成され、後で追加したデータは自動的に削除され、データベースが再実行されます.データベースが初期化されます
管理者(最大権限を持つ)アカウント:adminパスワード:adminアカウント:1パスワード:1一般ユーザーアカウント:zhangsanパスワード:123456
#        ,   
#  :wf   :  
#    ,   ,      ,     
import logging
import hashlib
import shelve
from random import randint
#     
class student():
    def __init__(self,id,yw,sx,en):
        self.id=id
        self.yw=yw
        self.sx=sx
        self.en=en
    def __str__(self):
        return f"  :{self.id}
:{self.total()}
" def total(self): return self.yw+self.sx+self.en def pm(self,alist): return len(list(filter(lambda i:i.total()>self.total(),alist)))+1 # user='' datas=[] accouts={"admin":{"pwd":"21232f297a57a5a743894a0e4a801fc3","role":"1234567890"},"zhangsan":{"pwd":"e10adc3949ba59abbe56e057f20f883e","role":"12360"},"1":{"pwd":"c4ca4238a0b923820dcc509a6f75849b","role":"1234567890"}} funcdic={"1":" ", "2":" ", "3":" ", "4":" ", "5":" ", "6":" ", "7":" ", "8":" ", "9":" ", "0":" !"} subjectdic={" ":0," ":1," ":2} logging.basicConfig(level= logging.DEBUG, filename = "xxgl.log", format = "%(asctime)s %(filename)s : %(levelname)s %(message)s", datefmt = "%Y-%m-%d %H:%M:%S", filemode = "a") for i in range(100): datas.append(student(f"15{i+1:03d}",randint(30,100),randint(40,100),randint(50,100))) # def init(): global datas,accouts with shelve.open("mydb") as f: try: datas=f["data"] accouts=f["user"] logging.info(" !") except: f["data"]=datas f["user"]=accouts logging.warning(" !") # def login(): global user uname=input(" :") passwd=input(" :") password=hashlib.md5(passwd.encode()).hexdigest() if uname in accouts: if password==accouts[uname]["pwd"]: logging.info(f" {uname} !") user=uname return print(" !") logging.warning(f" {uname} !") # def save(): with shelve.open("mydb") as f: f["data"]=datas f["user"]=accouts # def printdata(f,title): table=sorted(datas,key=f) print(f"{title:-^70s}") print(f'{" ":^10s}{" ":^10s}{" ":^10s}{" ":^10s}{" ":^10s}{" ":^10s}') print("-"*75) for i in table: print(f'{i.id:^12s}{i.yw:^12d}{i.sx:^12d}{i.en:^12d}{i.total():^12d}{i.pm(datas):^12d}') print("-"*75) logging.info(f"{user} {title}!") # def add1(): print(" ::".center(65,'-')) global datas b=[i.id for i in datas] while 1: id=input(" :").strip() if id not in b: break print(' , !') yw=int(input(" :").strip()) sx=int(input(" :").strip()) en=int(input(" :").strip()) datas.append(student(id,yw,sx,en)) save() logging.info(f"{user} , {id}。") # def datafx(): yw=[i.yw for i in datas] sx=[i.sx for i in datas] en=[i.en for i in datas] print(" ".center(50,"-")) print(f" :{sum(yw)/len(yw):40.2f}") print(f" :{100*sum([i>=60 for i in yw])/len(yw):40.2f}%") print(f" :{100*sum([i>=80 for i in yw])/len(yw):40.2f}%
") print(" ".center(50,"-")) print(f" :{sum(sx)/len(sx):40.2f}") print(f" :{100*sum([i>=60 for i in sx])/len(sx):40.2f}%") print(f" :{100*sum([i>=80 for i in sx])/len(sx):40.2f}%
") print(" ".center(50,"-")) print(f" :{sum(en)/len(en):40.2f}") print(f" :{100*sum([i>=60 for i in en])/len(en):40.2f}%") print(f" :{100*sum([i>=80 for i in en])/len(en):40.2f}%
") logging.info(f"{user} 。") # def query(): print(" :".center(65,'-')) b=[i.id for i in datas] while 1: id=input(" :") if id in b: break print(' , !') a=list(filter(lambda i:i.id==id,datas))[0] print(f" :{a.yw}") print(f" :{a.sx}") print(f" :{a.en}
") logging.info(f"{user} {id} 。") # def databk(): ywb=[] sxb=[] enb=[] for k in datas: if k.yw<60: ywb.append([k.id,k.yw]) if k.sx<60: sxb.append([k.id,k.sx]) if k.en<60: enb.append([k.id,k.en]) print(" :".center(65,'-')) print(f'{" ":^8s}{" ":^18s}') for i in ywb: print(f"{i[0]:^10s} {i[1]:^20d}
") print(" :".center(65,'-')) print(f'{" ":^8s}{" ":^18s}') for i in sxb: print(f"{i[0]:^10s} {i[1]:^20d}
") print(" :".center(65,'-')) print(f'{" ":^8s}{" ":^18s}') for i in enb: print(f"{i[0]:^10s} {i[1]:^20d}
") logging.info(f"{user} 。") # def update1(): print(" :".center(65,'-')) b=[i.id for i in datas] while 1: id=input(" :") if id in b: break print(' , !') a=list(filter(lambda i:i.id==id,datas))[0] s1=int(input(" :")) s2=int(input(" :")) s3=int(input(" :")) a.yw=s1 a.sx=s2 a.en=s3 save() print(' ') logging.info(f"{user} {id} 。") # def delete(): print(" :".center(65,'-')) global datas b=[i.id for i in datas] while 1: id=input(" :") if id in b: break print(' , !') a=list(filter(lambda i:i.id!=id,datas)) datas=a save() logging.info(f"{user} {id}。") # def manage(): global accouts print(" :") while 1: while 1: print(" ".center(70,'-')) op=input("1 2 3 4 5
").strip() if op in list("12345"): break print(" !") if op=="1": # print(f"{' ':-^60s}") uname=input(" :").strip() pd=input(" :").strip() pd=hashlib.md5(pd.encode()).hexdigest() ro=input(" :").strip() if uname in accouts: print(" !") else: accouts[uname]={"pwd":pd,"role":ro} logging.warning(f"{user} {uname}!") save() elif op=="2": # print(f"{' ':-^60s}") uname=input(" :").strip() if uname in accouts: del accouts[uname] save() logging.warning(f"{user} {uname}!") else: print(" !") elif op=="3": # print(f"{' ':-^60s}") while 1: uname=input(" ").strip() if uname not in accouts: print(' , ') else: break pwd1=input(" :") pwd2=input(" :") if pwd1==pwd2: pd=hashlib.md5(pwd1.encode()).hexdigest() accouts[uname]["pwd"]=pd save() logging.warning(f"{user} {uname} !") else: print(" !") quanxian=input(" :") accouts[uname]['role']=quanxian save() logging.warning(f"{user} {uname} !") elif op=="4": # print(f"{' ':-^60s}") print(f"{' ':^10s}{' ':^35s}{' ':^10s}") for k,v in accouts.items(): print(f"{k:^13s}{v['pwd']:^37s}{v['role']:^12s}") print() logging.warning(f"{user} !") else: break # def main(): init() login() if user in accouts: role=accouts[user]["role"] print(" ".center(60,"-")) print() print(" :wf :
".center(65)) while 1: print(" :") for i in role: print(f"{i:<5s}{funcdic[i]}") # while 1: select=input(" :").strip() if select in role: break print(" , !") if select=="1": #"1":" " printdata(lambda i:i.id," ") elif select=="2": # "2":" " printdata(lambda i:i.pm(datas)," ") elif select=="3": #"3":" " datafx() elif select=="4": #"4":" ", databk() elif select=="5": #"5":" ", add1() elif select=="6": #"6":" ", query() elif select=="7": #"7":" ", update1() elif select=="8": #"8":" ", delete() elif select=="9": #"9":" ", manage() else: logging.info(f"{user} 。") exit() if __name__=="__main__": main()