要求一:自定义用户信息数据结构,写入文件,然后读出内容,利用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. 【新特性】JDK1.6

    一.Desktop类和SystemTray类 在JDK6中 ,AWT新增加了两个类:Desktop和SystemTray. 前者可以用来打开系统默认浏览器浏览指定的URL,打开系统默认邮件客户端给指定 ...

  2. Summary on deep learning framework --- Theano && Lasagne

     Summary on deep learning framework --- Theano && Lasagne 2017-03-23 1. theano.function outp ...

  3. 后台启动mysql

    https://blog.csdn.net/codemacket/article/details/77719323

  4. 读书笔记--《编写高质量代码:改善Python程序的91个建议》

      第一章 引论 建议写Pythonic式的代码,我理解为充分利用pythonAPI,用最简洁方式写出代码 1.两个变量交换: a, b = b, a 2.翻转list: a = [1, 2, 3, ...

  5. php rsa

    <?php $res=openssl_pkey_new(); // Get private key $ok = openssl_pkey_export($res, $privkey); // G ...

  6. JavaScript 随意记

    js 志异: <script> // ----- 1 --------- alert(new Date()) alert(+new Date()) </script>

  7. three.js初探,立体几何入手(一)

    前言:首先先推荐一篇博客,关于webgl原理,讲的非常之通俗易懂了 图解WebGL&Three.js工作原理  webGL可以理解为openGL ES2.0 (webGL2.0 - openG ...

  8. 遇到的Cocos2dx问题

    平时工作中,多多少少,总会遇到些棘手的问题,此文章会不断更新,避免下次再入坑  Cocos 官方相关下载 https://github.com/fusijie/Cocos-Resource Easin ...

  9. 数据库连接超时:“The last packet successfully received from the server was xxx milliseconds ago”

    产生的原因:应用方的数据库连接有效期时间,大于数据库自己设置的有效期. 解决方案: 一.修改druid配置(如果使用druid的话) spring.datasource.druid.validatio ...

  10. Keras + LSTM 做回归demo

    学习神经网络 想拿lstm 做回归, 网上找demo 基本三种: sin拟合cos 那个, 比特币价格预测(我用相同的代码和数据没有跑成功, 我太菜了)和keras 的一个例子 我基于keras 那个 ...