1、logging 高级用法
# #logging 高级版 第一版 只在文件中显示或只在屏幕中显示 # 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') # #logging 高级版 第二版 文件和屏幕中都存在 # 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') # #logging 高级版 第三版 文件和屏幕中都存在,且设置显示格式 # 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) # fh.setFormatter(formatter) # sh.setFormatter(formatter) # logging.debug('debug message') # logging.info('info message') # logging.warning('warning message') # logging.error('error message') # logging.critical('critical message') # #logging 高级版 第四版 文件和屏幕中都存在,且设置显示格式 # #并且设置日志level水平 # 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.addHandler(fh) # logger.addHandler(sh) # fh.setFormatter(formatter) # sh.setFormatter(formatter) # fh.setLevel(logging.INFO) # sh.setLevel(logging.ERROR) # logging.debug('debug message') # logging.info('info message') # logging.warning('warning message') # logging.error('error message') # logging.critical('critical message')
2、collection模块()
# collections 模块给你提供的就是一些特殊的数据类型. #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) # 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), ('c', 3), ('b', 2),]) # 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) # 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): # dic1[i] # print(dic1) #from collections import Counter #c = Counter('abcdeabcdabcabafkjdslajlkfd') #print(c)
3、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) # 1题:4位,全数字的随机验证码 # 2题:4位,数字与字母(不区分)相结合的随机验证码. #logging 高级版 第一版 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')