1. 程式人生 > >python's sixth day for me 員工信息表

python's sixth day for me 員工信息表

索引 無需 spl class 系統 pwd div 獲取 args

import os
user_dic = {
    username:None,
    password:None,
    login:True
}
flag = False

name_list = [id,name,age,phone,job]

check_conditions = [>,<,=,like]
def auth(func):
    def wrapper(*args,**kwargs):
        with open(user-pwd,encoding=utf-8) as f:
            user 
= eval(f.read()) # print(user) count = 0 while count<3: username = input(請輸入用戶名:).strip() password = input(請輸入密碼:).strip() if user.get(username) and password == user[username]: user_dic[username] = username user_dic[
password] = password user_dic[login] = True ret = func(*args,**kwargs) return ret else: count += 1 if count == 3: print(由於多次輸入,您已經沒有輸入機會,請重新登陸...) else:
print(用戶名或密碼不正確,您還有%s次輸入機會,請重新輸入... % (2 - count)) return wrapper def get_last_id(): ‘‘‘ 此函數是獲取id表中的id :return: 返回值是最後的id,如果第一次給員工信息表添加信息,id為0 ‘‘‘ with open(id表,encoding=utf-8) as f: list_id = f.read() return int(list_id) if list_id else 0 def add(): ‘‘‘ 此函數是為員工信息表添加新的數據,關鍵點再添加新數據後id會隨著添加信息而改變 :return: None ‘‘‘ old_last_id = get_last_id() input_infor = input(請按照下列格式輸入信息: 姓名,年齡,電話,職業).strip() new_last_id = str(int(old_last_id)+1) add_infor = new_last_id + ,+ input_infor with open(員工信息表,encoding=utf-8,mode=a) as f2: #次三元運算是判斷員工信息表中如果是第一次添加內容則無需換行,否則需要換行。 f2.write(add_infor) if old_last_id == 0 else f2.write(\n+ add_infor) print(您已成功添加,請繼續操作) with open(id表,encoding=utf-8,mode=w) as f3: f3.write(new_last_id) def pop(): ‘‘‘ 此函數的功能是刪除員工信息表中的數據,關鍵點: flag的設置是因為我們在打開員工信息表的過程中不能刪除此員工信息表。 所以設置標誌位,等操作完成之後,再刪除原文件,重新命名新文件。 :return: ‘‘‘ pop_id = input(請輸入你想刪除的信息的id:) with open(員工信息表,encoding = utf-8) as f: for line in f: line_list = line.strip().split(,) if pop_id == line_list[0]: global flag flag = True with open(員工信息表,encoding=utf-8) as f1, open(員工信息表.bak,encoding=utf-8,mode=a) as f2: for line1 in f1: f2.write(line1) if pop_id != line1.strip().split(,)[0] else f2.write(‘‘) break else: print(沒有您輸入id,請重新輸入...) if flag: os.remove(員工信息表) os.rename(員工信息表.bak,員工信息表) def update(): ‘‘‘ 此函數的功能修改員工信息 語法:set 列名 = ’新的值‘ where 條件 先用where查找對應人的信息,再使用set來修改列名對應的值為‘新的值‘ 關鍵點: check_conditions,name_list 這兩個列表的運用 name_list 為員工信息表的列表 , ‘id‘, ‘name‘, ‘age‘, ‘phone‘, ‘job‘ 索引與員工信息表每行信息的每列一一對應。 思路: 1,先通過where 分割,得到content (如:set name = 顧清秋),cond (如:id<3) 2,循環check_conditions(>,<,=,like) 通過條件分割得到的是:name 是 id , key 是 >,cond 是3 3,對content進行操作,得到要修改的列名:column_name,要修改的值 uodate_value 4,文件改的思想,修改文件,需要對原文件進行讀取,再寫入新文件,然後將原文件刪除重命名新文件。 5,關鍵點:check_dict這是一個if 條件的字典,這樣設置是因為不管條件為<>=或like 它們只要滿足條件,下面進行的操作是一樣的,所以只是條件不同,所以可以根據 key的不同,執行check_list不痛的條件,然後進行相同的內容即可。 :return: ‘‘‘ pass def check(): ‘‘‘ 此函數是查詢功能: 支持三種語法: select 列名1,列名2,...where 列名條件 支持大於小於等於,也可模糊查詢 示例: check_infor = select name,age where age>22 check_infor = select * where job = IT check_infor = select * wherr phone like 133 關鍵點: check_conditions,name_list 這兩個列表的運用 :return: ‘‘‘ try: check_infor = input(請輸入查詢語句:).strip() content,condition = check_infor.split(where) # [‘select name, age‘,‘age>22‘] if select in content: for key in check_conditions: # [‘>‘, ‘<‘, ‘=‘, ‘like‘] if key in condition: index_check_conditions = check_conditions.index(key) name,cond = condition.strip().split(key) # age ,22 # content 為 ‘select name,id,或‘select * ‘‘ # name 是 where 後面的第一個關鍵字 # key 是比較運算符 # cond 是 實際需要比較的關鍵字 # 例如:where id > 3 name是id,key是> ,cond 是 3 content = content.split(select)[1] # ‘name,age‘ with open(員工信息表,encoding=utf-8) as f: for line in f: if line.strip(): # 保證每行不是空 line_list = [i.strip() for i in line.strip().replace(,,).split(,)] # 上面是為了替換掉中文的逗號, index= name_list.index(name.strip()) # index 查詢條件的索引沒比如你的查詢條件是 select * where id > 3 # name 就是 id 求出的 index 就是id 在 name_list 中的索引, # 也是id 在name_list 列表中的索引。 check_dict= { 0:line_list[index] > cond, 1:line_list[index] < cond, 2:line_list[index].strip() == cond.strip(), 3:cond.strip() in line_list[index].strip() } if check_dict[index_check_conditions]: if content.strip() == *: print(line.strip()) else: if , in content.strip(): select_name_list = content.strip().split(,) # [‘name‘,‘age‘] select_name_list = [i for i in select_name_list if i != ‘‘] str1 = ‘‘ for names in select_name_list: name_index = name_list.index(names.strip()) # 找出 在name_list中的索引, str1 = str1 + line_list[name_index] + , print(str1) else: print(line_list[name_list.index(content)]) break else: print(你輸入的不正確,請重新輸入...) else: print(您沒有輸入select語句或者輸入有誤,請重新輸入...) except Exception: print(你輸入的查詢語句有誤,請重新輸入...) def loginout(): print(感謝您登陸員工內部系統) user_dic[login] = False @auth def main(): menu = """ 歡迎%s用戶登陸內部員工系統,請進行選擇: 選項 1,增加員工信息 選項 2,刪除員工信息 選項 3,更改員工信息 選項 4,查詢員工信息 選項 5,退出內部員工系統 """ % (user_dic[username]) choice_dict = { 1:add, 2:pop, 3:update, 4:check, 5:loginout } while user_dic[login]: print(menu) option = input(請輸入你的選項:).strip() if option in choice_dict: choice_dict[option]() else: print(請輸入正確的選項) if __name__ == __main__: main()

python's sixth day for me 員工信息表