# -*- coding: utf-8 -*-
# @Time : 2019/1/4 20:49
# @Author : Endless-cloud
# @Site :
# @File : day 12 课后作业.py
# @Software: PyCharm
'''
# 3、编写装饰器,在每次执行被装饰函数之前打印一句’
每次执行被装饰函数之前都得先经过这里,这里根据需求添加代码’
在每次执行被装饰函数之前让用户输入用户名,密码,给用户三次机会,
登录成功之后,才能访问该函数.
'''
# def login(f):
# falg =True
# def inner(*args,**kwargs):
# nonlocal falg
# count = 3
# while falg:
#
# name =input('name>>>>>')
# password =input('password')
# if name =='wzh' and password =='123':
# f(*args,**kwargs)
# break
# else:
# count = count - 1
# print('账号密码错误{}'.format(count))
#
# if count==0:
# falg=False
# return inner
#
#
# @login
# def fun1(ss):
# print('登录成功{}'.format(ss))
# fun1('asdasdasda')
'''
# 4、编写装饰器,为多个函数加上认证的功能(用户的账号密码来源于文件,只支持单用户的账号密码,给用户三次机会),
要求登录成功一次,后续的函数都无需再输入用户名和密码
'''
# falg = False
#
#
# def login(f):
# def inner(*args,**kwargs):
# global falg
# if falg:
# ret =f(*args,**kwargs)
# # return ret
# else:
# username =input('请输入名字')
# password =input('请输入密码')
# if username=='wzh' and password =='123':
#
# falg=True
# ret =f(*args,**kwargs)
# # return ret
#
#
# return inner
#
#
# @login
# def fun1(ss):
# return 666
# @login
# def fun2(aa):
# print('de{}'.format(aa))
# @login
# def fun3(cc):
# print('asd{}'.format(cc))
# print(fun1('asdasdasda'))
# fun2('213')
# fun3('aswww')
'''
# 5.写一个函数完成三次登陆功能:
# 用户的用户名密码从一个文件register中取出。
# register文件包含多个用户名,密码,用户名密码通过|隔开,每个人的用户名密码占用文件中一行。
# 完成三次验证,三次验证不成功则登录失败,登录失败返回False。
# 登陆成功返回True。
'''
flag =False
def login(f):
def inner(*args,**kwargs):
global flag
count = 1
while count<=3:
if flag:
ret =f(*args,**kwargs)
return ret
else:
dic={}
with open('mima', 'r', encoding='utf-8') as f1:
for i in f1:
l=i.strip().split('|')
# list.append(i.strip().split('|'))
dic[l[0]]=l[1]
inusername =input('请输入账号')
inpassword =input('请输入密码')
# {'wzh': '123', 'haha': '123', 'chihuo': '123'}
try:
if inpassword == dic[inusername]:
flag =True
ret=f(*args,**kwargs)
return ret
else:
print('你还有{}次'.format(count))
except:
print('你还有{}次'.format(count))
count =count+1
return inner
@login
def fun(gg):
return gg+'登陆陈工'
@login
def fun1(ll):
return ll+'登陆陈工1'
@login
def fun2(ha):
return ha+'登陆陈工2'
print(fun('asdasdasd'))
print(fun1('asdasdasd'))
print(fun2('asdasdasd'))
'''
# 6.再写一个函数完成注册功能:
# (1)用户输入用户名密码注册。
# (2)注册时要验证(文件regsiter中)用户名是否存在,如果存在则让其重新输入用户名,如果不存在,则注册成功。
# (3)注册成功后,将注册成功的用户名,密码写入regsiter文件,并以 | 隔开。
# (4)注册成功后,返回True,否则返回False。
'''
# def zhuce():
# dic ={}
# with open('mima','r',encoding='utf-8') as f:
# for line in f:
#
# s =line.strip().split('|')
# dic[s[0]]=s[1]
# while 1:
# username =input('请输入用户名')
# password = input('请输入密码')
# if username in dic.keys():
# print('你输入的账号已经重复请重新输入')
# return False
# else:
# dic[username]=password
#
# print('你注册成功')
# lis3 ='\n'+username+'|'+str(password)
# # for n,j in dic.items():
# # # lis4 =[]
# # lis3.append('|'.join([n,j]))
#
#
# with open('mima','a',encoding='utf-8') as f2:
# f2.seek(0,2)
# f2.writelines(str(lis3))
# print('写入成功')
# return True
#
# zhuce()
'''
# 7. 用完成一个员工信息表的增删功能(升级题)。
# 文件存储格式如下:
id,name,age,phone,job
1,Alex,22,13651054608,IT
2,太白,23,13304320533,Tearcher
3,nezha,25,1333235322,IT
# 现在要让你实现两个功能:
# 第一个功能是实现给文件增加数据,用户通过输入姓名,年龄,电话,工作,给原文件增加数据
(增加的数据默认追加到原数据最后一行的下一行),但id要实现自增
(id自增有些难度,id是不需要用户输入的但是必须按照顺序增加)。
# 第二个功能是实现给原文件删除数据,用户只需输入id,则将原文件对应的这一条数据删除
(删除后下面的id不变,比如此时你输入1,则将第一条数据删除,但是下面所有数据的id值不变及太白,nezha的 id不变)。
'''
# def funxieru2():
#
# with open('xinxi','r',encoding='utf-8') as f1:
# titie =f1.readline()
# dic ={}
# for i in f1: #['1', 'Alex', '22', '13651054608', 'IT']
# l =i.strip().split(',')
# # dic1={}
# dic[l[0]]=l[1:]
#
# line_num =len(dic)
# # print(line_num)
#
#
# name =input('请输入名字')
# age = input('请输入年龄')
# id =input('请输入id')
# zhiye =input('请输入职业')
# key_num =str(line_num+1)
# dic[key_num]=[name,age,id,zhiye]
# print(dic)
# with open('xinxi','w',encoding='utf-8') as f3:
# f3.write(titie.strip())
# for i,j in dic.items():
#
# ln =','.join(list(i)+j)
# ln ='\n'+ln
# f3.write(ln)
# print('写入成功')
# # print(dic)
#
#
# funxieru2()
def fundel():
with open('xinxi','r',encoding='utf-8') as f4:
titie =f4.readline().strip()
dic1 ={}
for line in f4:
l =line.strip().split(',')
dic1[l[0]] =l[1:] #['1', 'Alex', '22', '13651054608', 'IT']
# print(dic1) #'1': ['Alex', '22', '13651054608', 'IT']
del_id =input('请输入删除的id')
dic1.pop(del_id)
# print(dic1)
with open('xinxi','w',encoding='utf-8') as f3:
f3.write(titie.strip())
for i,j in dic1.items():
ln =','.join(list(i)+j)
ln ='\n'+ln
f3.write(ln)
print('写入成功')
fundel()
# flag=False
# count = 3
# def login(func):
# def inner(*args,**kwargs):
# global flag,count
# if flag:
# print('每次执行被装饰函数之前都得先经过这里。')
# func_ret=func(*args,**kwargs)
# return func_ret,True
# else:
# while count>0:
# username=input('请输入账户名:').strip()
# password=input('请输入密码:').strip()
# with open('mima',encoding='utf-8') as f1:
# list_file=f1.readlines()
# for i in range(len(list_file)):
# if username == list_file[i].strip().split('|')[0] and password == list_file[i].strip().split('|')[1]:
# flag=True
# print('登录成功!')
# print('每次执行被装饰函数之前都得先经过这里。')
# func_ret=func(*args,**kwargs)
# return func_ret,True
# count -= 1
# print('您输入的账户名或密码错误,请重新输入,剩余%s机会' % count)
# if count == 0:
# return False
# return inner
#
# @login
# def func1(para1):
# print('in func1')
# return para1
# func1('test para1')
# @login
# def func2(para2):
# print('in func2')
# return para2
# func2('test para2')