要求一:自定义用户信息数据结构,写入文件,然后读出内容,利用eval重新获取数据结构
3 with open('user.db','w') as write_file:#创建并以写入的方式打开一个文件user.db
4 write_file.write(str({
5 "egon":{"password":"123",'status':False,'timeout':0},
6 "alex":{"password":"456",'status':False,'timeout':0},
7 }))#在user.db中加入两个用户信息以字典的方式储存
8
9 with open('user.db','r') as read_file:#以只读的方式打开一个文件user.db
10 data=read_file.read()#读取user.db中的数据
11 d=eval(data)#将user.db中的数据转为字典
12 print(d['egon']['password'])#打印字典中egon的password 对应value
13 print(d['egon']['status'])
14 print(d['egon']['timeout'])
求二:定义用户类,定义属性db,执行obj.db可以拿到用户数据结构
3 class User: #定义User类
4 db_path='user.db'
5 def __init__(self,username): #在实例化User类时,传入Username参数的值
6 self.username=username
7 @property#将db()方法作为属性,让用户调用
8 def db(self):
9 data=open(self.db_path,'r').read()#以只读的方式打开文件user.db
10 return eval(data)#以字典的方式返回user.db中的内容
11 u=User('egon')#实例化对象u,传入egon
12 print(u.db['egon'])#打印又u.db()返回的字典中,对应egon的value
13 print(u.db['egon']['password'])#打印又u.db()返回的字典中,对应egon的password,value
求三:分析下述代码的执行流程
3 import time
4 class User:#定义User类
5 db_path='user.db'
6 def __init__(self,name): #在实例化User类时,传入name参数的值
7 self.name=name
8 @property#将db()方法作为属性,让用户调用,同时产生db.setter和db.del方法
9 def db(self):
10 with open(self.db_path,'r') as read_file:#当调用db方法时,打开文件user.db
11 info=read_file.read()
12 return eval(info)#以字典的方式返回user.db中的用户信息
13 @db.setter#在对db属性进行修改操作的时候,调用此方法
14 def db(self,value):
15 with open(self.db_path,'w') as write_file:#创建并打开一个文件user.db
16 write_file.write(str(value))#在文件中写入db属性得到的值
17 write_file.flush()#刷新文件的缓冲区域,让数据立刻写入文件
18 def login(self): #定义login方法
19 data=self.db#data得到db方法(现在被@property修饰过的属性)返回的user.db中的数据
20 if data[self.name]['status']:#判断name的status字段是否为Ture
21 print('已经登录')
22 return True
23 if data[self.name]['timeout'] < time.time(): #判断name的timeout字段值是否小于....呃~1970年到现在的时间
24 count=0
25 while count < 3:
26 passwd=input('password>>: ')#输入密码
27 if not passwd:continue#如果密码为空,那么重新循环到输入密码
28 if passwd == data[self.name]['password']: #输入密码正确
29 data[self.name]['status']=True#更改用户的登陆状态
30 data[self.name]['timeout']=0#超时字段归0
31 self.db=data#将改写过的值重新调用db.setter方法存入user.db文件中,在用户看来就是对db属性进行了重新的赋值操作
32 break
33 count+=1#只允许用户输入三次错误的机会
34 else:
35 data[self.name]['timeout']=time.time()+10#如果三次输入错误,那么该用户将被锁定10秒
36 self.db=data#将改写过的值重新调用db.setter方法存入user.db文件中,在用户看来就是对db属性进行了重新的赋值操作
37 else:#如果判断用户的timeout大于1970年到现在的值
38 print('账号已经锁定10秒')
39
40 u1=User('egon') #实例化u1传入name,egon
41 print("egon login:")
42 u1.login()#u1调用login的方法
四:根据上述原理,编写退出登录方法(退出前要判断是否是登录状态),自定义property,供用户查看自己账号的锁定时间
3 import time
4 class User:#定义User类
5 db_path='user.db'
6 def __init__(self,name): #在实例化User类时,传入name参数的值
7 self.name=name
8 print("%s Login:"%self.name)
9 @property#将db()方法作为属性,让用户调用,同时产生db.setter和db.del方法
10 def db(self):
11 with open(self.db_path,'r') as read_file:#当调用db方法时,打开文件user.db
12 info=read_file.read()
13 return eval(info)#以字典的方式返回user.db中的用户信息
14 @db.setter#在对db属性进行修改操作的时候,调用此方法
15 def db(self,value):
16 with open(self.db_path,'w') as write_file:#创建并打开一个文件user.db
17 write_file.write(str(value))#在文件中写入db属性得到的值
18 write_file.flush()#刷新文件的缓冲区域,让数据立刻写入文件
19 @property
20 def UserLockTime(self):
21 return int(self.db[self.name]["timeout"]-time.time())
22 def loginOut(self):
23 data = self.db
24 if(data[self.name]['status']):
25 print(self.name, "正在登出.....")
26 data[self.name]['status'] = False
27 self.db = data
28 time.sleep(2)
29 print(self.name,"登出成功!")
30 else:
31 print(self.name,"并没有登陆")
32 def login(self): #定义login方法
33 data=self.db#data得到db方法(现在被@property修饰过的属性)返回的user.db中的数据
34 if data[self.name]['status']:#判断name的status字段是否为Ture
35 print('已经登录')
36 return True
37 if data[self.name]['timeout'] < time.time(): #判断name的timeout字段值是否小于....呃~1970年到现在的时间
38 count=0
39 while count < 3:
40 passwd=input('password>>: ')#输入密码
41 if not passwd:continue#如果密码为空,那么重新循环到输入密码
42 if passwd == data[self.name]['password']: #输入密码正确
43 data[self.name]['status']=True#更改用户的登陆状态
44 data[self.name]['timeout']=0#超时字段归0
45 self.db=data#将改写过的值重新调用db.setter方法存入user.db文件中,在用户看来就是对db属性进行了重新的赋值操作
46 print("欢迎%s登陆,马上为您进行登出服务"%self.name)
47 time.sleep(3)
48 break
49 count+=1#只允许用户输入三次错误的机会
50 else:
51 data[self.name]['timeout']=time.time()+180#如果三次输入错误,那么该用户将被锁定180秒
52 self.db=data#将改写过的值重新调用db.setter方法存入user.db文件中,在用户看来就是对db属性进行了重新的赋值操作
53 else:#如果判断用户的timeout大于1970年到现在的值
54 print('账号已经锁定180秒,剩余%s秒'%self.UserLockTime)
55 u1=User('egon') #实例化u1传入name,egon
56 u1.login()#u1调用login的方法
57 u1.loginOut()#u1调用loginOut方法
58 u2=User('alex')
59 u2.login()
												

Property Exercise的更多相关文章

  1. Extending JMeter – Creating Custom Config Element – Property File Reader

    JMeter is one of the best open source tools in the Test Automation Community. It comes with all the ...

  2. Conway's Game of Life: An Exercise in WPF, MVVM and C#

    This blog post was written for the Lockheed Martin Insight blog, sharing here for the external audie ...

  3. Property Finder – a Cross-Platform Xamarin MonoTouch Mobile App

    Developers are now finding themselves having to author applications for a diverse range of mobile pl ...

  4. day06-codes and exercise in class

    # Author: Ghost # Email: jiaci.liu@gmail.com ''' 1-Review of last week 2-interface class, abstract c ...

  5. 探究@property申明对象属性时copy与strong的区别

    一.问题来源 一直没有搞清楚NSString.NSArray.NSDictionary--属性描述关键字copy和strong的区别,看别人的项目中属性定义有的用copy,有的用strong.自己在开 ...

  6. JavaScript特性(attribute)、属性(property)和样式(style)

    最近在研读一本巨著<JavaScript忍者秘籍>,里面有一篇文章提到了这3个概念. 书中的源码可以在此下载.我将源码放到了线上,如果不想下载,可以直接访问在线网址,修改页面名就能访问到相 ...

  7. -Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HO 解决办法

    最近在使用maven,项目测试的时候出现了这么一个错.-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2 ...

  8. python property理解

    一般情况下我这样使用property: @property def foo(self): return self._foo # 下面的两个decrator由@property创建 @foo.sette ...

  9. Android动画效果之Property Animation进阶(属性动画)

    前言: 前面初步认识了Android的Property Animation(属性动画)Android动画效果之初识Property Animation(属性动画)(三),并且利用属性动画简单了补间动画 ...

随机推荐

  1. bash 和sed 和gawk

    bash内建命令 命令描述 : 扩展参数列表,执行重定向操作 . 读取并执行指定文件中的命令(在当前shell环境中) alias 为指定命令定义一个别名 bg 将作业以后台模式运行 bind 将键盘 ...

  2. How to Make a Computer Operating System

    How to Make a Computer Operating System 如何制作一个操作系统(翻译版) 原文地址:Github:How to Make a Computer Operating ...

  3. 关于C++中ios::sync_with_stdio(false)

    粘贴自:https://blog.csdn.net/weixin_44015865/article/details/84974373 在C++中的输入和输出有两种方式,一种是scanf和printf, ...

  4. Django使用redis实现缓存

    实现缓存的方式,有多种:本地内存缓存,数据库缓存,文件系统缓存.这里介绍使用Redis数据库进行缓存. 配置 CACHES = { "default": { "BACKE ...

  5. L1范数与L2范数​

    L1范数与L2范数​ ​ L1范数与L2范数在机器学习中,是常用的两个正则项,都可以防止过拟合的现象.L1范数的正则项优化参数具有稀疏特性,可用于特征选择:L2范数正则项优化的参数较小,具有较好的抗干 ...

  6. 趋势:flex和grid使布局更简单

    前言:记不久前面试的时候,面试官问我平时用什么布局方式,我非常耿直的说 div+css,利用position,float等布局,这就是非常传统的布局方式,通常都要写比较多的css代码:前几天在知乎上看 ...

  7. springboot2.0 JPA配置自定义repository,并作为基类BaseRepository使用

    springboot2.0 JPA配置自定义repository,并作为基类BaseRepository使用 原文链接:https://www.cnblogs.com/blog5277/p/10661 ...

  8. Left Join on 多条件查询时,条件过滤的问题

    例如:A  Left Join B on (...) on 后面的条件是对B数据的过滤,如果要对A的数据或者联合之后的数据集进行过滤,则要把过滤条件放在where子句中

  9. allure--下的各装饰器的翻译及自己的总结

    翻译图-快捷键 红色字体感觉用的会比较多,起码现在感觉应该是比其他的多一点 lable应该没有什么特殊的用法,只是对下面方法的一个汇总(或者可以这么说,下面的方法是lable更具体的实现) sever ...

  10. Unity --- 如何简单的判断图片是否含有 alpha channel

    var texImporter = AssetImporter.GetAtPath(assetPath) as TextureImporter; if (texImporter.DoesSourceT ...