原生sql批量删除/lambda配合filter使用/项目中使用logging模块

10/28

一、原生sql批量删除语句

delete from dimession where id between 6 and 9     #删除dimession表id 6-9的数据  

二、lambda配合filter使用

lambda单独使用:
    lambda x,y:x+y         #冒号左边放参数,冒号右边放返回值
filter使用:
    filter(function,iterable)    #第一个参数:判断函数,第二个参数:可迭代对象(数据)      结果返回迭代器对象
lambda+filter使用
    filter(lambda x:x%2,range(10))  #filter的数据循环传给lambda的x      结果返回迭代器对象

三、项目中使用logging模块

logging模块使用参考链接:https://www.cnblogs.com/Chaqian/archive/2020/05/30/12994521.html

import logging
LOG_FILENAME="/root/project/log.txt"
logging.basicConfig(filename=LOG_FILENAME,level=logging.INFO)    #消息记录到log.text中
logging.info("This message should go to the log file")           #打印到控制台

实际代码

config.py  配置

class DevelopmentConfig(Config):
    """开发环境下的配置"""
    DEBUG = True
    LOG_LEVEL = logging.DEBUG  #设置该环境下日志级别,大于这个级别的日志才会被记录

class ProductionConfig(Config):
    """生产环境下的配置"""
    DEBUG = False
    LOG_LEVEL = logging.WARNING


config = {
    "development": DevelopmentConfig,
    "production": ProductionConfig,
}

import logging
from logging.handlers import RotatingFileHandler
__init__.py
def setup_log(config_name):
    # 设置日志的记录等级  config_name=development/production
    # config环境配置
    logging.basicConfig(level=config[config_name].LOG_LEVEL)
    # 创建日志记录器,日志的保存路径,单个日志的最大容量,保存的日志文件上限数量
    #控制日志输出位置(文件中)
    file_log_handler = RotatingFileHandler("logs/log", maxBytes=1024 * 1024 * 100, backupCount=10)
    # 创建日志记录的格式 日志等级 输入日志信息的文件名 行数 日志信息
    formatter = logging.Formatter('[%(asctime)s] [%(levelname)s] [%(filename)s:%(lineno)d] [%(message)s]')
    # 为刚创建的日志记录器设置日志记录格式
    file_log_handler.setFormatter(formatter)
    # 为全局的日志工具对象(flask app使用的)添加日志记录器
    logging.getLogger().addHandler(file_log_handler)
原文地址:https://www.cnblogs.com/wangcuican/p/13891523.html