一、关于装饰器
#权限校验、用户认证、日志记录、性能测试、事务处理、缓存等都是装饰器的绝佳应用场景
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #!/usr/bin/env python 2 #-*- coding:utf-8 -*- 3 4 __author__ = 'andylin' 5 __date__ = '17-8-1 下午9:49' 6 7 8 9 import time 10 11 12 def timer(func): 13 def deco(*args,**kwargs): 14 start_time = time.time() 15 func(*args,**kwargs) 16 stop_time = time.time() 17 print("the func run time is %s" % (stop_time - start_time)) 18 return deco 19 20 21 @timer 22 def test1(): 23 time.sleep(1) 24 print("in the test1") 25 26 test1()
关于转入的参数
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
1 #!/usr/bin/env python 2 #-*- coding:utf-8 -*- 3 4 __author__ = 'andylin' 5 __date__ = '17-8-1 下午9:57' 6 7 8 uname = "andylin" 9 passwd = "abc123" 10 11 def auth(auto_type): 12 def login_check(func): 13 def login(*args,**kwargs): 14 username = input("Username:").strip() 15 password = input("Password:").strip() 16 if username == uname and passwd == password: 17 if auto_type == 'job': 18 func(*args,**kwargs) 19 print("Authentication Success! Welcome to job!") 20 elif auto_type == 'home': 21 func(*args,**kwargs) 22 print('Authentication Success!Welcome to home!') 23 else: 24 func(*args, **kwargs) 25 print('Authentication Success!Welcome to shcool!') 26 else: 27 print("Authentication Faild!") 28 return login 29 return login_check 30 31 32 @auth(auto_type='job') 33 def job(): 34 print("login Welcome to job!") 35 36 @auth(auto_type='home') 37 def home(): 38 print("login Welcome to home!") 39 40 @auth(auto_type='school') 41 def school(): 42 print("login Welcome to school!") 43 44 45 46 job() 47 home() 48 school()
二、关于了解Python的数据结构
容器(container)、可迭代对象(iterable)、迭代器(iterator)、生成器(generator)、列表/集合/字典推导式(list、set 、dict comprehension)众多概念
可以学习一下这个https://ask.hellobi.com/blog/pythoneer/7688 让我理解了很多
三、一些基本的函数
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#!/usr/bin/env python #-*- coding:utf-8 -*- __author__ = 'andylin' __date__ = '17-8-5 下午9:02' res = filter(lambda n:n>5,range(10)) res1 = [ i for i in range(10) if i > 5 ] for i in res: print(i) for i in res1: print(i) print("#"*50) res = map(lambda n:n+1,range(10)) # [ lambda n:n+1 for n in range(10) ] res1 = [ n+1 for n in range(10) ] res2 = [ lambda n:n+1 for n in range(10) ] for i in res: print(i) print("---"*30) for i in range(len(res2)): print(res2[i](i)) import functools res = functools.reduce(lambda x,y:x+y,range(10)) print(res) dict_list = { 6:2 , 8:0, 1:4, -5:6, 99:11 , 4:22 } print(sorted(dict_list.items(),key=lambda x:x[1]))
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#!/usr/bin/env python #-*- coding:utf-8 -*- __author__ = 'andylin' __date__ = '17-8-9 下午9:02' import random random_num = random.random() print(random_num) random_num_gt_int = random.uniform(0,9) print(random_num_gt_int) random_int = random.randint(0,10) print(random_int) random_range = random.randrange(1,9) print(random_range) list_src = [ 1,2,3,4,5,6,7,8,9 ] random.shuffle(list_src) print(list_src) print(random.sample(list_src,2)) choice = '' for i in range(4): num = random.randint(0,3) if i == num: tmp = chr(random.randint(65,90)) else: tmp = random.randint(1,9) choice +=str(tmp) print(choice)
四、日志模块
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#!/usr/bin/env python #-*- coding:utf-8 -*- __author__ = 'andylin' __date__ = '17-8-12 下午10:14' import logging def logs(messages): logger = logging.getLogger() handler = logging.FileHandler('access.log') fileFamter = logging.Formatter('%(asctime)s %(levelname)-4s %(message)s') handler.setFormatter(fileFamter) handler.setLevel(logging.WARNING) logger.addHandler(handler) shandle = logging.StreamHandler() shandle.setFormatter(fileFamter) shandle.setLevel(logging.WARNING) logger.addHandler(shandle) logger.warning(messages) logger.removeHandler(handler) logger.removeHandler(shandle) nowstr = "waring happing ..." logs(nowstr)
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#!/usr/bin/env python #-*- coding:utf-8 -*- __author__ = 'andylin' __date__ = '17-8-12 下午10:44' import logging from logging import handlers logger = logging.getLogger('Test') log_file = 'timelog.log' #fh = handlers.RotatingFileHandler(filename=log_file,maxBytes=10,backupCount=3,encoding='utf-8') fh = handlers.TimedRotatingFileHandler(filename=log_file,when='D',interval=7,backupCount=7) logfm = logging.Formatter('%(asctime)s %(levelname)-4s %(message)s') fh.setFormatter(logfm) fh.setLevel(logging.WARNING) logger.addHandler(fh) logger.warning('test1') logger.warning('test2') logger.warning('test3') logger.warning('test4')