要求一:自定义用户信息数据结构,写入文件,然后读出内容,利用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. Vue PC后台系统组件大全

    1.https://vue.ant.design/ 2.http://element-cn.eleme.io/#/zh-CN 3.https://www.iviewui.com/ 4.https:// ...

  2. centos7 fortune+cowsay+lolcat美化初始终端

    前序 fortune+cowsay+lolcat效果图(每次打开新的终端的时候, 显示名言) fortune 提供我的rpm包, fortune+依赖 安装它们 rpm -ivh *.rpm 调配中文 ...

  3. Hessian序列化的一个潜在问题

    一. 最近的用rpc框架的时候,当用hessian序列化对象是一个对象继承另外一个对象的时候,当一个属性在子类和有一个相同属性的时候,反序列化后子类属性总是为null. 二. 示例代码: DTO对象 ...

  4. DocumentEvent事件的应用

    将用户在一个文本区输入的单词按字典序排好后放入另一个文本区,并添加菜单 example002类 package example; public class example002 { public st ...

  5. Integer类toString(int i,int radix)方法

    Integer类toString(int i,int radix)方法: 首先抛出java的api中的介绍: public static String toString(int i, int radi ...

  6. Hadoop3.2.0集群(4节点-无HA)

    1.准备环境 1.1配置dns # cat /etc/hosts 172.27.133.60 hadoop-01 172.27.133.61 hadoop-02 172.27.133.62 hadoo ...

  7. Star in Parentheses

    问题 A: Star in Parentheses 时间限制: 1 Sec  内存限制: 128 MB 题目描述 You are given a string S, which is balanced ...

  8. Slimming Plan

    问题 B: Slimming Plan 时间限制: 1 Sec  内存限制: 128 MB 题目描述 Chokudai loves eating so much. However, his docto ...

  9. 信息安全之路-web-xss学习(1)

    关于xss反射性漏洞 1.未加任何过滤的 (1).在dvwa平台上可以看到源代码,如下 low级别 为了便于理解,代码如下: <?php // Is there any input? if( a ...

  10. SonarQube 中文教程 (1)- 简介

    SonarQube是什么 SonarQube 是一个用于代码质量管理的开源平台,用于管理源代码的质量. 通过插件形式,可以支持包括 java, C#, C/C++, PL/SQL, Cobol, Ja ...