基于面向对象的用户登录接口

要求一:

自定义用户信息数据结构,写入文件,然后读出内容,利用eval重新获取数据结构
with open('user.db','w') as write_file:
    write_file.write(str({
        "egon":{"password":"123",'status':False,'timeout':0},
        "alex":{"password":"456",'status':False,'timeout':0},
        }))

with open('user.db','r') as read_file:
    data=read_file.read()
    d=eval(data)
    print(d['egon']['password'])
    print(d['egon']['status'])
    print(d['egon']['timeout'])

要求二:

定义用户类,定义属性db,执行obj.db可以拿到用户数据结构
class User:
    db_path='user.db'
    def __init__(self,username):
        self.username=username
    @property
    def db(self):
        data=open(self.db_path,'r').read()
        return eval(data)

u=User('egon')
print(u.db['egon'])
print(u.db['egon']['password'])

要求三:分析下述代码的执行流程

class User:
    db_path='user.db'
    def __init__(self,name):
        self.name=name
    @property
    def db(self):
        with open(self.db_path,'r') as read_file:
            info=read_file.read()
            return eval(info)
    @db.setter
    def db(self,value):
        with open(self.db_path,'w') as write_file:
            write_file.write(str(value))
            write_file.flush()

    def login(self):
        data=self.db
        if data[self.name]['status']:
            print('已经登录')
            return True
        if data[self.name]['timeout'] < time.time():
            count=0
            while count < 3:
                passwd=input('password>>: ')
                if not passwd:continue
                if passwd == data[self.name]['password']:
                    data[self.name]['status']=True
                    data[self.name]['timeout']=0
                    self.db=data
                    break
                count+=1
            else:
                data[self.name]['timeout']=time.time()+10
                self.db=data
        else:
            print('账号已经锁定10秒')

u1=User('egon')
u1.login()

u2=User('alex')
u2.login()

要求四:

根据上述原理,编写退出登录方法(退出前要判断是否是登录状态),自定义property,供用户查看自己账号的锁定时间
import time

class User:
	db_path='user.db'
	def __init__(self,name):
		self.name=name
	@property
	def db(self):
		with open(self.db_path,'r') as read_file:
			info=read_file.read()
			return eval(info)
	@db.setter
	def db(self,value):
		with open(self.db_path,'w') as write_file:
			write_file.write(str(value))
			write_file.flush()
	@property
	def locktime(self):
		data = self.db[self.name]
		if data['timeout'] == 0:
			return "this account has not locked"
		else:
			return "accout has been locked at " + time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(data['timeout']))
	def login(self):
		data=self.db
		if data[self.name]['status']:
			print('已经登录')
			return True
		if data[self.name]['timeout'] > time.time():
			print('账号已经锁定10秒')
			return False
		count = 0
		while count < 3:
			password = input("password:")
			if not password: continue
			if password == data[self.name]['password']:
				data[self.name]['status'] = True
				data[self.name]['timeout'] =0
				self.db = data
				return True
			else:
				print("error password")
			count += 1
		else:
			data[self.name]['timeout'] = time.time() + 10
			self.db = data
			print("your account has been locked!")
			return False
	def index(self):
		while True:
			command = input(">")
			if command == 'q':
				self.logout()
				break
			else:
				print("no command found %s" %command)
	def logout(self):
		data = self.db
		if data[self.name]['status'] == True:
			data[self.name]['status'] = False
			self.db = data
			print("your account has been logout!")

u1=User('egon')
if u1.login():
	u1.index()
print(u1.locktime)
原文地址:https://www.cnblogs.com/anyanyaaaa/p/6752091.html