python learning module to add two

1, logging module

Basic Configuration 1.1 logging module

import logging

logging.basicConfig(
    # 1、日志输出位置:1、终端 2、文件
    filename='access.log', # 不指定,默认打印到终端

    # 2、日志格式
    format='%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s',
    
    # 3、时间格式
    datefmt='%Y-%m-%d %H:%M:%S %p',
    
    # 4、日志级别
    # critical => 50
    # error => 40
    # warning => 30
    # info => 20
    # debug => 10
    level=10,

)

logging.debug('调试debug') # 10
logging.info('消息info')   # 20
logging.warning('警告warn')# 30
logging.error('egon提现失败') # 40
logging.critical('严重critical') # 50

1.2 log configuration dictionary

1.2.1 defines three log output format, the format string may be used as the log

%(name)s Logger的名字
%(levelno)s 数字形式的日志级别
%(levelname)s 文本形式的日志级别
%(pathname)s 调用日志输出函数的模块的完整路径名,可能没有
%(filename)s 调用日志输出函数的模块的文件名
%(module)s 调用日志输出函数的模块名
%(funcName)s 调用日志输出函数的函数名
%(lineno)d 调用日志输出函数的语句所在的代码行
%(created)f 当前时间,用UNIX标准的表示时间的浮 点数表示
%(relativeCreated)d 输出日志信息时的,自Logger创建以 来的毫秒数
%(asctime)s 字符串形式的当前时间。默认格式是 “2003-07-08 16:49:45,896”。逗号后面的是毫秒
%(thread)d 线程ID。可能没有
%(threadName)s 线程名。可能没有
%(process)d 进程ID。可能没有
%(message)s用户输出的消息

1.2.2 stressed: where the% (name) s is designated when getlogger name

standard_format = '%(asctime)s - %(threadName)s:%(thread)d - 日志名字:%(name)s - %(filename)s:%(lineno)d -' \
                  '%(levelname)s - %(message)s'

simple_format = '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s'

test_format = '%(asctime)s] %(message)s'

1.2.3 log configuration dictionary

LOGGING_DIC = {
    'version': 1,
    'disable_existing_loggers': False,
    'formatters': {
        'standard': {
            'format': standard_format
        },
        'simple': {
            'format': simple_format
        },
        'test': {
            'format': test_format
        },
    },
    'filters': {},

    # handlers是日志的接收者,不同的handler会将日志输出到不同的位置

​    'handlers': {
​        #打印到终端的日志
​        'console': {
​            'level': 'DEBUG',
​            'class': 'logging.StreamHandler',  # 打印到屏幕
​            'formatter': 'simple'
​        },
​        'default': {
​            'level': 'DEBUG',
​            'class': 'logging.handlers.RotatingFileHandler',  # 保存到文件

            # 'maxBytes': 1024*1024*5,  # 日志大小 5M

​            'maxBytes': 1000,
​            'backupCount': 5,
​            'filename': 'a1.log',  # os.path.join(os.path.dirname(os.path.dirname(__file__)),'log','a2.log')
​            'encoding': 'utf-8',
​            'formatter': 'standard',

​        },
​        #打印到文件的日志,收集info及以上的日志
​        'other': {
​            'level': 'DEBUG',
​            'class': 'logging.FileHandler',  # 保存到文件
​            'filename': 'a2.log', # os.path.join(os.path.dirname(os.path.dirname(__file__)),'log','a2.log')
​            'encoding': 'utf-8',
​            'formatter': 'test',
​    
​        },
​    },

    # loggers是日志的产生者,产生的日志会传递给handler然后控制输出

​    'loggers': {
​        #logging.getLogger(__name__)拿到的logger配置
​        'kkk': {
​            'handlers': ['console','other'],  # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕
​            'level': 'DEBUG', # loggers(第一层日志级别关限制)--->handlers(第二层日志级别关卡限制)
​            'propagate': False,  # 默认为True,向上(更高level的logger)传递,通常设置为False即可,否则会一份日志向上层层传递
​        },
​        '终端提示': {
​            'handlers': ['console',],  # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕
​            'level': 'DEBUG',  # loggers(第一层日志级别关限制)--->handlers(第二层日志级别关卡限制)
​            'propagate': False,  # 默认为True,向上(更高level的logger)传递,通常设置为False即可,否则会一份日志向上层层传递
​        },
​        '': {
​            'handlers': ['default', ],  # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕
​            'level': 'DEBUG',  # loggers(第一层日志级别关限制)--->handlers(第二层日志级别关卡限制)
​            'propagate': False,  # 默认为True,向上(更高level的logger)传递,通常设置为False即可,否则会一份日志向上层层传递
​        },
​    },

}


1.2.3 generated log files

The next thing to do is: get the log that is generated by loggers to have a log
generated by the first log: kkk
generated by the second log: bbb

But we need to import log configuration dictionary LOGGING_DIC

import settings
from logging import config,getLogger

config.dictConfig(settings.LOGGING_DIC)


logger1=getLogger('kkk')
logger1.info('这是一条info日志')

logger2=getLogger('终端提示')
logger2.info('logger2产生的info日志')

logger3=getLogger('用户交易')
logger3.info('logger3产生的info日志')

logger4=getLogger('用户常规')
logger4.info('logger4产生的info日志')

Two important supplementary amount of knowledge

1) log naming names

Log name is the difference between the log vesting of a very important identity

2) log rotation

Logging critical information process programmer running

2, the regular expression (re module)

import re

print(re.findall('\w','aAbc123_*()-='))
print(re.findall('\W','aAbc123_*()-= '))
print(re.findall('\s','aA\rbc\t\n12\f3_*()-= '))
print(re.findall('\S','aA\rbc\t\n12\f3_*()-= '))
print(re.findall('\d','aA\rbc\t\n12\f3_*()-= '))
print(re.findall('\D','aA\rbc\t\n12\f3_*()-= '))
print(re.findall('\D','aA\rbc\t\n12\f3_*()-= '))
print(re.findall('\Aalex',' alexis alex sb'))
                         alex
print(re.findall('sb\Z',' alexis alexsb sb'))
                                      sb\Z
print(re.findall('sb\Z',"""alex
alexis
alex
sb
"""))

print(re.findall('^alex','alexis alex sb'))
print(re.findall('sb$','alexis alex sb'))
print(re.findall('sb$',"""alex
alexis
alex
sb
"""))

print(re.findall('^alex$','alexis alex sb'))
print(re.findall('^alex$','al       ex'))
print(re.findall('^alex$','alex'))
重复匹配:| . | * | ? | .* | .*? | + | {n,m} |
1、.:匹配除了\n之外任意一个字符,指定re.DOTALL之后才能匹配换行符
print(re.findall('a.b','a1b a2b a b abbbb a\nb a\tb a*b'))
                                                  a.b
['a1b','a2b','a b','abb','a\tb','a*b']
print(re.findall('a.b','a1b a2b a b abbbb a\nb a\tb a*b',re.DOTALL))

2、*:左侧字符重复0次或无穷次,性格贪婪
print(re.findall('ab*','a ab abb abbbbbbbb bbbbbbbb'))
                                               ab*
['a','ab','abb','abbbbbbbb']

3、+:左侧字符重复1次或无穷次,性格贪婪
print(re.findall('ab+','a ab abb abbbbbbbb bbbbbbbb'))
                        ab+

4、?:左侧字符重复0次或1次,性格贪婪
print(re.findall('ab?','a ab abb abbbbbbbb bbbbbbbb'))
                                               ab?
['a','ab','ab','ab']

5、{n,m}:左侧字符重复n次到m次,性格贪婪
{0,} => *
{1,} => +
{0,1} => ?
{n}单独一个n代表只出现n次,多一次不行少一次也不行

print(re.findall('ab{2,5}','a ab abb abbb abbbb abbbbbbbb bbbbbbbb'))
                                                          ab{2,5}
['abb','abbb','abbbb','abbbbb]

print(re.findall('\d+\.?\d*',"asdfasdf123as1111111.123dfa12adsf1asdf3"))
                                                                  \d+\.?\d*                                      \d+\.?\d+


[]匹配指定字符一个
print(re.findall('a\db','a1111111b a3b a4b a9b aXb a b a\nb',re.DOTALL))
print(re.findall('a[501234]b','a1111111b a3b a4b a9b aXb a b a\nb',re.DOTALL))
print(re.findall('a[0-5]b','a1111111b a3b a1b a0b a4b a9b aXb a b a\nb',re.DOTALL))
print(re.findall('a[0-9a-zA-Z]b','a1111111b axb a3b a1b a0b a4b a9b aXb a b a\nb',re.DOTALL))

print(re.findall('a[^0-9a-zA-Z]b','a1111111b axb a3b a1b a0b a4b a9b aXb a b a\nb',re.DOTALL))
print(re.findall('a-b','a-b aXb a b a\nb',re.DOTALL))
print(re.findall('a[-0-9\n]b','a-b a0b a1b a8b aXb a b a\nb',re.DOTALL))

Guess you like

Origin www.cnblogs.com/leilijian/p/12613289.html