python员工信息管理_python员工信息表管理系统

importos

BASR_DIR= os.path.dirname(os.path.abspath(__file__)) #获取绝对路径#数据库路径

__staff_db = BASR_DIR + r‘/staff_db‘

__user_db = BASR_DIR +r‘/user_db‘success_entyry= False #登录状态

n =3 #登录机会次数

def file_operation(file, mode, *args):‘‘‘格式化员工数据库格式,整理成一个字典,便于查询、修改‘‘‘dic={}if mode == ‘r‘:

with open(file, mode,encoding=‘utf-8‘) as f:

k= f.readline().strip().split(‘,‘)

staffs_list=f.readlines()for i instaffs_list:

l= i.strip().split(‘,‘)

dic[l[0]]=dict(zip(k,l))returndicif mode == ‘w‘:

staff_str= ‘id,name,age,phone,job\n‘

for i inargs[0]:for j inargs[0][i].values():

staff_str+= str(j) + ‘,‘staff_str= staff_str[:-1] + ‘\n‘staff_str= staff_str[:-1]

with open(file, mode,encoding=‘utf-8‘) as f:

f.write(staff_str)if mode == ‘init‘:

with open(file,‘w‘)as f:

f.write(args[0])pass

definit_database():‘‘‘‘初始化数据库‘‘‘

if not os.path.exists(__staff_db):

init_str= "id,name,age,phone,job\n1,Alex,22,13651054608,IT\n2,Egon,23,13304320533,Tearcher\n3,nezha,25,1333235322,IT"file_operation(__staff_db, ‘init‘, init_str)if not os.path.exists(__user_db):

with open(__user_db,‘a‘):pass

deflogin():‘‘‘注册,带密码重复验证‘‘‘user={}

f=open(__user_db,‘a‘)

name=input("请输入您的用户名")whileTrue:

password1=input("请输入您的密码")

password2=input("请再次输入您的密码")if password1!=password2:print("密码不一致,请重新修改")else:print("注册成功!")breakuser[name]=password2

f.write(str(user))

f.write(‘\n‘)

f.close()defentry():‘‘‘登录,带次数限制‘‘‘

globalnprint(‘*******登陆********‘)

with open(__user_db,‘r‘) as f :

name= input("请输入您的用户名")

password= input("请输入您的密码")

user={}

user[name]=password

flag=0for line inf:if str(user)==line.strip():

flag=1

print("登陆成功")globalsuccess_entyry

success_entyry=Trueif flag==0:

n-=1

print("用户名或者密码错误")print("您剩余登陆次数为%d"%(n))defmain_deractor(fun):‘‘‘主函数装饰器,增加登录注册功能‘‘‘

definner():while n>0 and notsuccess_entyry:print(‘********\033[1;35m欢迎来到管理员系统\033[0m!**********‘)

choice= input(‘输入1:登录\n输入2:注册\n输入3:退出\n‘).strip()if choice == ‘1‘:

entry()elif choice == ‘2‘:login()elif choice ==‘3‘:break

else:print("别瞎几把乱输!!")ifsuccess_entyry:fun()returninnerdefsplit_inp(inp_str, mode):‘‘‘分割输入命令,两个列表,一个是要查询的列,一个是条件

例如:select name, age where age>22

返回的 r = [name.age] comandlist = [age,>,22]‘‘‘l1= inp_str.split(‘where‘)if mode == ‘select‘:

r= l1[0].replace(mode, ‘‘).replace(‘ ‘, ‘‘).split(‘,‘) #返回的列名

if mode ==‘set‘:

r= l1[0].replace(mode, ‘‘).replace(‘ ‘, ‘‘).split(‘=‘)

comand_str= l1[1].replace(‘ ‘, ‘‘)

comand_list=[]if ‘like‘ incomand_str:

index= comand_str.index(‘like‘)

comand_list.append(comand_str[:index])

comand_list.append(comand_str[index:index+ 4])

comand_list.append(comand_str[index+ 4:])elif ‘>=‘ in comand_str or ‘<=‘ incomand_str:

index= comand_str.index(‘=‘)

comand_list.append(comand_str[:index- 1])

comand_list.append(comand_str[index- 1:index + 1])

comand_list.append(comand_str[index+ 1:])else:for i in [‘>‘, ‘

index=comand_str.index(i)

comand_list.append(comand_str[:index])

comand_list.append(comand_str[index])

comand_list.append(comand_str[index+ 1:])returnr, comand_listdefmsg_display(r, id, staff_list):‘‘‘查询信息时,打印对应信息‘‘‘

if r == [‘*‘]:print(str(staff_list[id]).strip(‘{}‘))else:print(str(staff_list[id]).strip(‘{}‘))defnew_del(comand_str):‘‘‘删除‘‘‘l= comand_str.replace(‘ ‘,‘‘).split(‘id=‘)

staff_dic= file_operation(__staff_db, ‘r‘)del staff_dic[l[1]]

file_operation(__staff_db,‘w‘,staff_dic)print("\033[1;32m删除成功!\033[0m")defnew_append(comand_str):‘‘‘增加员工信息‘‘‘staff_dic= file_operation(__staff_db, ‘r‘)

new_id= str(max([int(i) for i in staff_dic.keys()]) + 1)

l= comand_str.replace(‘ ‘, ‘‘).split(‘values‘)

new_staff= l[1].strip(‘()‘).split(‘,‘)if len(new_staff) != 4:print("您输入的格式有误,请重新输入!")returnnew_staff.insert(0,new_id)

new_staff_dic= dict(zip([‘id‘,‘name‘,‘age‘,‘phone‘,‘job‘],new_staff))

staff_dic[new_id]=new_staff_dic

file_operation(__staff_db,‘w‘,staff_dic)print("\033[1;32m增加成功!\033[0m")defset_select(comand_str,mode):‘‘‘‘查询或者更改员工信息‘‘‘staff_dic= file_operation(__staff_db, ‘r‘)

r, comand_list= split_inp(comand_str, ‘select‘) if mode == ‘select‘ else split_inp(comand_str, ‘set‘)if ‘like‘ incomand_str:for i instaff_dic:if comand_list[2] instaff_dic[i][comand_list[0]]:if mode == ‘select‘: msg_display(r, i, staff_dic)if mode == ‘set‘:

staff_dic[i][r[0]]= r[1]

file_operation(__staff_db, ‘w‘, staff_dic)print("\033[1;32m修改成功\033[0m")elif ‘>=‘ in comand_str or ‘<=‘ incomand_str:for i instaff_dic:if eval(staff_dic[i][comand_list[0]] + comand_list[1] + comand_list[2]):if mode == ‘select‘: msg_display(r, i, staff_dic)if mode == ‘set‘:

staff_dic[i][r[0]]= r[1]

file_operation(__staff_db, ‘w‘, staff_dic)print("\033[1;32m修改成功\033[0m")else:if comand_list[2].isdigit():for i instaff_dic:if eval(staff_dic[i][comand_list[0]] + comand_list[1].replace(‘=‘, ‘==‘) + comand_list[2]):if mode == ‘select‘: msg_display(r, i, staff_dic)if mode == ‘set‘:

staff_dic[i][r[0]]= r[1]

file_operation(__staff_db, ‘w‘, staff_dic)print("\033[1;32m修改成功\033[0m")else:for i instaff_dic:if staff_dic[i][comand_list[0]] == comand_list[2]:if mode == ‘select‘: msg_display(r, i, staff_dic)if mode == ‘set‘:

staff_dic[i][r[0]]= r[1]

file_operation(__staff_db, ‘w‘, staff_dic)print("\033[1;32m修改成功\033[0m")defdeal_comand(comand_str,mode):‘‘‘将增删改查整合在一起‘‘‘

if mode == ‘del‘:

new_del(comand_str)elif mode == ‘append‘:

new_append(comand_str)else:

set_select(comand_str,mode)defhelp():print(‘***********\033[1;35m指令格式参考如下:\033[0m*************‘)print(‘1:查找员工:\033[1;32m select name, age where age>22\033[0m‘)print(‘2:添加员工:\033[1;32m append values(nezha,25,1333235322,IT) (注:无需输入ID)\033[0m‘)print(‘3:修改员工:\033[1;32m set age = 18 where name =alex \033[0m‘)print(‘4:删除员工:\033[1;32m del id = 1 \033[0m‘)print(‘5:退出:\033[1;32m q\033[0m‘)

@main_deractordefmain():

n= 3

print(‘********\033[1;35m恭喜成功进入管理员系统\033[0m!**********‘)while n>0:print(‘tip:输入\033[1;32m help\033[0m查看使用手册‘)try:

comand_str= input(">>>:").strip()if comand_str.upper() == ‘Q‘:break

elif comand_str == ‘help‘:

help()else:

mode=comand_str.split()[0]

deal_comand(comand_str, mode)exceptBaseException as f:print("您输入的命令有误!请重新输入!!")

help()

init_database()

main()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值