terryutils 1.0.3__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- terryutils-1.0.3/PKG-INFO +18 -0
- terryutils-1.0.3/README.md +8 -0
- terryutils-1.0.3/pyproject.toml +21 -0
- terryutils-1.0.3/setup.cfg +4 -0
- terryutils-1.0.3/terryutils/__init__.py +2 -0
- terryutils-1.0.3/terryutils/log.py +32 -0
- terryutils-1.0.3/terryutils/mysql_util.py +108 -0
- terryutils-1.0.3/terryutils.egg-info/PKG-INFO +18 -0
- terryutils-1.0.3/terryutils.egg-info/SOURCES.txt +10 -0
- terryutils-1.0.3/terryutils.egg-info/dependency_links.txt +1 -0
- terryutils-1.0.3/terryutils.egg-info/requires.txt +1 -0
- terryutils-1.0.3/terryutils.egg-info/top_level.txt +2 -0
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: terryutils
|
|
3
|
+
Version: 1.0.3
|
|
4
|
+
Summary: My personal python toolkit
|
|
5
|
+
Author-email: Terry Zhu <596480606@qq.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Requires-Python: >=3.8
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
Requires-Dist: requests>=2.28.0
|
|
10
|
+
|
|
11
|
+
# your-toolkit
|
|
12
|
+
|
|
13
|
+
个人 Python 工具包
|
|
14
|
+
|
|
15
|
+
## 安装
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pip install TerryUtils
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "terryutils" # pip install 用这个
|
|
7
|
+
version = "1.0.3"
|
|
8
|
+
description = "My personal python toolkit"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
license = { text = "MIT" }
|
|
12
|
+
authors = [
|
|
13
|
+
{ name="Terry Zhu", email="596480606@qq.com" }
|
|
14
|
+
]
|
|
15
|
+
|
|
16
|
+
dependencies = [
|
|
17
|
+
"requests>=2.28.0"
|
|
18
|
+
]
|
|
19
|
+
|
|
20
|
+
[tool.setuptools.packages.find]
|
|
21
|
+
where = ["."]
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import logging
|
|
2
|
+
import os
|
|
3
|
+
from logging.handlers import TimedRotatingFileHandler
|
|
4
|
+
|
|
5
|
+
# 配置日志
|
|
6
|
+
def setup_logger(app_name="MyApp", log_dir="./logs"):
|
|
7
|
+
os.makedirs(log_dir, exist_ok=True)
|
|
8
|
+
log_filename = os.path.join(log_dir, f'{app_name}.log') # 固定名字,滚动文件自动加日期
|
|
9
|
+
logger = logging.getLogger(app_name)
|
|
10
|
+
logger.setLevel(logging.DEBUG)
|
|
11
|
+
|
|
12
|
+
# 每天生成一个新文件,保留30天
|
|
13
|
+
file_handler = TimedRotatingFileHandler(
|
|
14
|
+
log_filename, when='midnight', interval=1, backupCount=30, encoding='utf-8'
|
|
15
|
+
)
|
|
16
|
+
file_handler.setLevel(logging.DEBUG)
|
|
17
|
+
|
|
18
|
+
console_handler = logging.StreamHandler()
|
|
19
|
+
console_handler.setLevel(logging.INFO)
|
|
20
|
+
|
|
21
|
+
formatter = logging.Formatter(
|
|
22
|
+
'%(asctime)s - %(name)s - %(levelname)s - %(message)s',
|
|
23
|
+
datefmt='%Y-%m-%d %H:%M:%S'
|
|
24
|
+
)
|
|
25
|
+
file_handler.setFormatter(formatter)
|
|
26
|
+
console_handler.setFormatter(formatter)
|
|
27
|
+
|
|
28
|
+
logger.handlers = []
|
|
29
|
+
logger.addHandler(file_handler)
|
|
30
|
+
logger.addHandler(console_handler)
|
|
31
|
+
|
|
32
|
+
return logger
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
import pymysql
|
|
2
|
+
import traceback
|
|
3
|
+
|
|
4
|
+
import pymysql
|
|
5
|
+
from pymysql.cursors import DictCursor
|
|
6
|
+
|
|
7
|
+
class MysqlUtil:
|
|
8
|
+
def __init__(self, host, user, password, database, port=3306, dict_result=False, debug=True):
|
|
9
|
+
"""
|
|
10
|
+
初始化数据库连接
|
|
11
|
+
:param dict_result: 是否返回 dict
|
|
12
|
+
:param debug: 是否打印 SQL 调试信息
|
|
13
|
+
"""
|
|
14
|
+
self.conn_params = {
|
|
15
|
+
"host": host,
|
|
16
|
+
"user": user,
|
|
17
|
+
"password": password,
|
|
18
|
+
"database": database,
|
|
19
|
+
"port": port,
|
|
20
|
+
"charset": "utf8mb4",
|
|
21
|
+
"autocommit": False, # 手动事务管理
|
|
22
|
+
}
|
|
23
|
+
if dict_result:
|
|
24
|
+
self.conn_params["cursorclass"] = DictCursor
|
|
25
|
+
|
|
26
|
+
self.conn = None
|
|
27
|
+
self.debug = debug
|
|
28
|
+
self._connect()
|
|
29
|
+
|
|
30
|
+
def _connect(self):
|
|
31
|
+
"""建立连接"""
|
|
32
|
+
self.conn = pymysql.connect(**self.conn_params)
|
|
33
|
+
|
|
34
|
+
def _ensure_connection(self):
|
|
35
|
+
"""确保连接可用,失效时自动重连"""
|
|
36
|
+
try:
|
|
37
|
+
self.conn.ping(reconnect=True)
|
|
38
|
+
except Exception:
|
|
39
|
+
self._connect()
|
|
40
|
+
|
|
41
|
+
def _execute(self, func, sql=None, params=None):
|
|
42
|
+
"""
|
|
43
|
+
通用执行器,保证 commit/rollback
|
|
44
|
+
"""
|
|
45
|
+
self._ensure_connection()
|
|
46
|
+
try:
|
|
47
|
+
if self.debug and sql:
|
|
48
|
+
# print(f"[DEBUG SQL] {sql} | params={params}")
|
|
49
|
+
pass
|
|
50
|
+
result = func()
|
|
51
|
+
self.conn.commit()
|
|
52
|
+
return result
|
|
53
|
+
except Exception:
|
|
54
|
+
traceback.print_exc()
|
|
55
|
+
self.conn.rollback()
|
|
56
|
+
raise
|
|
57
|
+
|
|
58
|
+
def execute(self, sql, params=None):
|
|
59
|
+
"""执行单条增删改"""
|
|
60
|
+
def run():
|
|
61
|
+
with self.conn.cursor() as cursor:
|
|
62
|
+
return cursor.execute(sql, params or ())
|
|
63
|
+
return self._execute(run, sql, params)
|
|
64
|
+
|
|
65
|
+
def execute_by_sqls(self, sqls):
|
|
66
|
+
"""执行多条 SQL"""
|
|
67
|
+
def run():
|
|
68
|
+
with self.conn.cursor() as cursor:
|
|
69
|
+
for sql in sqls:
|
|
70
|
+
if self.debug:
|
|
71
|
+
print(f"[DEBUG SQL] {sql}")
|
|
72
|
+
cursor.execute(sql)
|
|
73
|
+
return self._execute(run)
|
|
74
|
+
|
|
75
|
+
def executemany(self, sql, param_list):
|
|
76
|
+
"""批量执行"""
|
|
77
|
+
def run():
|
|
78
|
+
with self.conn.cursor() as cursor:
|
|
79
|
+
return cursor.executemany(sql, param_list)
|
|
80
|
+
return self._execute(run, sql, param_list)
|
|
81
|
+
|
|
82
|
+
def fetchone(self, sql, params=None):
|
|
83
|
+
"""查询一条"""
|
|
84
|
+
def run():
|
|
85
|
+
with self.conn.cursor() as cursor:
|
|
86
|
+
cursor.execute(sql, params or ())
|
|
87
|
+
return cursor.fetchone()
|
|
88
|
+
return self._execute(run, sql, params)
|
|
89
|
+
|
|
90
|
+
def fetchall(self, sql, params=None):
|
|
91
|
+
"""查询多条"""
|
|
92
|
+
def run():
|
|
93
|
+
with self.conn.cursor() as cursor:
|
|
94
|
+
cursor.execute(sql, params or ())
|
|
95
|
+
return cursor.fetchall()
|
|
96
|
+
return self._execute(run, sql, params)
|
|
97
|
+
|
|
98
|
+
def close(self):
|
|
99
|
+
"""关闭连接"""
|
|
100
|
+
if self.conn:
|
|
101
|
+
self.conn.close()
|
|
102
|
+
|
|
103
|
+
def __enter__(self):
|
|
104
|
+
return self
|
|
105
|
+
|
|
106
|
+
def __exit__(self, exc_type, exc_val, exc_tb):
|
|
107
|
+
self.close()
|
|
108
|
+
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
Metadata-Version: 2.1
|
|
2
|
+
Name: terryutils
|
|
3
|
+
Version: 1.0.3
|
|
4
|
+
Summary: My personal python toolkit
|
|
5
|
+
Author-email: Terry Zhu <596480606@qq.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Requires-Python: >=3.8
|
|
8
|
+
Description-Content-Type: text/markdown
|
|
9
|
+
Requires-Dist: requests>=2.28.0
|
|
10
|
+
|
|
11
|
+
# your-toolkit
|
|
12
|
+
|
|
13
|
+
个人 Python 工具包
|
|
14
|
+
|
|
15
|
+
## 安装
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
pip install TerryUtils
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
README.md
|
|
2
|
+
pyproject.toml
|
|
3
|
+
terryutils/__init__.py
|
|
4
|
+
terryutils/log.py
|
|
5
|
+
terryutils/mysql_util.py
|
|
6
|
+
terryutils.egg-info/PKG-INFO
|
|
7
|
+
terryutils.egg-info/SOURCES.txt
|
|
8
|
+
terryutils.egg-info/dependency_links.txt
|
|
9
|
+
terryutils.egg-info/requires.txt
|
|
10
|
+
terryutils.egg-info/top_level.txt
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
requests>=2.28.0
|