python第五天

连写两天博客实在是!!!愚蠢的人类啊

1.双层(多层)装饰器:装饰器就是将原函数装饰成另外一个函数,既然又成为了函数,当然可以再被装饰,理解这个理念就可以了,层数关系是由下到上一层层被包装的!如果还不理解,去百度吧!北方不想给愚蠢的人类进行过多的解释(其实是语文学的不好的缘故...

2.模块:不要起一个跟内置模块重名的文件名!!!

3.生成器:yield

4.日志模块:

 1 #!/usr/bin/env python
 2 import logging
 3 logger= logging.getLogger('北方姆Q')
 4 logger.setLevel(logging.INFO)
 5 
 6 ch = logging.StreamHandler()
 7 ch.setLevel(logging.ERROR)
 8 
 9 fh = logging.FileHandler('log.txt')
10 fh.setLevel(logging.DEBUG)
11 
12 formatter1 = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
13 formatter2 = logging.Formatter('%(asctime)s - %(lineno)d - %(process)d- %(levelname)s - %(message)s')
14 
15 ch.setFormatter(formatter2)
16 fh.setFormatter(formatter1)
17 
18 logger.addHandler(ch)
19 logger.addHandler(fh)
20 
21 logger.debug('DEBUG!')
22 logger.info('INFO!')
23 logger.warning('WARNING!')
24 logger.error('ERROR!')
25 logger.critical('CRITICAL!')
1 C:UsersAdministratorAppDataLocalProgramsPythonPython35-32python.exe C:/Users/Administrator/PycharmProjects/ACE/study5/logfile.py
2 2016-08-11 11:52:51,155 - 24 - 6220- ERROR - ERROR!
3 2016-08-11 11:52:51,155 - 25 - 6220- CRITICAL - CRITICAL!
4 
5 Process finished with exit code 0

5.时间模块:

 1 #!/usr/bin/env python
 2 import time
 3 print(time.time())                  #从1970-1-1起到现在的时间戳
 4 print(time.ctime())                 #当前日期
 5 print(time.ctime(time.time()-86400))    #将时间戳转化成字符串
 6 print(time.gmtime())                #将当前时间戳转化成struct_time格式
 7 print(time.localtime())             #将本地时间戳转化成struct_time格式
 8 print(time.mktime(time.localtime()))    #与ocaltime()相反
 9 #time.sleep(3)                           #阻塞时间
10 print(time.strftime('%Y-%m-%d %H:%M:%S', time.gmtime()))    #将struct_time格式转换成指定格式
11 print(time.strptime('2012-12-12', '%Y-%m-%d'))    
 1 C:UsersAdministratorAppDataLocalProgramsPythonPython35-32python.exe C:/Users/Administrator/PycharmProjects/ACE/study5/shijianmokuai.py
 2 1470887616.780512
 3 Thu Aug 11 11:53:36 2016
 4 Wed Aug 10 11:53:36 2016
 5 time.struct_time(tm_year=2016, tm_mon=8, tm_mday=11, tm_hour=3, tm_min=53, tm_sec=36, tm_wday=3, tm_yday=224, tm_isdst=0)
 6 time.struct_time(tm_year=2016, tm_mon=8, tm_mday=11, tm_hour=11, tm_min=53, tm_sec=36, tm_wday=3, tm_yday=224, tm_isdst=0)
 7 1470887616.0
 8 2016-08-11 03:53:36
 9 time.struct_time(tm_year=2012, tm_mon=12, tm_mday=12, tm_hour=0, tm_min=0, tm_sec=0, tm_wday=2, tm_yday=347, tm_isdst=-1)
10 
11 Process finished with exit code 0
 1 #!/usr/bin/env python
 2 import datetime
 3 print(datetime.datetime.today())            #输出当前时间
 4 time_now = datetime.datetime.now()
 5 print(time_now.timetuple())                 #struct_time格式
 6 print(time_now.replace(2012, 2, 2))         #直接替换
 7 print(datetime.datetime.now() - datetime.timedelta(days=10))    #10天前
 8 print(datetime.datetime.now() + datetime.timedelta(days=10))    #10天后
 9 print(datetime.datetime.now() - datetime.timedelta(hours=10))   #10小时前
10 print(datetime.datetime.now() + datetime.timedelta(hours=10))   #10小时后
11 print(datetime.datetime.now() - datetime.timedelta(seconds=100))    #100秒前
12 print(datetime.datetime.now() + datetime.timedelta(seconds=100))    #100秒后
13 print(datetime.datetime.now() + datetime.timedelta(weeks=3))    #3周后
14 print(datetime.datetime.now() - datetime.timedelta(weeks=3))    #3周前
 1 C:UsersAdministratorAppDataLocalProgramsPythonPython35-32python.exe C:/Users/Administrator/PycharmProjects/ACE/study5/shijianmo.py
 2 2016-08-11 11:54:22.109105
 3 time.struct_time(tm_year=2016, tm_mon=8, tm_mday=11, tm_hour=11, tm_min=54, tm_sec=22, tm_wday=3, tm_yday=224, tm_isdst=-1)
 4 2012-02-02 11:54:22.109104
 5 2016-08-01 11:54:22.109104
 6 2016-08-21 11:54:22.109104
 7 2016-08-11 01:54:22.109104
 8 2016-08-11 21:54:22.109104
 9 2016-08-11 11:52:42.109104
10 2016-08-11 11:56:02.109104
11 2016-09-01 11:54:22.109104
12 2016-07-21 11:54:22.109104
13 
14 Process finished with exit code 0

GET到了新技能!!!!

原文地址:https://www.cnblogs.com/bfmq/p/5750519.html