24 day of python
python2x python3x 区别:
1,大环境:
python2x:
大神贡献源码,这些源码有自己语言的特色,源码不规范,源码重复代码太多.
python崇尚的是优美清晰简单.
python3x:
龟叔重新整理,将源码规范化,简单化,统一化.
2,python2x: print('内容') print 内容
python3x: print('内容')
3,python2x: raw_input() input() 只能输入数字
python3x: input()
4,python2x: range() ---> [1,2,3]
python3x: range() ---> 可迭代对象
logging 日志
1,被动触发:与异常处理配合,访问记录。
2,主动触发:检测运维人员输入的指令,检测服务器的重要信息,访问记录。等等
低配版 low版
import logging logging.basicConfig(level=logging.INFO, format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', filename='low版logging.log' ) msg = 'cpu 正常,硬盘参数...,流量的max:..最小值:.....' logging.info(msg) 日志的信息:不能写入文件与显示 同时进行。
第一版:只输入文件中. import logging logger = logging.getLogger() # 创建logger对象. fh = logging.FileHandler('高配版logging.log',encoding='utf-8') # 创建文件句柄 # 吸星大法 logger.addHandler(fh) logging.debug('debug message') logging.info('info message') logging.warning('warning message') logging.error('error message') logging.critical('critical message') 第二版:文件和屏幕都存在. import logging logger = logging.getLogger() # 创建logger对象. fh = logging.FileHandler('高配版logging.log',encoding='utf-8') # 创建文件句柄 sh = logging.StreamHandler() #产生了一个屏幕句柄 # 吸星大法 logger.addHandler(fh) #添加文件句柄 logger.addHandler(sh) #添加屏幕句柄 logging.debug('debug message') logging.info('info message') logging.warning('warning message') logging.error('error message') logging.critical('critical message') 第三版:文件和屏幕都存在的基础上 设置显示格式. import logging logger = logging.getLogger() # 创建logger对象. fh = logging.FileHandler('高配版logging.log',encoding='utf-8') # 创建文件句柄 sh = logging.StreamHandler() #产生了一个屏幕句柄 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') # 吸星大法 logger.addHandler(fh) #添加文件句柄 logger.addHandler(sh) #添加屏幕句柄 sh.setFormatter(formatter) # 设置屏幕格式 fh.setFormatter(formatter) # 设置文件的格式 (这两个按照需求可以单独设置) logging.debug('debug message') logging.info('info message') logging.warning('warning message') logging.error('error message') logging.critical('critical message') 第四版 文件和屏幕都存在的基础上 设置显示格式.并且设置日志水平. import logging logger = logging.getLogger() # 创建logger对象. fh = logging.FileHandler('高配版logging.log',encoding='utf-8') # 创建文件句柄 sh = logging.StreamHandler() #产生了一个屏幕句柄 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') # logger.setLevel(logging.DEBUG) #如果你对logger对象设置日志等级.那么文件和屏幕都设置了. #总开关 默认从warning开始,如果想设置分开关:必须要从他更高级:(ERROR,critical)从这来个开始. # 吸星大法 logger.addHandler(fh) #添加文件句柄 logger.addHandler(sh) #添加屏幕句柄 sh.setFormatter(formatter) # 设置屏幕格式 fh.setFormatter(formatter) # 设置文件的格式 (这两个按照需求可以单独设置) fh.setLevel(logging.DEBUG) logging.debug('debug message') logging.info('info message') logging.warning('warning message') logging.error('error message') logging.critical('critical message')
collection模块
namedtuple
tu = (1,2) print(tu[0],tu[1]) from collections import namedtuple point = namedtuple('Point',['x','y']) p = point(10,30) # p = (x=10,y=30) print(p.x) print(p.y) print(p[0]) print(p[1])
deque 双向队列
from collections import deque q = deque(['a','b','c','d','e']) print(q) q.append(666) # 添加最右边 q.append(777) q.appendleft(111) # 从最左边添加 q.appendleft(222) print(q) q.pop() # 从右边删除 q.popleft() # 从左边删除 q.popleft() print(q)
queue队列 原则:先进先出.fifo
栈:先进后出
dic = {} dic['name'] = 'alex' dic['age'] = '1000' dic['sex'] = '男' print(dic) from collections import OrderedDict od = OrderedDict() od['name'] = 'alex' od['age'] = '1000' od['sex'] = '男' print(od)
装逼版:
d = dict([('a',1),('b',2),('c',3)]) print(d) from collections import OrderedDict od1 = OrderedDict([('a',1),('b',2),('c',3)]) print(od1)
l1 = [11,22,33,44,55,77,88,99,90] dic = {} for i in l1: if i > 66: if 'key1' not in dic: dic['key1'] = [] dic['key1'].append(i) else: if 'key2' not in dic: dic['key2'] = [] dic['key2'].append(i) print(dic)
from collections import defaultdict l1 = [11, 22, 33,44,55,66,77,88,99,90] my_dict = defaultdict(list) my_dict['key1'] my_dict['key2'] print(my_dict)
from collections import defaultdict l1 = [11, 22, 33,44,55,66,77,88,99,90] my_dict = defaultdict(list) for value in l1: if value > 66: my_dict['k1'].append(value) else: my_dict['k2'].append(value) print(my_dict)
创建个字典
dic1 = {} #---> dic1={1:5,2:5,3:5.....20:5}
for i in range(1,21) dic1[i] = 5 print(dic1) dic1 = {x:5 for x in range(1,21)} dic1 = dict.fromkeys(range(1,21),5) dic1 = defaultdict(lambda:5) for i in range(1,21): dic[i] print(dic1)
from collections import Counter c = Counter('abcdeabcdabcabafkjdslajlkfd') print(c)
random模块
import random # 随机 import random print(random.random()) # 0~1 之间的小数 print(random.uniform(1,3)) # 1~3小数 print(random.randint(1,5)) # 1 <= x <= 5 print(random.randrange(1,10,2)) #1<=x<10 的奇数 顾首不顾尾 print(random.choice([1,'23',2,3])) # 任选一个 常用 print(random.choice('fdsjafdsfgdsagfdsa')) # 任选一个 常用 random.sample([1,1,1,1,1,6,7,8,9,10],2) # 列表元素任意2个组合 item = [i for i in range(1,14)] random.shuffle(item) # 打乱顺序 print(item)