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,
maxconnections=5,
mincached=2,
maxcached=2,
maxshared=3,
blocking=True,
setsession=[],
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()
日志打印
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():
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