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(属性动画)(三),并且利用属性动画简单了补间动画 ...
随机推荐
- ldap集成bitbucket
confluence ldap配置跟jira ldap集成一样,请参考:https://www.cnblogs.com/imcati/p/9378668.html 需在 Global permissi ...
- Missing artifact com.oracle:ojdbc14:jar:10.2.0.3.0
1.Missing artifact com.oracle:ojdbc14:jar:10.2.0.3.0操作如下: 2.下载链接:链接:https://pan.baidu.com/s/1Ziyg2jl ...
- 根据字符串导入包使用-----importlib
import importlibo = importlib.import_module("xx.oo")s2 = "Person"the_class = get ...
- 【做题】POI2011R1 - Plot——最小圆覆盖&倍增
原文链接 https://www.cnblogs.com/cly-none/p/loj2159.html 题意:给出\(n\)个点,你需要按编号将其划分成不超过\(m\)段连续的区间,使得所有每个区间 ...
- java解压多层目录中多个压缩文件和处理压缩文件中有内层目录的情况
代码: package com.xiaobai; import java.io.File; import java.io.FileOutputStream; import java.io.IOExce ...
- 剑指offer:链表中倒数第k个结点
问题描述 输入一个链表,输出该链表中倒数第k个结点. 解题思路 两个指针都指向头结点,第一个指针先移动k-1个结点,之后两指针同时移动,当第一个指针到链表尾的时候,第二个指针刚好指向倒数第k个结点. ...
- div变成输入框
<style> #test{ width: 150px;; min-height:20px; max-height:70px; outline: 0; border: 1px solid ...
- day3——两数之和
// 小白一名,0算法基础,艰难尝试算法题中,若您发现本文中错误, 或有其他见解,往不吝赐教,感激不尽,拜谢. 领扣 第2题 今日算法 题干 //给定一个整数数组和一个目标值,找出数组中和为目标值的两 ...
- MySQL优化查询 5.7版本
1. 变更参数 : query_cache_type 如果何配置查询缓存: query_cache_type 这个系统变量控制着查询缓存工能的开启的关闭.query_cache_type=0时表示关闭 ...
- NetSec2019 20165327 Exp6 信息搜集与漏洞扫描
NetSec2019 20165327 Exp6 信息搜集与漏洞扫描 一.实践目标 掌握信息搜集的最基础技能与常用工具的使用方法. 二.实践内容 1.各种搜索技巧的应用 2.DNS IP注册信息的查询 ...