zoukankan      html  css  js  c++  java
  • Python常用模块

    configparser

    配置文件a.cfg如下:

    [section1]
    k1 = v1
    k2:v2
    user=jack
    age=18
    is_admin=true
    
    
    [section2]
    k1 = v1
    

    读取内容

    import configparser
    
    config=configparser.ConfigParser()
    config.read('a.cfg')
    
    #查看所有的标题
    res=config.sections() #['section1', 'section2']
    print(res)
    
    #查看标题section1下所有key=value的key
    options=config.options('section1')
    print(options) #['k1', 'k2', 'user', 'age', 'is_admin', 'salary']
    
    #查看标题section1下所有key=value的(key,value)格式
    item_list=config.items('section1')
    print(item_list) #[('k1', 'v1'), ('k2', 'v2'), ('user', 'egon'), ('age', '18'), ('is_admin', 'true'), ('salary', '31')]
    
    #查看标题section1下user的值=>字符串格式
    val=config.get('section1','user')
    print(val) #jack
    
    #查看标题section1下age的值=>整数格式
    val1=config.getint('section1','age')
    print(val1) #18
    
    #查看标题section1下is_admin的值=>布尔值格式
    val2=config.getboolean('section1','is_admin')
    print(val2) #True
    
    #查看标题section1下salary的值=>浮点型格式
    val3=config.getfloat('section1','salary')
    print(val3) #31.0
    

    改写

    import configparser
    
    config = configparser.ConfigParser()
    config.read('a.cfg',encoding='utf8')
    config.remove_section('section2')
    
    config.remove_option('section1', 'k1')
    
    config.add_section('section3')
    config.set('section3', 'niefeng','wudi')
    config.write(open('b.cfg', 'w', encoding='utf8'))
    

    logging

    日志级别

    CRITICAL = 50 #FATAL = CRITICAL
    ERROR = 40
    WARNING = 30 #WARN = WARNING
    INFO = 20
    DEBUG = 10
    

    默认级别为warning,默认打印到终端

    logging全局配置,针对所有logger有效

    import logging
    logging.basicConfig(filename='access.log',
                        format='%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S',
                        level=10
                        )
    
    logging.debug('debug')
    logging.info('info')
    logging.warning('warn')
    logging.error('error')
    logging.critical('critical')
    

    logging.basicConfig配置参数

    可在logging.basicConfig()函数中可通过具体参数来更改logging模块默认行为,可用参数有
    filename:用指定的文件名创建FiledHandler(后边会具体讲解handler的概念),这样日志会被存储在指定的文件中。
    filemode:文件打开方式,在指定了filename时使用这个参数,默认值为“a”还可指定为“w”。
    format:指定handler使用的日志显示格式。
    datefmt:指定日期时间格式。
    level:设置rootlogger(后边会讲解具体概念)的日志级别
    stream:用指定的stream创建StreamHandler。可以指定输出到sys.stderr,sys.stdout或者文件,默认为sys.stderr。若同时列出了filename和stream两个参数,则stream参数会被忽略。
    
    
    format参数中可能用到的格式化串:
    %(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用户输出的消息
    

    logger对象配置

    
    import logging
    
    # 1、logger对象:负责产生日志,然后交给Filter过滤,然后交给不同的Handler输出
    # admin 表示日志的记录者,也可以用程序名__file__
    logger=logging.getLogger('admin')
    
    #2、Filter对象:不常用,略
    
    #3、Handler对象:接收logger传来的日志,然后控制输出
    h1=logging.FileHandler('t1.log',mode='a+') #打印到文件
    h2=logging.FileHandler('t2.log', mode='a+') #打印到文件
    h3=logging.StreamHandler() #打印到终端
    
    #4、Formatter对象:日志格式
    formmater1=logging.Formatter('%(asctime)s - %(name)s - %(levelname)s -%(module)s:  %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S %p',)
    
    formmater2=logging.Formatter('%(asctime)s :  %(message)s',
                        datefmt='%Y-%m-%d %H:%M:%S %p',)
    
    formmater3=logging.Formatter('%(name)s %(message)s',)
    
    
    #5、为Handler对象绑定格式
    h1.setFormatter(formmater1)
    h2.setFormatter(formmater2)
    h3.setFormatter(formmater3)
    
    #6、将Handler添加给logger并设置日志级别
    logger.addHandler(h1)
    logger.addHandler(h2)
    logger.addHandler(h3)
    logger.setLevel(10)
    
    # 7、测试
    logger.debug('debug')
    logger.info('info')
    logger.warning('warning')
    logger.error('error')
    logger.critical('critical')
    

    使用时候应该注意:logger是第一级过滤,只有等级大于等于logger的等级然后才能到handler,我们可以给logger和handler同时设置level

    生产使用

    方式一

    conf 目录下的settings.py

    import os
    
    # 定义三种日志输出格式 开始
    
    standard_format = '[%(asctime)s][%(threadName)s:%(thread)d][task_id:%(name)s][%(filename)s:%(lineno)d]' 
                      '[%(levelname)s][%(message)s]' #其中name为getlogger指定的名字
    
    simple_format = '[%(levelname)s][%(asctime)s][%(filename)s:%(lineno)d]%(message)s'
    # simple_format = '%(asctime)s %(filename)s[line: %(lineno)d] %(levelname)s: %(message)s'
    
    id_simple_format = '[%(levelname)s][%(asctime)s] %(message)s'
    
    # 定义日志输出格式 结束
    # 程序文件主目录
    BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
    logfile_dir = os.path.join(BASE_DIR, 'logs')  # log文件的目录
    
    logfile_name = 'my.log'  # log文件名
    
    # 如果不存在定义的日志目录就创建一个
    if not os.path.isdir(logfile_dir):
        os.mkdir(logfile_dir)
    
    # log文件的全路径
    logfile_path = os.path.join(logfile_dir, logfile_name)
    
    # log配置字典
    LOGGING_DIC = {
        'version': 1,
        'disable_existing_loggers': False,
        'formatters': {
            'standard': {
                'format': standard_format
            },
            'simple': {
                'format': simple_format
            },
        },
        'filters': {},
        'handlers': {
            #打印到终端的日志
            'console': {
                'level': 'DEBUG',
                'class': 'logging.StreamHandler',  # 打印到屏幕
                'formatter': 'simple'
            },
            #打印到文件的日志,RotatingFileHandler 的意思是"旋转",表示日志最多保留backupCount个最新的日志,旧日志会被删除替换
            'default': {
                'level': 'DEBUG',
                'class': 'logging.handlers.RotatingFileHandler',  # 保存到文件
                'formatter': 'standard',
                'filename': logfile_path,  # 日志文件
                'maxBytes': 1024*1024*5,  # 日志最大大小 5M
                'backupCount': 5,  # 随着日志的生成,最终只保留最新的5个日志文件,如my.log.1 my.log.2等
                'encoding': 'utf-8',  # 日志文件的编码,再也不用担心中文log乱码了
            },
        },
        'loggers': {
            #logging.getLogger(__name__)拿到的logger配置,根据 __name__ 得到的字符串为key去这个字典取logger对象,''表示字典没有对应的key就走这个默认值
            '': {
                'handlers': ['default', 'console'],  # 这里把上面定义的两个handler都加上,即log数据既写入文件又打印到屏幕
                'level': 'DEBUG',    # loger 对象的等级
                'propagate': True,  # 向上(更高level的logger)传递
            },
        },
    }
    

    utils目录下的log.py

    import logging.config
    from conf import settings
    
    def get_loger():
        logging.config.dictConfig(settings.LOGGING_DIC)  # 导入上面定义的logging配置
        logger = logging.getLogger()  # 生成一个log实例
        return logger
    

    settings里的配置已经在内部实现了单例模式,所以在不同线程调用调用get_loger():,得到的logger对象都是同一个

    方式二

    log.py

    import os
    import logging
    from config import settings
    
    
    class Logger(object):
        __instance = None
    
        def __init__(self):
            self.run_log_file = settings.RUN_LOG_FILE
            self.error_log_file = settings.ERROR_LOG_FILE
            self.run_logger = None
            self.error_logger = None
    
            self.initialize_run_log()
            self.initialize_error_log()
    
        def __new__(cls, *args, **kwargs):
            if not cls.__instance:
                cls.__instance = object.__new__(cls, *args, **kwargs)
            return cls.__instance
    
        @staticmethod
        def check_path_exist(log_abs_file):
            log_path = os.path.split(log_abs_file)[0]
            if not os.path.exists(log_path):
                os.mkdir(log_path)
    
        def initialize_run_log(self):
            self.check_path_exist(self.run_log_file)
            file_1_1 = logging.FileHandler(self.run_log_file, 'a', encoding='utf-8')
            fmt = logging.Formatter(fmt="%(asctime)s - %(levelname)s :  %(message)s")
            file_1_1.setFormatter(fmt)
            logger1 = logging.Logger('run_log', level=logging.INFO)
            logger1.addHandler(file_1_1)
            self.run_logger = logger1
    
        def initialize_error_log(self):
            self.check_path_exist(self.error_log_file)
            file_1_1 = logging.FileHandler(self.error_log_file, 'a', encoding='utf-8')
            fmt = logging.Formatter(fmt="%(asctime)s  - %(levelname)s :  %(message)s")
            file_1_1.setFormatter(fmt)
            logger1 = logging.Logger('run_log', level=logging.ERROR)
            logger1.addHandler(file_1_1)
            self.error_logger = logger1
    
        def log(self, message, mode=True):
            """
            写入日志
            :param message: 日志信息
            :param mode: True表示运行信息,False表示错误信息
            :return:
            """
            if mode:
                self.run_logger.info(message)
            else:
                self.error_logger.error(message)
    

    time和datetime


    结构化时间用来计算,格式化的字符串用来显示,时间戳是给计算机看的

    import datetime
    
    # print(datetime.datetime.now()) #返回 2016-08-19 12:47:03.941925
    #print(datetime.date.fromtimestamp(time.time()) )  # 时间戳直接转成日期格式 2016-08-19
    # print(datetime.datetime.now() )
    # print(datetime.datetime.now() + datetime.timedelta(3)) #当前时间+3天
    # print(datetime.datetime.now() + datetime.timedelta(-3)) #当前时间-3天
    # print(datetime.datetime.now() + datetime.timedelta(hours=3)) #当前时间+3小时
    # print(datetime.datetime.now() + datetime.timedelta(minutes=30)) #当前时间+30分
    
    # c_time  = datetime.datetime.now()
    # print(c_time.replace(minute=3,hour=2)) #时间替换
    
  • 相关阅读:
    学好VC++的十大良好习惯
    VC6.0调试技巧
    匈牙利命名法
    VC中常用文件操作(三)
    VL_FEAT——图像处理函数库
    MATLAB——linspace
    (Your)((Term)((Project)))(字符串处理)
    Matrix Chain Multiplication(栈的简单应用)
    The Triangle(DP基础)
    MATLAB——polyfit
  • 原文地址:https://www.cnblogs.com/longyunfeigu/p/9567273.html
Copyright © 2011-2022 走看看