Property Exercise
要求一:自定义用户信息数据结构,写入文件,然后读出内容,利用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的更多相关文章
- 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 ...
- 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 ...
- 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 ...
- day06-codes and exercise in class
# Author: Ghost # Email: jiaci.liu@gmail.com ''' 1-Review of last week 2-interface class, abstract c ...
- 探究@property申明对象属性时copy与strong的区别
一.问题来源 一直没有搞清楚NSString.NSArray.NSDictionary--属性描述关键字copy和strong的区别,看别人的项目中属性定义有的用copy,有的用strong.自己在开 ...
- JavaScript特性(attribute)、属性(property)和样式(style)
最近在研读一本巨著<JavaScript忍者秘籍>,里面有一篇文章提到了这3个概念. 书中的源码可以在此下载.我将源码放到了线上,如果不想下载,可以直接访问在线网址,修改页面名就能访问到相 ...
- -Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HO 解决办法
最近在使用maven,项目测试的时候出现了这么一个错.-Dmaven.multiModuleProjectDirectory system property is not set. Check $M2 ...
- python property理解
一般情况下我这样使用property: @property def foo(self): return self._foo # 下面的两个decrator由@property创建 @foo.sette ...
- Android动画效果之Property Animation进阶(属性动画)
前言: 前面初步认识了Android的Property Animation(属性动画)Android动画效果之初识Property Animation(属性动画)(三),并且利用属性动画简单了补间动画 ...
随机推荐
- vue 去中心化的路由拆分方案:require.context
代码地址:https://github.com/lisiyizu/vue-router-dynamic
- Pandas: 使用str.replace() 进行文本清洗
str.replace()可以一次处理一整个Series.str.replace()的正式形式为 Series.str.replace(pat, repl) ,其中pat为想要寻找的模式,一般为正则表 ...
- 7-27 Codeforces Round #499 (Div. 2)
C. Fly 链接:http://codeforces.com/group/1EzrFFyOc0/contest/1011/problem/C 题型:binary search .math. 题意:总 ...
- Xilinx FPGA DPR技术
动态部分重配置技术 DPR(Dynamic Partial Reconfiguration)可以使得PL的一个部分或几个部分在运行时刻被完全地重新配置.这些部分需要被指定为可重配置分区(Reconfi ...
- 剑指offer 09:变态跳台阶
题目描述 一只青蛙一次可以跳上1级台阶,也可以跳上2级……它也可以跳上n级.求该青蛙跳上一个n级的台阶总共有多少种跳法. /* f(n-1) = f(n-2) + f(n-3) + ... + f(0 ...
- 《Spring技术内幕》读书笔记
简介: 1.spring 与unix.window这些操作在计算机系统中起到的作用是类似的 2.两大核心模块:IOC\AOP 3.为应用开发提供了许多现成的系统组件:事务处理.Web MV.JDBC. ...
- webpack学习笔记 (一)
一.安装nodejs: 点击打开nodejs官方站点: 点击下图框住的按钮,下周nodejs安装包: 安装下载好的安装包. 安装完毕之后,在cmd中输入node -v查看是否已经安装成功 如果有版本 ...
- 浅谈企业IT技术运营中台
关注嘉为科技,获取运维新知 如果你是IT圈内的人,在2月份,你的朋友圈里面最火的词应该就是“中台”了,我们在此不讨论企业的技术中台.数据中台.AI中台.业务中台,想和大家讨论一下IT技术运营中台. “ ...
- zoj3223
其实这题不超时完全是因为串长度太小,如果串够长,一次匹配后都要往上跳,复杂度是n^2的. #include <iostream> #include <cstdio> #incl ...
- 延迟提交form
提交按钮延迟提交form表单 function a(){document.getElementById('form1').submit();}setTimeout(a,5000);