第26天

今天总共讲了configparser、logging、collections三个模块

1、configparser:配置模块

形如字典,格式为

# import configparser
# config = configparser.ConfigParser()
# config["DEFAULT"] = {'a': '45',
#                       'Compression': 'yes',
#                      'CompressionLevel': '9',
#                      'ForwardX11':'yes'
#                      }
#
# config['bitbucket.org'] = {'User':'hg'}
# config['topsecret.server.com'] = {'Host Port':'50022','ForwardX11':'no'}
# with open('example.ini', 'w') as f:
#    config.write(f)

这要写进去了,就是这么个效果;

也就是说,等号左边的中括号中的内容作为标题,键值对作为赋值(这叫什么,我也不知道,反正就这意思),有瑕疵,就是里面的除了default都是小写,如果要是有这个大小写的需求就不要用这

2、logging模块:形成日志

注意这里,日志的内容是程序猿们自己写的,而不是他自己就会全自动帮你写

logging分为两大类:

(1)简单配置模式:

import logging
# 默认情况下 只显示 警告 及警告级别以上信息
# logging.basicConfig(level=logging.DEBUG,
#                     format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s',
#                     datefmt='%a, %d %b %y %H:%M:%S',
#                     filename = 'userinfo.log'
#                     )
# logging.debug('debug message')       # debug 调试模式 级别最低
# logging.info('info message')         # info  显示正常信息
# logging.warning('warning message')   # warning 显示警告信息
# logging.error('error message')       # error 显示错误信息
# logging.critical('critical message') # critical 显示严重错误信息

简单是很简单,但是这就造成了它的不灵活,而且它的编码形式不能改,也不能在写在文件的同时输出在屏幕上。

(2)logger :配置logger对象

自由度很高,就是看不懂

import logging
logger = logging.getLogger()  # 实例化了一个logger对象

fh = logging.FileHandler('test.log',encoding='utf-8')    # 实例化了一个文件句柄
sh = logging.StreamHandler()

fmt = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
fh.setFormatter(fmt)   # 格式和文件句柄或者屏幕句柄关联
sh.setFormatter(fmt)
sh.setLevel(logging.WARNING)

# 吸星大法
logger.addHandler(fh)  # 和logger关联的只有句柄
logger.addHandler(sh)
logger.setLevel(logging.DEBUG)

logger.debug('debug message')       # debug 调试模式 级别最低
logger.info('info message')         # info  显示正常信息
logger.warning('warning message')   # warning 显示警告信息
logger.error('error message')       # error 显示错误信息
logger.critical('critical message')

 logging模块提供5中日志级别,从低到高一次:debug info warning error critical
 默认从warning模式开始显示
 只显示一些基础信息,我们还可以对显示的格式做一些配置

3、collections模块

from collections import deque

这个是双端队列

这里牵扯到了一个队列(queue)的概念

所谓队列,则是一个有序的列表,先进先出叫做队列,先进后出叫做栈

个人认为理解为一个列表最为恰当。

其方法有append(右加)appendleft(左加)

还有个namedtuple的例子,就是一个有名字的元组,下例就是最好的解释

# from collections import namedtuple
# Point = namedtuple('Point',['x','y'])
# p = Point(1,2)
# p = Point(1,2)
# print(p)
# print(p.x)
# print(p.y)

这个是x,y坐标的例子,通过这个方法,可以看起来比单纯的元组要生动一些

猜你喜欢

转载自www.cnblogs.com/ylx900/p/8920731.html