python's sixteenth day for me 员工信息表

时间:2022-06-05 20:45:45
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 = {
'':add,
'':pop,
'':update,
'':check,
'':loginout
}
while user_dic['login']:
print(menu)
option = input('请输入你的选项:').strip()
if option in choice_dict:
choice_dict[option]()
else:
print('请输入正确的选项') if __name__ == '__main__':
main()