ezKit 1.12.6__py3-none-any.whl → 1.12.7__py3-none-any.whl
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.
- ezKit/database.py +47 -0
- {ezkit-1.12.6.dist-info → ezkit-1.12.7.dist-info}/METADATA +1 -1
- {ezkit-1.12.6.dist-info → ezkit-1.12.7.dist-info}/RECORD +6 -6
- {ezkit-1.12.6.dist-info → ezkit-1.12.7.dist-info}/WHEEL +0 -0
- {ezkit-1.12.6.dist-info → ezkit-1.12.7.dist-info}/licenses/LICENSE +0 -0
- {ezkit-1.12.6.dist-info → ezkit-1.12.7.dist-info}/top_level.txt +0 -0
ezKit/database.py
CHANGED
@@ -6,8 +6,10 @@
|
|
6
6
|
# https://docs.sqlalchemy.org/en/20/core/connections.html#sqlalchemy.engine.CursorResult
|
7
7
|
# PostgreSQL 14 Data Types
|
8
8
|
# https://www.postgresql.org/docs/14/datatype.html
|
9
|
+
|
9
10
|
import csv
|
10
11
|
import json
|
12
|
+
from datetime import date, datetime
|
11
13
|
from typing import Any, Type
|
12
14
|
|
13
15
|
import pandas as pd
|
@@ -20,6 +22,51 @@ from . import utils
|
|
20
22
|
Base = declarative_base()
|
21
23
|
|
22
24
|
|
25
|
+
def orm_object_to_dict(obj, include: list | None = None) -> dict | None:
|
26
|
+
"""
|
27
|
+
将 ORM 对象转为 dict, 可选择只包含部分字段.
|
28
|
+
:param obj: SQLAlchemy ORM 实例
|
29
|
+
:param include: 要保留的字段列表(白名单)
|
30
|
+
"""
|
31
|
+
if obj is None:
|
32
|
+
return None
|
33
|
+
|
34
|
+
data = {}
|
35
|
+
|
36
|
+
for column in obj.__table__.columns:
|
37
|
+
key = column.name
|
38
|
+
if include and key not in include:
|
39
|
+
continue
|
40
|
+
value = getattr(obj, key)
|
41
|
+
if isinstance(value, (datetime, date)):
|
42
|
+
# data[key] = value.isoformat()
|
43
|
+
data[key] = value.strftime("%Y-%m-%d %H:%M:%S")
|
44
|
+
else:
|
45
|
+
data[key] = value
|
46
|
+
|
47
|
+
return data
|
48
|
+
|
49
|
+
|
50
|
+
def orm_list_to_dict(obj_list, include: list | None = None) -> list:
|
51
|
+
return [orm_object_to_dict(obj, include) for obj in obj_list]
|
52
|
+
|
53
|
+
|
54
|
+
def orm_to_dict(obj, include: list | None = None) -> dict | list | None:
|
55
|
+
"""
|
56
|
+
ORM 对象 (单个或列表) 转 JSON 字符串.
|
57
|
+
:param include: 仅导出指定字段(白名单)
|
58
|
+
"""
|
59
|
+
try:
|
60
|
+
if isinstance(obj, list):
|
61
|
+
data = orm_list_to_dict(obj, include)
|
62
|
+
else:
|
63
|
+
data = orm_object_to_dict(obj, include)
|
64
|
+
return data
|
65
|
+
except Exception as e:
|
66
|
+
logger.exception(e)
|
67
|
+
return None
|
68
|
+
|
69
|
+
|
23
70
|
class Database:
|
24
71
|
"""Database"""
|
25
72
|
|
@@ -3,7 +3,7 @@ ezKit/_file.py,sha256=0qRZhwYuagTgTGrhm-tzAMvEQT4HTJA_xZKqF2bo0ho,1207
|
|
3
3
|
ezKit/bottle.py,sha256=43h4v1kzz6qrLvCt5IMN0H-gFtaT0koG9wETqteXsps,181666
|
4
4
|
ezKit/bottle_extensions.py,sha256=27rogmfK7mL2qUSjXH79IMGZbCVULtYEikql_N9O6Zs,1165
|
5
5
|
ezKit/cipher.py,sha256=7jBarRH7ukSYzkz-Anl8B8JzluhnRz4CLHidPRRj_cg,2939
|
6
|
-
ezKit/database.py,sha256=
|
6
|
+
ezKit/database.py,sha256=1_RmCwLA6kzigcMcewkoOYStkhFd_w-Qn84vGy-tInI,15553
|
7
7
|
ezKit/dockerhub.py,sha256=j-wQO-71BsOgExHZhYynuy2k_hCX3on-vg0TH7QCit4,1996
|
8
8
|
ezKit/http.py,sha256=zhNxJF-x91UqGncXWxVXnhZVpFo_wmmpGnMXVT11y9E,1832
|
9
9
|
ezKit/markdown_to_html.template,sha256=21G2sSVGJn6aJvHd0NN4zY5YiDteKe4UtW36AzBwSdk,22274
|
@@ -15,8 +15,8 @@ ezKit/token.py,sha256=Ac-i9xfq4TqpGyfCzakjrh4NYzxHiN2sCQrMk1tzVi8,1716
|
|
15
15
|
ezKit/utils.py,sha256=VyAw2yEs15VER9RCpMgskx1l0zrSpRlV34X___eOia0,42416
|
16
16
|
ezKit/xftp.py,sha256=-XQXyhMqeigT63P6sXkSS7r4GROXyqqlkzKxITLWG-g,8278
|
17
17
|
ezKit/zabbix.py,sha256=PkMnfu7mcuotwwIIsHaC9FsNg-gap6hD1xvm0AwSL1Y,33777
|
18
|
-
ezkit-1.12.
|
19
|
-
ezkit-1.12.
|
20
|
-
ezkit-1.12.
|
21
|
-
ezkit-1.12.
|
22
|
-
ezkit-1.12.
|
18
|
+
ezkit-1.12.7.dist-info/licenses/LICENSE,sha256=OXLcl0T2SZ8Pmy2_dmlvKuetivmyPd5m1q-Gyd-zaYY,35149
|
19
|
+
ezkit-1.12.7.dist-info/METADATA,sha256=1uzq8FpjYS1rIP8iewgixWFGzahSaQe32exFrp2xYyc,316
|
20
|
+
ezkit-1.12.7.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
|
21
|
+
ezkit-1.12.7.dist-info/top_level.txt,sha256=aYLB_1WODsqNTsTFWcKP-BN0KCTKcV-HZJ4zlHkCFw8,6
|
22
|
+
ezkit-1.12.7.dist-info/RECORD,,
|
File without changes
|
File without changes
|
File without changes
|