python语法_模块_loggin_hashlib_conifgparser

loggin模块 日志模块,级别从上往下依次增强,debug 和 info 默认不输出信息,只有后三种级别有输出信息

loggin.debug('')

loggin.info('')

loggin.waring('')

loggin.error('')

loggin.critical('')

日志有两种形式,第一种显示在前台(标准流),第二种是记录在日志里。可调的。

日志的设置,灵活设定日志级别和打印方式:

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='myapp.log',
                filemode='w')
logging.basicConfig函数各参数:
filename: 指定日志文件名
filemode: 和file函数意义相同,指定日志文件的打开模式,'w'或'a'
format: 指定输出的格式和内容,format可以输出很多有用信息,如上例所示:
 %(levelno)s: 打印日志级别的数值
 %(levelname)s: 打印日志级别名称
 %(pathname)s: 打印当前执行程序的路径,其实就是sys.argv[0]
 %(filename)s: 打印当前执行程序名
 %(funcName)s: 打印日志的当前函数
 %(lineno)d: 打印日志的当前行号
 %(asctime)s: 打印日志的时间
 %(thread)d: 打印线程ID
 %(threadName)s: 打印线程名称
 %(process)d: 打印进程ID
 %(message)s: 打印日志信息
datefmt: 指定时间格式,同time.strftime()
level: 设置日志级别,默认为logging.WARNING
stream: 指定将日志的输出流,可以指定输出到sys.stderr,sys.stdout或者文件,默认输出到sys.stderr,当stream和filename同时指定时,stream被忽略

 

灵活定制日志输出模式:

logger = loggin.getLogger()

fh = loggin.FileHandler('test.log')

ch = loggin.StreamHandler()

formmater = loggin.Formmater()

fh.setFormmater(formmater )

ch.setFormmater(formmater )

logger.addhandler(fh)

logger.addhandler(ch)

logger.setLevel(logging.DEBUG) 设置日志打印的默认级别

logger.debug('')

logger.info('')

logger.waring('')

logger.error('')

logger.critical('')

#########

hashlib 模块,从明文加密成密文。

加密算法 md5,hash

m = hashlib.md5()

m.update('hello word'.encode('utf8'))  注意unicode 类型 一定要转换成byte类型

m.hexdigest() 获取m 通过md5转换后的字符串

m.update('alxe') 多次update 就相当将内容拼接起来进行转换,等价于 m.update('hello wordalxe')

另外一种算法

hashlib.sha 128 256 512

m = hashlib.sha256()

 m.update('hello word'.encode('utf8'))

############

configparser 模块,配置文件模块

配置文件的定义和编写

config = configparser.ConfigParser()

config['DEFAULT'] = {'name':'gm','age':'11','sex':'man'}

config['bitbucket.org']={'user':'gh'}

config['topsecrt.net']={'port':'3306','type':'file'}  #这三个config只是定义配置该怎么写,但是并没有写到文件中,需要调用文件操作以及config.write方法才能正式写进去。

with open('example.ini','w') as configfile

  config.write(configfile)

 

修该配置文件

config = configparser.ConfigParser()

config.read(‘example.ini’) 先读取,再进行操作

print(config.sections()) 打印除掉default以外的所有sections name

print(config.defaults()) 打印default 下的信息。

asasa  in config: 返回True,False 用来判断 配置是否存在配置文件中。

print(config['bitbucket.org']['user']) 打印配置大段的 具体配置的值

 for key in config:

  print(key) 这里打印的是 配置文件里大块名

for key in config['bitbucket.org']:

  print(jkey) 打印包含default和 bitbucket.org 下的key

 

增删改

config.remove_sections() 删除快

 

config.has_sections('') 判断是否还包含快

config.set(‘sectionname’,'key','value') 修改快下的键值对

config.remove_options('serciontname','keyname')删除快下的键值对

 

config.write(open('i_cfg.ini','w')) 修改完后要写入文件

posted @ 2018-12-13 11:45  小水胖  阅读(125)  评论(0编辑  收藏  举报