python之路day04
2019.5.14,总结下今天学的知识:
装饰器:
定义:本质是函数,(装饰其他函数)就是为其他函数添加附加功能
原则:(1)不能修改被装饰函数的源代码 (2)不用修改被装饰函数的调用方式
1 # Author:K 2 ''' 3 装饰器: 4 定义:本质是函数,(装饰其他函数)就是为其他函数添加附加功能 5 原则:(1)不能修改被装饰函数的源代码 (2)不用修改被装饰函数的调用方式 6 7 ''' 8 9 # 现为test()加上计算test()运行时间的功能,并且要求不改变test()的源码,也不改变test()的调用方式 10 # 因此要定义一个装饰器 11 import time 12 def decorator(func): 13 def calculate_func(*args,**kwargs): 14 start_time = time.time() 15 func(*args,**kwargs) 16 end_time = time.time() 17 res = end_time - start_time 18 print("The run time of test1() is %s" % res) 19 return calculate_func 20 21 #-----------------现又为test()添加func2()功能 应该怎么实现??????? 22 23 24 # 在定义test()函数之前声明要添加的功能 @decorator 25 @decorator # 相当于 test = decorator1(test) 26 def test1(): 27 time.sleep(1) 28 print("in the test") 29 30 @decorator 31 def test2(name): 32 time.sleep(1) 33 print("name:",name) 34 35 36 test1() 37 test2('K')
1 # Author:K 2 ''' 3 装饰器高级版: 4 (1)index()里不需要输入账户密码 5 (2)homepage()需要输入账户密码 6 (3)bbs()里用远程的账户密码 7 8 ''' 9 10 user = "KisI" 11 psw = "111" 12 13 def auth(auth_type): 14 print("auth dunc:",auth_type) 15 def outer_wrapper(func): 16 def wrapper(*args,**kwargs): 17 print("wrapper func args:",*args,**kwargs) 18 if auth_type == "local": 19 username = input("Please input username:").strip() 20 password = input("Please input password:").strip() 21 if username == user and password == psw: 22 print("login successful!") 23 else: 24 print("wrong username or password!") 25 elif auth_type == "ldap": 26 print("ldap func --->") 27 return wrapper 28 return outer_wrapper 29 30 31 def index(): 32 print("Welcome!") 33 34 @auth(auth_type = "local") 35 def homepage(): 36 print("homepage!") 37 38 @auth(auth_type = "ldap") 39 def bbs(): 40 print("bbs") 41 42 index() 43 homepage() 44 #bbs()
生成器:只有在调用时才会生成相应的数据,
要想得到下一个数据,只能用__next__()方法,而且只能一个一个取,不能往回取
1 # Author:K 2 ''' 3 生成器:只有在调用时才会生成相应的数据, 4 要想得到下一个数据,只能用__next__()方法,而且只能一个一个取,不能往回取 5 ''' 6 7 test = (i*2 for i in range(10)) #定义一个生成器 8 9 print(test.__next__()) # 0 10 print(test.__next__()) # 2 11 12 # ---------------- 13 print("-----------") 14 # 生成器 15 def fibonacci(n): 16 count = 0 17 a , b = 0 , 1 18 while count < n: 19 yield b 20 a , b = b , a + b 21 count += 1 22 23 func_gen = fibonacci(10) #用函数定义一个生成器 24 25 for i in func_gen: 26 print(i)
另外,还有一个关于生成器并行的例子,对这个例子不是很明白。以后学懂了来补充!
1 # Author:K 2 #--------------------不是很明白这个例子!-------------------- 3 import time 4 def consumer(name): 5 print("%s准备吃包子啦!" % name) 6 while True: 7 steamed_bun = yield 8 print("%s吃了一个包子%s" % (name,steamed_bun)) 9 10 def producer(name): 11 c = consumer("I") 12 c.__next__() 13 print("%s开始做包子" % name) 14 for i in range(10): 15 time.sleep(1) 16 print("%s做了一个包子!" % name) 17 c.send(i) 18 19 20 21 producer("K")
迭代器:这里简单定义一下迭代器,简而言之,就是可直接作用于for循环的对象。
1 # Author:K 2 from collections import Iterable 3 from collections import Iterator 4 5 test_list = [1,2,3,4,5,7] 6 print(isinstance(test_list,Iterable)) # 判断是否是可迭代的 输出 --> True 7 8 test_iterator = iter(test_list) 9 print(isinstance(test_iterator,Iterator)) # 判断是否是迭代器 输出 --> True 10 11 for i in range(len(test_list)): 12 print(test_iterator.__next__())
内置方法:
1 # Author:K 2 3 # all(*args, **kwargs) 传入的可迭代的参数只要有0就是False 否则是True 4 print(all([0,2,3])) # False 5 print(all([])) # True 6 print(all([1,2])) # True 7 8 # any(*args, **kwargs) 传入的可迭代的参数只要有一个不为0就是True 否则是False 9 print(any([1,2,0])) # True 10 print(any([0])) # False 11 print(any([])) # False 12 13 # bin(*args, **kwargs) 把一个整数转成二进制 14 print(bin(7)) # 0b111 15 16 # 将code字符串变成可执行的代码 作用:与import相比,可以动态导入想要执行的代码 没啥用,直接exec()也行 17 str = ''' 18 print("Test!") 19 ''' 20 code = compile(str,"","exec") 21 # exec(code) 22 exec(str) 23 24 # eval() 将字符串变成字典 25 str = ''' 26 { 27 "name":"zhangsan", 28 "age":12 29 } 30 ''' 31 dict = eval(str) 32 print(dict) 33 34 #filter() 过滤不想要的值 35 res = filter(lambda n : n > 5,range(10)) #只要大于5的值 36 for i in res: 37 print(i) 38 39 #map() 对数据进行处理 40 res = map(lambda n : n > 5,range(10)) # 在范围里的数中,大于5返回True,小小于5返回False 41 print(type(res)) 42 for i in res: 43 print(i) 44 45 #reduce() 计算并返回结果 46 import functools 47 res = functools.reduce(lambda x,y:x+y,range(1,101)) #x是结果,y是第一个值。以此类推 48 print(res) 49 50 #hex() 把数字变成十六进制 51 print(hex(15)) 52 53 #oct() 把数字变成八进制 54 print(oct(15)) 55 56 # reversed(obj) 翻转对象 57 a = [555,77,3,22,56] 58 b = reversed(a) 59 for i in b: 60 print(i) 61 62 # sorted() 排序 63 print(sorted(a)) 64 65 a = {1:23,4:22,0:41} 66 print(sorted(a.items())) # 默认按照key排序 67 print(sorted(a.items(),key = lambda x:x[1])) #还可以自定义排序 68 69 # zip() 拉链函数 70 a = [1,2,3,4,5,6] #按最少的一一对应 71 b = ['a','b','c','d'] 72 for i in zip(a,b): 73 print(i)
作业:
模拟实现一个ATM + 购物商城程序
- 额度 15000或自定义
- 实现购物商城,买东西加入 购物车,调用信用卡接口结账
- 可以提现,手续费5%
- 每月22号出账单,每月10号为还款日,过期未还,按欠款总额 万分之5 每日计息
- 支持多账户登录
- 支持账户间转账
- 记录每月日常消费流水
- 提供还款接口
- ATM记录操作日志
- 提供管理接口,包括添加账户、用户额度,冻结账户等。。。
- 用户认证用装饰器
示例代码 https://github.com/triaquae/py3_training/tree/master/atm
简易流程图:https://www.processon.com/view/link/589eb841e4b0999184934329
参考博客:金角大王Alexhttps://www.cnblogs.com/alex3714/articles/5765046.html

浙公网安备 33010602011771号