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')) 修改完后要写入文件

python语法_模块_loggin_hashlib_conifgparser的更多相关文章

  1. python语法_模块

    方便调用的分组函数文件,一个py模块就是一个模块,模块分三类 python标准库 第三方模块 应程序自定义模块 模块的掉用: 可以把多个功能(函数)包含在一个模块文件里,调用时直接使用import 就 ...

  2. python语法_模块_os_sys

    os模块:提供对此操作系统进行操作的接口 os.getcwd() 获取python运行的工作目录. os.chdir(r'C:\USERs') 修改当前工作目录. os.curdir 返回当前目录 ( ...

  3. python语法_模块_time_datetime_random

    模块:可以被公共调用的. time import time print(time.time()) 时间戳方式显示时间. time.sleep(3) 休眠3秒 time.clock() 计算cpu执行时 ...

  4. python语法_模块_re(正则表达)

    字符串操作方法有: s = 'hello word' s.find('l') 查询第一个l的索引 s.replace('ll','xx') 替换 s.split('w') 以w进行分割 这些都是完全匹 ...

  5. Python 进阶_模块 & 包

    目录 目录 模块的搜索路径和路径搜索 搜索路径 命名空间和变量作用域的比较 变量名的查找覆盖 导入模块 import 语句 from-import 语句 扩展的 import 语句 as 自动载入模块 ...

  6. python语法_函数

    ---恢复内容开始--- 函数: 1 减少重复代码 2 定义一个功能,需要直接调用 3 保持代码一致性 def  funcation_name(参数s): 功能代码块0 参数可以为多个,传入时按照前后 ...

  7. python语法_装饰器

    装饰器的知识点储备: 1 作用域 2 高阶函数 3 闭包 内部函数,对外部作用作用域的变量进行了引用,该内部函数就认为是闭包, def outer(): x=10 def inner(): print ...

  8. python语法_文件操作

    牢记“”“能调用方法的一定是对象”“” 文件的操作流程, 1 建立文件(打开文件)open('filename','模式').read() [这一步其实就是创建对象] 2 通过句柄进行操作 3 关闭o ...

  9. python语法_列表生成器_生成器_迭代器_异常捕获

    列表生成式 a = [x for x in range(10)] print(a) x 可进行操作 a = [x*2 for x in range(10)] print(a) x甚至可以为函数, de ...

随机推荐

  1. 基于物品的协同过滤item-CF 之电影推荐 python

    推荐算法有基于协同的Collaboration Filtering:包括 user Based和item Based:基于内容 : Content Based 协同过滤包括基于物品的协同过滤和基于用户 ...

  2. vue打包遇到的坑

    1.-webkie-box-orient:vertical没大打包上,解决方案 /* ! autoprefixer: off */ -webkit-box-orient: vertical; /* a ...

  3. 小程序:动态监测input和取值

    1.输入框失去焦点取值 wxml: <input bindblur="tab" type='text'></input> js: tab:function( ...

  4. angular/cli 常用指令

    1.安装@angular/cli npm install -g @angular/cli 2.更新@angular/cli npm uninstall -g @angular/cli npm cach ...

  5. ***微信小程序学习文档和资料归档收集

    微信小程序官方文档: https://cloud.tencent.com/document/product/619 小程序培训视频教程: https://xw.qq.com/edu/201805140 ...

  6. css选择器+、~、>

    在css中,选择器是一种模式,用于选择需要添加样式的元素.它有多种选择器,我们看一下比较常见,但容易混淆的三个选择器+.~.> A + B(相邻兄弟选择器) 定义:css2. 匹配B元素,满足条 ...

  7. Entitas Learning Document

    Entitas Learning Document You can find Entitas project in there Entitas for Unity Github There are a ...

  8. C++ STL容器总结

    1.    STL 容器 1.    按种类划分 顺序容器( sequence containers):是一种各元素之间有顺序关系的线性表,是一种线性结构的可序群集.顺序性容器中的每个元素均有固定的位 ...

  9. MyCat分片规则--笔记(二)

    概述 myCat实现分库分表的策略,对数据量的处理带来很大的便利,这里主要整理下MyCat的使用以及常用路由算法,针对MyCat里面的事务.集群后续再做整理:另外内容整理,不免会参考技术大牛的博客,内 ...

  10. Useful Git Commands for me

    查看Git追踪的文件 git ls-files   移除远程仓库的文件夹 git rm -r --cached some-directory git commit -m "Remove th ...