1. 程式人生 > >python自動化-作業4.用戶管理程序——A+

python自動化-作業4.用戶管理程序——A+

name 狀態 not append 要求 huawei hone dex 退出

作業內容:

  用戶管理程序:

    普通用戶:登錄、註冊、修改密碼、查看本用戶信息

    管理員用戶:登錄、註冊、修改密碼、查看本用戶信息,

          刪除、添加普通用戶

          修改普通用戶密碼

          查看所有普通用戶,按照指定關鍵字搜索用戶信息,

          提高普通用戶權限

   要求:

    1.用戶信息存文件

    2.權限用裝飾器

  1.0版完成時間:2108/12/8

FLOG = {is_login:False,                           # 是否註冊標誌
        is_register
:False, # 是否登錄標誌 user_info:[], # 登錄用戶的性息 line_count:0, # 所要修改的內容在文件user_data中的行數 } temp_file = [] def login(u,p): # 登錄 with open(user_data,r+,encoding=
utf-8) as f: count = 0 for i in f: count += 1 username = i.split(|)[0] password = i.split(|)[1] email = i.split(|)[2] phone = i.split(|)[3] conservator = i.split(|)[4] if u == username and p == password: FLOG[
is_login] = True FLOG[user_info] = [username, password, email, phone, conservator] FLOG[line_count] = count if not FLOG.get(is_login): print(賬號或密碼錯誤,請重新登錄.center(50,-)) else: print(恭喜【{}】登錄成功!.format(u)) def register(u,p): # 註冊 with open(user_data,r+,encoding=utf-8) as f: for i in f: username = i.split(|)[0] if u == username: # 判斷是否有用戶名重復註冊 print(該賬號已被註冊.center(50,-)) FLOG[is_register] = True with open(user_data, a+, encoding=utf-8) as f: if not FLOG.get(is_register): in_email = input(請輸入您的郵箱:>>>).strip() in_phone = input(請輸入您的電話號碼:>>>).strip() in_conservator = input(是否為管理員(1.不是,2.是):>>>).strip() str_join = {}|{}|{}|{}|{}|\n.format(u,p,in_email,in_phone,in_conservator) f.write(str_join) print(註冊成功.center(50,-)) FLOG[is_register] = False def outer(fun): # 是否登錄裝飾器 def inner(*args,**kwargs): if FLOG.get(is_login): r = fun(*args,**kwargs) else: print(請先登錄.center(50,-)) while not FLOG[is_login]: in_username = input(請輸入你的用戶名:>>>).strip() in_password = input(請輸入您的密碼:>>>).strip() login(in_username,in_password) r = fun(*args,**kwargs) return r return inner @outer def modify_pwd(): # 修改密碼 in_password = input(請輸入您的新密碼:>>>).strip() FLOG[user_info][1] = in_password str_join = |.join(FLOG[user_info])+|\n temp_file = [] temp_flog = False with open(user_data,r+,encoding=utf-8) as f: count = 0 for i in f: count += 1 if FLOG.get(line_count) == count: temp_flog = True continue if temp_flog: temp_file.append(i) with open(user_data,r+,encoding=utf-8) as f: for i in range(FLOG.get(line_count)-1): f.readline() f.seek(f.tell()) f.truncate() f.write(str_join) # 在這一行中添加修改了密碼的用戶性息 for i in temp_file: f.write(i) print(修改成功!.center(50,-)) @outer def print_userinfo(): # 查看本用戶性息 print(登錄用戶性息.center(100,-)) print(用戶名 密碼 郵箱 電話 用戶權限) print({: <12}{: <12}{: <24}{: <18}{: <2}.format(FLOG[user_info][0],FLOG[user_info][1],FLOG[user_info][2],FLOG[user_info][3],FLOG[user_info][4])) def privilege_user(fun): # 是否為管理員裝飾器 def inner(*args,**kwargs): if FLOG[user_info][4] == 2: r = fun(*args,**kwargs) else: print(您的權限不夠,請以管理員權限登錄.center(50,-)) FLOG[is_login] = False # 退出登錄狀態 print(【{}】退出登錄!.format(FLOG[user_info][0])) FLOG[user_info] = [] while not FLOG[user_info][4] == 2: in_username = input(請輸入你的用戶名:>>>).strip() in_password = input(請輸入您的密碼:>>>).strip() login(in_username,in_password) r = fun(*args,**kwargs) return r return inner @outer @privilege_user def del_comuser(): # 刪除普通用戶 in_user = input(請輸入您要刪除的用戶名:>>>) temp_is_comuser = False with open(user_data,r+,encoding=utf-8) as f: # 判斷有沒有這個用戶,判斷是否為普通用戶 for i in f: username = i.split(|)[0] privilege = i.split(|)[4] if in_user == username and privilege == 1: temp_is_comuser = True temp_file = [] temp_flog = False if temp_is_comuser: # temp_flog = True 刪除輸入的普通用戶 with open(user_data,r+,encoding=utf-8) as f: count = 0 for i in f: count += 1 username = i.split(|)[0] if in_user == username: FLOG[line_count] = count temp_flog = True continue if temp_flog: temp_file.append(i) with open(user_data,r+,encoding=utf-8) as f: for i in range(FLOG.get(line_count)-1): f.readline() f.seek(f.tell()) f.truncate() # 清除光標之後的內容 for i in temp_file: f.write(i) print(刪除用戶成功.center(50,-)) else: print(用戶不存在/用戶權限為管理員,無法刪除.center(50,-)) @outer @privilege_user def add_user(): # 添加用戶 in_username = input(請輸入您要添加用戶的用戶名:>>>).strip() in_password = input(請輸入您要添加用戶的密碼:>>>).strip() in_email = input(請輸入您要添加用戶的郵箱:>>>).strip() in_phone = input(請輸入您要添加用戶的電話號碼:>>>).strip() in_conservator = input(添加的用戶是否為管理員(1.不是,2.是):>>>).strip() str_join = {}|{}|{}|{}|{}|\n.format(in_username, in_password, in_email, in_phone, in_conservator) tamp_flog = False with open(user_data, r+, encoding=utf-8) as f: for i in f: username = i.split(|)[0] password = i.split(|)[1] if username == in_username and password == in_password: print(用戶以存在.center(50,-)) tamp_flog = True if not tamp_flog: with open(user_data, a+, encoding=utf-8) as f: f.write(str_join) print(用戶添加成功.center(50,-)) @outer @privilege_user def alter_comuser_pwd(): # 修改普通用戶密碼 in_user = input(請輸入您要修改用戶的用戶名:>>>) temp_is_comuser = False with open(user_data, r+, encoding=utf-8) as f: # 判斷有沒有這個用戶,判斷是否為普通用戶 count = 0 for i in f: count += 1 username = i.split(|)[0] privilege = i.split(|)[4] if in_user == username and privilege == 1: temp_is_comuser = True FLOG[line_count] = count FLOG[user_info] = [i.split(|)[0],i.split(|)[1],i.split(|)[2],i.split(|)[3],i.split(|)[4]] temp_file = [] temp_flog = False if temp_is_comuser: in_password = input(請輸入您的新密碼:>>>).strip() FLOG[user_info][1] = in_password str_join = |.join(FLOG[user_info])+|\n with open(user_data,r+,encoding=utf-8) as f: count = 0 for i in f: count += 1 if FLOG.get(line_count) == count: temp_flog = True continue if temp_flog: temp_file.append(i) with open(user_data,r+,encoding=utf-8) as f: for i in range(FLOG.get(line_count)-1): f.readline() f.seek(f.tell()) f.truncate() f.write(str_join) # 在這一行中添加修改了密碼的用戶性息 for i in temp_file: f.write(i) print(修改成功.center(50,-)) else: print(用戶不存在/用戶權限為管理員,修改失敗.center(50,-)) @outer @privilege_user def print_all_comuser_info(): # 查看所用普通用戶 Tamp_FLOG_user_info = FLOG[user_info] print(所有普通用戶性息.center(100,-)) print(用戶名 密碼 郵箱 電話 用戶權限) with open(user_data,r+,encoding=utf-8) as f: for i in f: privilege = i.split(|)[4] if privilege == 1: FLOG[user_info] = [i.split(|)[0], i.split(|)[1], i.split(|)[2], i.split(|)[3], i.split(|)[4]] print({: <12}{: <12}{: <24}{: <18}{: <2}.format(FLOG[user_info][0], FLOG[user_info][1], FLOG[user_info][2], FLOG[user_info][3], FLOG[user_info][4])) FLOG[user_info] = Tamp_FLOG_user_info @outer @privilege_user def raise_authority(): # 提高普通用戶權利 in_user = input(請輸入普通用戶的用戶名:>>>) is_comuser = False temp_file = [] temp_flog = False with open(user_data,r,encoding=utf-8) as f: count = 0 for i in f: count += 1 username = i.split(|)[0] privilege = i.split(|)[4] if in_user == username and privilege == 1: # 確認有這個用戶這個用戶為普通用戶 is_comuser = True FLOG[user_info] = [i.split(|)[0], i.split(|)[1], i.split(|)[2], i.split(|)[3], i.split(|)[4]] FLOG[line_count] = count if is_comuser: FLOG[user_info][4] = 2 str_join = |.join(FLOG[user_info])+|\n with open(user_data,r+,encoding=utf-8) as f: count = 0 for i in f: count += 1 if FLOG.get(line_count) == count: temp_flog = True continue if temp_flog: temp_file.append(i) with open(user_data,r+,encoding=utf-8) as f: for i in range(FLOG.get(line_count)-1): f.readline() f.seek(f.tell()) f.truncate() f.write(str_join) # 在這一行中添加修改了密碼的用戶性息 for i in temp_file: f.write(i) print(修改成功.center(50,-)) else: print(沒有這個用戶.center(50,-)) if __name__ == __main__: while True: print(用戶管理程序.center(100,-)) print(1.登錄\n2.註冊\n3.修改密碼\n 4.查看本用戶性息\n 5.刪除普通用戶\n 6.添加用戶\n 7.修改普通用戶密碼\n 8.查看所有普通用戶\n 9.提高普通用戶權限) index = input(請輸入您的選擇(註意:5-9只使用於管理員):>>>) if int(index) in range(1,10): if index == 1: # 登錄 username = input(請輸入你的用戶名:>>>).strip() password = input(請輸入您的密碼:>>>).strip() login(username,password) if index == 2: # 註冊 username = input(請輸入你的用戶名:>>>).strip() password = input(請輸入您的密碼:>>>).strip() register(username,password) if index == 3: # 修改密碼 modify_pwd() if index == 4: # 查看本用戶性息 print_userinfo() if index == 5: # 刪除普通用戶 del_comuser() if index == 6: # 添加用戶 add_user() if index == 7: # 修改普通用戶密碼 alter_comuser_pwd() if index == 8: # 查看所用普通用戶 print_all_comuser_info() if index == 9: # 提高普通用戶權限 raise_authority() else: print(您的輸入有誤,請重新輸入.center(50,-))

user_data文件:

ggz|123|[email protected]|18670221892|2|
ggz1|123|[email protected]|18670221892|1|
g|123|[email protected]|18670221892|2|
huawei|123|[email protected]|18670221892|1|
zhongxin|123456|[email protected]|18670221892|1|
z|123|[email protected]|18670221892|1|
lianxiang|123|123@123|123|1|

調試文件:

with open(user_data,r+,encoding=utf-8) as f:
    f.write(guo|123|[email protected]|18670221892|1|\n)
    f.write(ggz|123|[email protected]|18670221892|2|\n)
    f.write(xiaomi|123|[email protected]|18670221892|1|\n)
    f.write(meizu|123|[email protected]|18670221892|1|\n)
    f.write(huawei|123|[email protected]|18670221892|1|\n)
    f.write(zhongxin|123|[email protected]|18670221892|1|\n)

python自動化-作業4.用戶管理程序——A+