day26 logging模块. collections模块. . random模块. https://www.cnblogs.com/jin-xin/articles/9265561.html

 logging  日志
#记录.花钱的,(淘宝)败家的,
# 访问的记录,
# 员工信息,debug等等都需要日志.
# 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)
# 日志的信息:不能写入文件与显示 同时进行.

2.高配版

# 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) #添加屏幕句柄
# sh.setFormatter(formatter) #设置屏幕格式
# fh.setFormatter(formatter)  #设置文件的格式
# fh.setLevel(logging.INFO) #设置文件显示的等级
#
# logging.debug("debug message")
# logging.info("info message")
# logging.warning("warning message")
# logging.error("error message")
# logging.critical("critical message")

2 # 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)

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位,数字与字母(不区分)相结合的随机验证码.

 

posted @ 2018-07-10 09:26  山东张铭恩  阅读(141)  评论(0编辑  收藏  举报