logging模块,collections模块,random模块

logging日志模块

低配版

import logging
logging.basicConfig(lexel=logging.INFO,
                            format='%(asctime)s%(filename)s[line:%(lineno)d]%(levelname)s%(message)s
                             filename='low版logging.log'
    
                            )    
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() #创建日志对象
fh = logging.Filenhandler(''高配版logging.log',encoding='utf-8') #创建一个文件句柄
sh = logging.StreamHandler() # 创建一个屏幕句柄
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger.setLevel(logging.DEBUG)

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')

  collections 模块 提供特殊的数据类型模块

from collections import namedtuple #对应值的元组
point = namedtuple('Point',['x','y'])
 p = point(10,30) # p = (x=10,y=30)

q = deque(['a','b','c','d','e']) #双向队列 原则;先进先出

q.appendleft(111)左

from collections import OrderedDict 添加的字典的元素变成有序的
od = OrderedDict()

Counter类的目的是用来跟踪值出现的次数

random

>>> random.random()      # 大于0且小于1之间的小数
0.7664338663654585
>>> random.uniform(1,3) #大于1小于3的小数
1.6270147180533838
#随机整数 >>> random.randint(1,5) # 大于等于1且小于等于5之间的整数 >>> random.randrange(1,10,2) # 大于等于1且小于10之间的奇数 #随机选择一个返回 >>> random.choice([1,'23',[4,5]]) # #1或者23或者[4,5]*** #随机选择多个返回,返回的个数为函数的第二个参数 >>> random.sample([1,'23',[4,5]],2) # #列表元素任意2个组合 *** [[4, 5], '23'] #打乱列表顺序 >>> item=[1,3,5,7,9] >>> random.shuffle(item) # 打乱次序 >>> item [5, 1, 3, 7, 9] >>> random.shuffle(item) >>> item [5, 9, 7, 1, 3]
原文地址:https://www.cnblogs.com/wy3713/p/9285946.html