Python常用数据库封装方法

mysql,mongo,redis 连接池方法

import pymysql
import redis
from aliyun.log import LogClient
from dbutils.persistent_db import PersistentDB
from dbutils.pooled_db import PooledDB
from pymongo import MongoClient


def connect_mongo_by_uri(uri):
    """
    :uri:
        example: "mongodb://[username[:password]@]host:port"
    """
    return MongoClient(uri)


def select_collection(client: MongoClient, datebase_name: str, collection_name: str):
    if not isinstance(client, MongoClient):
        raise TypeError("client is not a MongoClient instance")
    if not (datebase_name and collection_name):
        raise Exception("datebase_name or collection_name is empty")
    return client[datebase_name][collection_name]


def get_log_client(endpoint, access_id, access_key):
    return LogClient(endpoint, access_id, access_key)


def connect_mysql_by_param(host: str = 'localhost', port: int = 3306,
                           user: str = 'root', passwd: str = 'root', db: str = '', charset: str = "utf8"):
    """
     连接mysql
    :param host:
    :param port:
    :param user:
    :param passwd:
    :param db:
    :param charset:
    :return: conn
    """
    return pymysql.connect(host=host, port=port, user=user, passwd=passwd, db=db, charset=charset)


def connect_redis_by_uri(host: str = 'localhost', port: int = 6379,
                         passwd: str = 'root', max_connections: int = 1024, db: int = 0, **kwargs):
    """

    :param max_connections:
    :param host:
    :param port:
    :param passwd:
    :param db:
    :param kwargs: 其余参数可补充
    :return:
    """
    pool = redis.ConnectionPool(host=host, port=port, password=passwd, max_connections=max_connections, db=db, **kwargs)
    conn = redis.Redis(connection_pool=pool)
    return conn


def connect_mysql_pool_by_param(configs):
    '''
    创建数据库连接池
    :param configs:  mysql 单独连接,写入host,db,等主要参数
    :param is_mult_thread: 传入Bool值,True:多线程连接  False:单线程连接
    :return:
    '''
    poolDB = PooledDB(
        # 指定数据库连接驱动
        creator=pymysql,
        # 连接池允许的最大连接数,0和None表示没有限制
        maxconnections=5,
        # 初始化时,连接池至少创建的空闲连接,0表示不创建
        mincached=2,
        # 连接池中空闲的最多连接数,0和None表示没有限制
        maxcached=2,
        # 连接池中最多共享的连接数量,0和None表示全部共享(其实没什么卵用)
        maxshared=3,
        # 连接池中如果没有可用共享连接后,是否阻塞等待,True表示等等,
        # False表示不等待然后报错
        blocking=True,
        # 开始会话前执行的命令列表
        setsession=[],
        # ping Mysql服务器检查服务是否可用
        ping=0,
        **configs
    )
    return poolDB

字符串相似度匹配

import difflib


def string_similar(s1, s2):
    """
    字符串,相似度匹配,
    :param s1:
    :param s2:
    :return:
    """
    return difflib.SequenceMatcher(None, s1, s2).quick_ratio()

日志打印

#!/usr/bin/python
# encoding=utf-8

import os
import logging
import platform
import time
from multiprocessing import current_process
from logging.handlers import RotatingFileHandler

PROJECT_PATH = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))


def get_info_logger(log_name):
    logger = logging.getLogger()
    handler = RotatingFileHandler(
        f"{PROJECT_PATH}/logs/{log_name}_{int(time.time())}.log", maxBytes=20 * 1024 * 1024, backupCount=5,
        encoding="utf-8")
    handler.setFormatter(logging.Formatter(
        fmt="[%(asctime)s] - [%(threadName)s] - {%(module)s:%(funcName)s:%(lineno)d} %(levelname)s - %(message)s",
        datefmt="%Y-%m-%d %H:%M:%S"))
    handler.setLevel(logging.INFO)
    logger.setLevel(logging.INFO)
    logger.addHandler(handler)
    if not judge_system(): # 默认linux运行,不在终端输出日志
        logger.addHandler(logging.StreamHandler())

    return logger


def judge_system():
    sys = platform.system()
    if sys == "Windows":
        return False
    elif sys == "Linux":
        return True
    else:
        return False

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值