re-common 2.0.1__py3-none-any.whl → 10.0.0__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.
- re_common/baselibrary/__init__.py +4 -0
- re_common/baselibrary/baseabs/__init__.py +7 -0
- re_common/baselibrary/baseabs/baseabs.py +26 -0
- re_common/baselibrary/database/__init__.py +0 -0
- re_common/baselibrary/database/mbuilder.py +132 -0
- re_common/baselibrary/database/moudle.py +93 -0
- re_common/baselibrary/database/msqlite3.py +194 -0
- re_common/baselibrary/database/mysql.py +169 -0
- re_common/baselibrary/database/sql_factory.py +26 -0
- re_common/baselibrary/mthread/MThreadingRun.py +486 -0
- re_common/baselibrary/mthread/MThreadingRunEvent.py +349 -0
- re_common/baselibrary/mthread/__init__.py +3 -0
- re_common/baselibrary/mthread/mythreading.py +695 -0
- re_common/baselibrary/pakge_other/__init__.py +0 -0
- re_common/baselibrary/pakge_other/socks.py +404 -0
- re_common/baselibrary/readconfig/__init__.py +0 -0
- re_common/baselibrary/readconfig/config_factory.py +18 -0
- re_common/baselibrary/readconfig/ini_config.py +317 -0
- re_common/baselibrary/readconfig/toml_config.py +49 -0
- re_common/baselibrary/temporary/__init__.py +0 -0
- re_common/baselibrary/temporary/envdata.py +36 -0
- re_common/baselibrary/tools/__init__.py +0 -0
- re_common/baselibrary/tools/all_requests/__init__.py +0 -0
- re_common/baselibrary/tools/all_requests/aiohttp_request.py +118 -0
- re_common/baselibrary/tools/all_requests/httpx_requet.py +102 -0
- re_common/baselibrary/tools/all_requests/mrequest.py +412 -0
- re_common/baselibrary/tools/all_requests/requests_request.py +81 -0
- re_common/baselibrary/tools/batch_compre/__init__.py +0 -0
- re_common/baselibrary/tools/batch_compre/bijiao_batch.py +31 -0
- re_common/baselibrary/tools/contrast_db3.py +123 -0
- re_common/baselibrary/tools/copy_file.py +39 -0
- re_common/baselibrary/tools/db3_2_sizedb3.py +102 -0
- re_common/baselibrary/tools/foreachgz.py +40 -0
- re_common/baselibrary/tools/get_attr.py +11 -0
- re_common/baselibrary/tools/image_to_pdf.py +62 -0
- re_common/baselibrary/tools/java_code_deal.py +139 -0
- re_common/baselibrary/tools/javacode.py +79 -0
- re_common/baselibrary/tools/mdb_db3.py +48 -0
- re_common/baselibrary/tools/merge_file.py +171 -0
- re_common/baselibrary/tools/merge_gz_file.py +165 -0
- re_common/baselibrary/tools/mhdfstools/__init__.py +0 -0
- re_common/baselibrary/tools/mhdfstools/down_hdfs_files.py +42 -0
- re_common/baselibrary/tools/mhdfstools/hdfst.py +42 -0
- re_common/baselibrary/tools/mhdfstools/up_hdfs_files.py +38 -0
- re_common/baselibrary/tools/mongo_tools.py +50 -0
- re_common/baselibrary/tools/move_file.py +170 -0
- re_common/baselibrary/tools/move_mongo/__init__.py +0 -0
- re_common/baselibrary/tools/move_mongo/mongo_table_to_file.py +63 -0
- re_common/baselibrary/tools/move_mongo/move_mongo_table.py +354 -0
- re_common/baselibrary/tools/move_mongo/use_mttf.py +18 -0
- re_common/baselibrary/tools/move_mongo/use_mv.py +93 -0
- re_common/baselibrary/tools/mpandas/__init__.py +0 -0
- re_common/baselibrary/tools/mpandas/mpandasreadexcel.py +125 -0
- re_common/baselibrary/tools/mpandas/pandas_visualization.py +8 -0
- re_common/baselibrary/tools/myparsel.py +104 -0
- re_common/baselibrary/tools/rename_dir_file.py +37 -0
- re_common/baselibrary/tools/sequoiadb_utils.py +398 -0
- re_common/baselibrary/tools/split_line_to_many.py +25 -0
- re_common/baselibrary/tools/stringtodicts.py +33 -0
- re_common/baselibrary/tools/workwechant_bot.py +84 -0
- re_common/baselibrary/utils/__init__.py +0 -0
- re_common/baselibrary/utils/baseaiohttp.py +296 -0
- re_common/baselibrary/utils/baseaiomysql.py +87 -0
- re_common/baselibrary/utils/baseallstep.py +191 -0
- re_common/baselibrary/utils/baseavro.py +19 -0
- re_common/baselibrary/utils/baseboto3.py +291 -0
- re_common/baselibrary/utils/basecsv.py +32 -0
- re_common/baselibrary/utils/basedict.py +133 -0
- re_common/baselibrary/utils/basedir.py +241 -0
- re_common/baselibrary/utils/baseencode.py +351 -0
- re_common/baselibrary/utils/baseencoding.py +29 -0
- re_common/baselibrary/utils/baseesdsl.py +86 -0
- re_common/baselibrary/utils/baseexcel.py +264 -0
- re_common/baselibrary/utils/baseexcept.py +109 -0
- re_common/baselibrary/utils/basefile.py +654 -0
- re_common/baselibrary/utils/baseftp.py +214 -0
- re_common/baselibrary/utils/basegzip.py +60 -0
- re_common/baselibrary/utils/basehdfs.py +135 -0
- re_common/baselibrary/utils/basehttpx.py +268 -0
- re_common/baselibrary/utils/baseip.py +87 -0
- re_common/baselibrary/utils/basejson.py +2 -0
- re_common/baselibrary/utils/baselist.py +32 -0
- re_common/baselibrary/utils/basemotor.py +190 -0
- re_common/baselibrary/utils/basemssql.py +98 -0
- re_common/baselibrary/utils/baseodbc.py +113 -0
- re_common/baselibrary/utils/basepandas.py +302 -0
- re_common/baselibrary/utils/basepeewee.py +11 -0
- re_common/baselibrary/utils/basepika.py +180 -0
- re_common/baselibrary/utils/basepydash.py +143 -0
- re_common/baselibrary/utils/basepymongo.py +230 -0
- re_common/baselibrary/utils/basequeue.py +22 -0
- re_common/baselibrary/utils/baserar.py +57 -0
- re_common/baselibrary/utils/baserequest.py +279 -0
- re_common/baselibrary/utils/baseset.py +8 -0
- re_common/baselibrary/utils/basesmb.py +403 -0
- re_common/baselibrary/utils/basestring.py +382 -0
- re_common/baselibrary/utils/basetime.py +320 -0
- re_common/baselibrary/utils/basetuple.py +0 -0
- re_common/baselibrary/utils/baseurl.py +121 -0
- re_common/baselibrary/utils/basezip.py +57 -0
- re_common/baselibrary/utils/core/__init__.py +8 -0
- re_common/baselibrary/utils/core/bottomutils.py +18 -0
- re_common/baselibrary/utils/core/mdeprecated.py +327 -0
- re_common/baselibrary/utils/core/mlamada.py +16 -0
- re_common/baselibrary/utils/core/msginfo.py +25 -0
- re_common/baselibrary/utils/core/requests_core.py +103 -0
- re_common/baselibrary/utils/fateadm.py +429 -0
- re_common/baselibrary/utils/importfun.py +123 -0
- re_common/baselibrary/utils/mfaker.py +57 -0
- re_common/baselibrary/utils/my_abc/__init__.py +3 -0
- re_common/baselibrary/utils/my_abc/better_abc.py +32 -0
- re_common/baselibrary/utils/mylogger.py +414 -0
- re_common/baselibrary/utils/myredisclient.py +861 -0
- re_common/baselibrary/utils/pipupgrade.py +21 -0
- re_common/baselibrary/utils/ringlist.py +85 -0
- re_common/baselibrary/utils/version_compare.py +36 -0
- re_common/baselibrary/utils/ydmhttp.py +126 -0
- re_common/facade/__init__.py +1 -0
- re_common/facade/lazy_import.py +11 -0
- re_common/facade/loggerfacade.py +25 -0
- re_common/facade/mysqlfacade.py +467 -0
- re_common/facade/now.py +31 -0
- re_common/facade/sqlite3facade.py +257 -0
- re_common/facade/use/__init__.py +0 -0
- re_common/facade/use/mq_use_facade.py +83 -0
- re_common/facade/use/proxy_use_facade.py +20 -0
- re_common/libtest/__init__.py +0 -0
- re_common/libtest/base_dict_test.py +19 -0
- re_common/libtest/baseavro_test.py +13 -0
- re_common/libtest/basefile_test.py +14 -0
- re_common/libtest/basemssql_test.py +77 -0
- re_common/libtest/baseodbc_test.py +8 -0
- re_common/libtest/basepandas_test.py +38 -0
- re_common/libtest/get_attr_test/__init__.py +0 -0
- re_common/libtest/get_attr_test/get_attr_test_settings.py +14 -0
- re_common/libtest/get_attr_test/settings.py +55 -0
- re_common/libtest/idencode_test.py +54 -0
- re_common/libtest/iniconfig_test.py +35 -0
- re_common/libtest/ip_test.py +35 -0
- re_common/libtest/merge_file_test.py +20 -0
- re_common/libtest/mfaker_test.py +9 -0
- re_common/libtest/mm3_test.py +32 -0
- re_common/libtest/mylogger_test.py +89 -0
- re_common/libtest/myparsel_test.py +28 -0
- re_common/libtest/mysql_test.py +151 -0
- re_common/libtest/pymongo_test.py +21 -0
- re_common/libtest/split_test.py +12 -0
- re_common/libtest/sqlite3_merge_test.py +6 -0
- re_common/libtest/sqlite3_test.py +34 -0
- re_common/libtest/tomlconfig_test.py +30 -0
- re_common/libtest/use_tools_test/__init__.py +3 -0
- re_common/libtest/user/__init__.py +5 -0
- re_common/studio/__init__.py +5 -0
- re_common/studio/assignment_expressions.py +37 -0
- re_common/studio/mydash/__init__.py +0 -0
- re_common/studio/mydash/test1.py +19 -0
- re_common/studio/pydashstudio/__init__.py +0 -0
- re_common/studio/pydashstudio/first.py +9 -0
- re_common/studio/streamlitstudio/__init__.py +0 -0
- re_common/studio/streamlitstudio/first_app.py +66 -0
- re_common/studio/streamlitstudio/uber_pickups.py +24 -0
- re_common/studio/test.py +19 -0
- re_common/vip/__init__.py +0 -0
- re_common/vip/base_step_process.py +11 -0
- re_common/vip/baseencodeid.py +91 -0
- re_common/vip/changetaskname.py +28 -0
- re_common/vip/core_var.py +24 -0
- re_common/vip/mmh3Hash.py +90 -0
- re_common/vip/proxy/__init__.py +0 -0
- re_common/vip/proxy/allproxys.py +127 -0
- re_common/vip/proxy/allproxys_thread.py +159 -0
- re_common/vip/proxy/cnki_proxy.py +153 -0
- re_common/vip/proxy/kuaidaili.py +87 -0
- re_common/vip/proxy/proxy_all.py +113 -0
- re_common/vip/proxy/update_kuaidaili_0.py +42 -0
- re_common/vip/proxy/wanfang_proxy.py +152 -0
- re_common/vip/proxy/wp_proxy_all.py +182 -0
- re_common/vip/read_rawid_to_txt.py +92 -0
- re_common/vip/title/__init__.py +5 -0
- re_common/vip/title/transform/TransformBookTitleToZt.py +125 -0
- re_common/vip/title/transform/TransformConferenceTitleToZt.py +139 -0
- re_common/vip/title/transform/TransformCstadTitleToZt.py +196 -0
- re_common/vip/title/transform/TransformJournalTitleToZt.py +203 -0
- re_common/vip/title/transform/TransformPatentTitleToZt.py +132 -0
- re_common/vip/title/transform/TransformRegulationTitleToZt.py +114 -0
- re_common/vip/title/transform/TransformStandardTitleToZt.py +135 -0
- re_common/vip/title/transform/TransformThesisTitleToZt.py +135 -0
- re_common/vip/title/transform/__init__.py +11 -0
- {re_common-2.0.1.dist-info → re_common-10.0.0.dist-info}/METADATA +1 -1
- re_common-10.0.0.dist-info/RECORD +213 -0
- re_common-2.0.1.dist-info/RECORD +0 -25
- {re_common-2.0.1.dist-info → re_common-10.0.0.dist-info}/LICENSE +0 -0
- {re_common-2.0.1.dist-info → re_common-10.0.0.dist-info}/WHEEL +0 -0
- {re_common-2.0.1.dist-info → re_common-10.0.0.dist-info}/top_level.txt +0 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
class BaseAbs(object):
|
|
4
|
+
"""
|
|
5
|
+
抽象工厂模式,方便管理和引用 目前主要将配置文件和sql相关的库引入了抽象工厂模式中
|
|
6
|
+
"""
|
|
7
|
+
@staticmethod
|
|
8
|
+
def get_sql_factory():
|
|
9
|
+
"""
|
|
10
|
+
sql相关的工厂
|
|
11
|
+
:return:
|
|
12
|
+
"""
|
|
13
|
+
from re_common.baselibrary.database.sql_factory import SqlFactory
|
|
14
|
+
return SqlFactory()
|
|
15
|
+
|
|
16
|
+
@staticmethod
|
|
17
|
+
def get_config_factory():
|
|
18
|
+
"""
|
|
19
|
+
配置文件相关的工厂
|
|
20
|
+
:return:
|
|
21
|
+
"""
|
|
22
|
+
from re_common.baselibrary.readconfig.config_factory import ConfigFactory
|
|
23
|
+
return ConfigFactory()
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
|
|
File without changes
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
from abc import ABCMeta, abstractmethod
|
|
2
|
+
|
|
3
|
+
from re_common.baselibrary.database.moudle import SqlMoudle, Sqlite3Moudle
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class MysqlBuilderAbstract:
|
|
7
|
+
__metaclass__ = ABCMeta
|
|
8
|
+
|
|
9
|
+
@abstractmethod
|
|
10
|
+
def build_server_address(self, host):
|
|
11
|
+
pass
|
|
12
|
+
|
|
13
|
+
@abstractmethod
|
|
14
|
+
def build_username(self, user):
|
|
15
|
+
pass
|
|
16
|
+
|
|
17
|
+
@abstractmethod
|
|
18
|
+
def build_password(self, passwd):
|
|
19
|
+
pass
|
|
20
|
+
|
|
21
|
+
@abstractmethod
|
|
22
|
+
def build_port(self, port):
|
|
23
|
+
pass
|
|
24
|
+
|
|
25
|
+
@abstractmethod
|
|
26
|
+
def use_db(self, db):
|
|
27
|
+
pass
|
|
28
|
+
|
|
29
|
+
@abstractmethod
|
|
30
|
+
def build_chart(self, chartset):
|
|
31
|
+
pass
|
|
32
|
+
|
|
33
|
+
@abstractmethod
|
|
34
|
+
def get_moudle(self):
|
|
35
|
+
pass
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class Sqlite3BuilderAbstract:
|
|
39
|
+
__metaclass__ = ABCMeta
|
|
40
|
+
|
|
41
|
+
@abstractmethod
|
|
42
|
+
def build_file_path(self, dbpath):
|
|
43
|
+
pass
|
|
44
|
+
|
|
45
|
+
@abstractmethod
|
|
46
|
+
def build_isolation_level(self):
|
|
47
|
+
pass
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class MysqlBuilder(MysqlBuilderAbstract):
|
|
51
|
+
def __init__(self, configfile, configname, keytransformdicts=None):
|
|
52
|
+
self.sqlmoudle = SqlMoudle()
|
|
53
|
+
|
|
54
|
+
from re_common.baselibrary.readconfig.ini_config import IniConfig
|
|
55
|
+
self.ic = IniConfig(configfile).builder()
|
|
56
|
+
self.configfile = configfile
|
|
57
|
+
self.configname = configname
|
|
58
|
+
# 使用key转换器转换key
|
|
59
|
+
self.keytransformdicts = keytransformdicts
|
|
60
|
+
"""
|
|
61
|
+
python2
|
|
62
|
+
self.config.readfp(open(configfile, 'rb'))
|
|
63
|
+
python3
|
|
64
|
+
self.config.read_file(open(configfile, 'r'))
|
|
65
|
+
"""
|
|
66
|
+
|
|
67
|
+
def build_port(self, port):
|
|
68
|
+
self.sqlmoudle.port = int(self.ic.get_value(self.configname, port))
|
|
69
|
+
|
|
70
|
+
def build_server_address(self, host):
|
|
71
|
+
self.sqlmoudle.host = self.ic.get_value(self.configname, host)
|
|
72
|
+
|
|
73
|
+
def build_password(self, passwd):
|
|
74
|
+
self.sqlmoudle.passwd = self.ic.get_value(self.configname, passwd)
|
|
75
|
+
|
|
76
|
+
def use_db(self, db):
|
|
77
|
+
self.sqlmoudle.db = self.ic.get_value(self.configname, db)
|
|
78
|
+
|
|
79
|
+
def build_username(self, user):
|
|
80
|
+
self.sqlmoudle.user = self.ic.get_value(self.configname, user)
|
|
81
|
+
|
|
82
|
+
def build_chart(self, chartset):
|
|
83
|
+
self.sqlmoudle.charset = self.ic.get_value(self.configname, chartset)
|
|
84
|
+
|
|
85
|
+
def get_moudle(self):
|
|
86
|
+
return self.sqlmoudle
|
|
87
|
+
|
|
88
|
+
def build_all(self):
|
|
89
|
+
if not self.keytransformdicts:
|
|
90
|
+
self.keytransformdicts = {"port": "port", "host": "host", "passwd": "passwd", "user": "user", "db": "db",
|
|
91
|
+
"chartset": "chartset"}
|
|
92
|
+
self.build_port(self.keytransformdicts["port"])
|
|
93
|
+
self.build_server_address(self.keytransformdicts["host"])
|
|
94
|
+
self.build_password(self.keytransformdicts["passwd"])
|
|
95
|
+
self.build_username(self.keytransformdicts["user"])
|
|
96
|
+
self.use_db(self.keytransformdicts["db"])
|
|
97
|
+
self.build_chart(self.keytransformdicts["chartset"])
|
|
98
|
+
return self
|
|
99
|
+
|
|
100
|
+
def get_tuples(self):
|
|
101
|
+
return self.sqlmoudle.host, self.sqlmoudle.user, self.sqlmoudle.passwd, self.sqlmoudle.port
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
class Sqlite3Builder(Sqlite3BuilderAbstract):
|
|
105
|
+
def __init__(self, configfile="", sec=""):
|
|
106
|
+
self.sqlite3moudle = Sqlite3Moudle()
|
|
107
|
+
from re_common.baselibrary.readconfig.ini_config import IniConfig
|
|
108
|
+
self.ic = IniConfig(configfile).builder()
|
|
109
|
+
self.configfile = configfile
|
|
110
|
+
self.sec = sec
|
|
111
|
+
|
|
112
|
+
def build_file_path(self, dbpath='', opt="dbpath"):
|
|
113
|
+
if dbpath:
|
|
114
|
+
self.sqlite3moudle.database = dbpath
|
|
115
|
+
else:
|
|
116
|
+
self.sqlite3moudle.database = self.ic.get_value(self.sec, opt)
|
|
117
|
+
return self
|
|
118
|
+
|
|
119
|
+
def build_timeout(self, timeout):
|
|
120
|
+
self.sqlite3moudle.timeout = timeout
|
|
121
|
+
return self
|
|
122
|
+
|
|
123
|
+
def build_isolation_level(self):
|
|
124
|
+
pass
|
|
125
|
+
|
|
126
|
+
def build_all(self):
|
|
127
|
+
self.build_file_path()
|
|
128
|
+
self.build_isolation_level()
|
|
129
|
+
return self
|
|
130
|
+
|
|
131
|
+
def get_moudle(self):
|
|
132
|
+
return self.sqlite3moudle
|
|
@@ -0,0 +1,93 @@
|
|
|
1
|
+
#!/usr/bin/env python
|
|
2
|
+
# -*- coding:utf-8 -*-
|
|
3
|
+
from pymysql.cursors import Cursor
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
# pymysql的moudle
|
|
7
|
+
class SqlMoudle(object):
|
|
8
|
+
def __init__(self):
|
|
9
|
+
"""
|
|
10
|
+
host
|
|
11
|
+
string, host to connect
|
|
12
|
+
|
|
13
|
+
user
|
|
14
|
+
string, user to connect as
|
|
15
|
+
|
|
16
|
+
passwd
|
|
17
|
+
string, password to use
|
|
18
|
+
|
|
19
|
+
db
|
|
20
|
+
string, database to use
|
|
21
|
+
|
|
22
|
+
port
|
|
23
|
+
integer, TCP/IP port to connect to
|
|
24
|
+
|
|
25
|
+
这是一个标准连接的所有参数
|
|
26
|
+
cursorclass pymysql.cursors.DictCursor 表示返回数据为dicts
|
|
27
|
+
"""
|
|
28
|
+
self.host = None
|
|
29
|
+
self.user = None
|
|
30
|
+
self.password = ""
|
|
31
|
+
self.database = None
|
|
32
|
+
self.port = 3306
|
|
33
|
+
self.unix_socket = None
|
|
34
|
+
self.charset = ''
|
|
35
|
+
self.sql_mode = None
|
|
36
|
+
self.read_default_file = None
|
|
37
|
+
self.conv = None
|
|
38
|
+
self.use_unicode = True
|
|
39
|
+
self.client_flag = 0
|
|
40
|
+
self.cursorclass = Cursor
|
|
41
|
+
self.init_command = None
|
|
42
|
+
self.connect_timeout = 10
|
|
43
|
+
self.ssl = None
|
|
44
|
+
self.read_default_group = None
|
|
45
|
+
self.compress = None
|
|
46
|
+
self.named_pipe = None
|
|
47
|
+
self.autocommit = False
|
|
48
|
+
self.db = None
|
|
49
|
+
self.passwd = None
|
|
50
|
+
self.local_infile = False
|
|
51
|
+
self.max_allowed_packet = 16 * 1024 * 1024
|
|
52
|
+
self.defer_connect = False
|
|
53
|
+
self.auth_plugin_map = None
|
|
54
|
+
self.read_timeout = None
|
|
55
|
+
self.write_timeout = None
|
|
56
|
+
self.bind_address = None
|
|
57
|
+
self.binary_prefix = False
|
|
58
|
+
self.program_name = None
|
|
59
|
+
self.server_public_key = None
|
|
60
|
+
|
|
61
|
+
# @property
|
|
62
|
+
# def get_serveraddress(self):
|
|
63
|
+
# return self._serveraddress
|
|
64
|
+
#
|
|
65
|
+
# @get_serveraddress.setter
|
|
66
|
+
# def set_serveraddress(self,serveraddress):
|
|
67
|
+
# self._serveraddress = serveraddress
|
|
68
|
+
#
|
|
69
|
+
# @get_serveraddress.deleter
|
|
70
|
+
# def del_serveraddress(self):
|
|
71
|
+
# del self._serveraddress
|
|
72
|
+
|
|
73
|
+
def to_dict(self):
|
|
74
|
+
return self.__dict__
|
|
75
|
+
|
|
76
|
+
def __str__(self):
|
|
77
|
+
return str(self.__dict__)
|
|
78
|
+
|
|
79
|
+
|
|
80
|
+
# sqlite3的 moudle
|
|
81
|
+
class Sqlite3Moudle(object):
|
|
82
|
+
def __init__(self):
|
|
83
|
+
self.database = ""
|
|
84
|
+
self.timeout = None
|
|
85
|
+
self.detect_types = None
|
|
86
|
+
self.isolation_level = None
|
|
87
|
+
self.check_same_thread = None
|
|
88
|
+
self.factory = None
|
|
89
|
+
self.cached_statements = None
|
|
90
|
+
# self.uri = None
|
|
91
|
+
|
|
92
|
+
def to_dict(self):
|
|
93
|
+
return self.__dict__
|
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
import sqlite3
|
|
2
|
+
|
|
3
|
+
from re_common.baselibrary.database.moudle import Sqlite3Moudle
|
|
4
|
+
from re_common.baselibrary.database.sql_factory import SqlFactory
|
|
5
|
+
from re_common.baselibrary.utils.basedict import BaseDicts
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def dict_factory(cursor, row):
|
|
9
|
+
"""
|
|
10
|
+
设置返回字典
|
|
11
|
+
:param row:
|
|
12
|
+
:return:
|
|
13
|
+
"""
|
|
14
|
+
d = {}
|
|
15
|
+
for index, col in enumerate(cursor.description):
|
|
16
|
+
d[col[0]] = row[index]
|
|
17
|
+
return d
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class Sqlite3(SqlFactory):
|
|
21
|
+
def __init__(self):
|
|
22
|
+
# 就是conn
|
|
23
|
+
self.db = ''
|
|
24
|
+
self.cursor = ''
|
|
25
|
+
self.sqlite3_moudle = {}
|
|
26
|
+
|
|
27
|
+
def set_result_dict(self):
|
|
28
|
+
def dict_factory(cursor, row):
|
|
29
|
+
d = {}
|
|
30
|
+
for idx, col in enumerate(cursor.description):
|
|
31
|
+
d[col[0]] = row[idx]
|
|
32
|
+
return d
|
|
33
|
+
|
|
34
|
+
self.db.row_factory = dict_factory
|
|
35
|
+
self.get_cursor()
|
|
36
|
+
return self
|
|
37
|
+
|
|
38
|
+
def set_encoding(self, bytes=False, encoding="gbk"):
|
|
39
|
+
"""
|
|
40
|
+
https://blog.csdn.net/xkxjy/article/details/8179479
|
|
41
|
+
在不知道编码格式的情况下 使用bytes 但我没有成功
|
|
42
|
+
:param bytes:
|
|
43
|
+
:param encoding:
|
|
44
|
+
:return:
|
|
45
|
+
"""
|
|
46
|
+
# if bytes:
|
|
47
|
+
# self.db.text_factory = bytes
|
|
48
|
+
# else:
|
|
49
|
+
# self.db.text_factory = lambda x: str(x, encoding, 'ignore')
|
|
50
|
+
self.db.text_factory = lambda x: str(x, encoding, 'ignore')
|
|
51
|
+
|
|
52
|
+
def link(self, sqlite3_moudle: Sqlite3Moudle):
|
|
53
|
+
"""
|
|
54
|
+
连接数据库
|
|
55
|
+
:param sqlite3_moudle:
|
|
56
|
+
:param mysqlmoudle:
|
|
57
|
+
:return:
|
|
58
|
+
"""
|
|
59
|
+
self.sqlite3_moudle = sqlite3_moudle
|
|
60
|
+
# 返回连接对象
|
|
61
|
+
dicts = sqlite3_moudle.to_dict()
|
|
62
|
+
dicts = BaseDicts.removeDictsNone(dicts)
|
|
63
|
+
self.db = sqlite3.connect(**dicts)
|
|
64
|
+
self.cursor = self.db.cursor()
|
|
65
|
+
return self.db, self.cursor
|
|
66
|
+
|
|
67
|
+
def get_cursor(self) -> sqlite3.Cursor:
|
|
68
|
+
"""
|
|
69
|
+
本函数通过链接返回sqlite3的游标
|
|
70
|
+
:param connect: 传入的是sqlite3的连接
|
|
71
|
+
:return: 返回游标
|
|
72
|
+
"""
|
|
73
|
+
assert isinstance(self.db, sqlite3.Connection)
|
|
74
|
+
self.cursor = self.db.cursor()
|
|
75
|
+
return self.cursor
|
|
76
|
+
|
|
77
|
+
def execute(self, sql: str) -> sqlite3.Cursor:
|
|
78
|
+
"""
|
|
79
|
+
通过游标对象执行sql语句并返回结果
|
|
80
|
+
:param cursor: 游标对象
|
|
81
|
+
:param sql: 需要执行的sql语句
|
|
82
|
+
:return: 返回结果的游标对象
|
|
83
|
+
"""
|
|
84
|
+
assert isinstance(self.cursor, sqlite3.Cursor)
|
|
85
|
+
result = self.cursor.execute(sql)
|
|
86
|
+
return result
|
|
87
|
+
|
|
88
|
+
def executemany(self, sql: str, seq_of_parameters) -> sqlite3.Cursor:
|
|
89
|
+
assert isinstance(self.cursor, sqlite3.Cursor)
|
|
90
|
+
result = self.cursor.executemany(sql, seq_of_parameters)
|
|
91
|
+
return result
|
|
92
|
+
|
|
93
|
+
def get_all_field_info(self, tablename):
|
|
94
|
+
"""
|
|
95
|
+
获取字段详细信息
|
|
96
|
+
:param tablename:
|
|
97
|
+
:return:
|
|
98
|
+
"""
|
|
99
|
+
sql = "PRAGMA table_info({})".format(tablename)
|
|
100
|
+
result = self.execute(sql)
|
|
101
|
+
return result.fetchall()
|
|
102
|
+
|
|
103
|
+
def get_all_field(self, tablename):
|
|
104
|
+
"""
|
|
105
|
+
仅仅获取字段
|
|
106
|
+
:param tablename:
|
|
107
|
+
:return:
|
|
108
|
+
"""
|
|
109
|
+
listinfo = self.get_all_field_info(tablename)
|
|
110
|
+
return [row[1] for row in listinfo]
|
|
111
|
+
|
|
112
|
+
def get_table_name(self):
|
|
113
|
+
"""
|
|
114
|
+
获取表名
|
|
115
|
+
:return:
|
|
116
|
+
"""
|
|
117
|
+
sql = "select name from sqlite_master where type='table' order by name;"
|
|
118
|
+
result = self.execute(sql)
|
|
119
|
+
return result.fetchall()
|
|
120
|
+
|
|
121
|
+
def get_rowcount(self, ) -> int:
|
|
122
|
+
"""
|
|
123
|
+
获取sql语句影响的行数
|
|
124
|
+
:param cursor: 游标对象
|
|
125
|
+
:return: 影响的行数
|
|
126
|
+
"""
|
|
127
|
+
assert isinstance(self.cursor, sqlite3.Cursor)
|
|
128
|
+
num = self.cursor.rowcount
|
|
129
|
+
return num
|
|
130
|
+
|
|
131
|
+
def close_cursor(self) -> None:
|
|
132
|
+
"""
|
|
133
|
+
关闭游标
|
|
134
|
+
:param cursor: 游标
|
|
135
|
+
:return: None
|
|
136
|
+
"""
|
|
137
|
+
assert isinstance(self.cursor, sqlite3.Cursor)
|
|
138
|
+
self.cursor.close()
|
|
139
|
+
|
|
140
|
+
def my_commit(self) -> None:
|
|
141
|
+
"""
|
|
142
|
+
提交
|
|
143
|
+
:param connect: 连接
|
|
144
|
+
:return: None
|
|
145
|
+
"""
|
|
146
|
+
assert isinstance(self.db, sqlite3.Connection)
|
|
147
|
+
self.db.commit()
|
|
148
|
+
|
|
149
|
+
def close_connect(self) -> None:
|
|
150
|
+
"""
|
|
151
|
+
链接关闭
|
|
152
|
+
:param connect: 连接
|
|
153
|
+
:return: None
|
|
154
|
+
"""
|
|
155
|
+
assert isinstance(self.db, sqlite3.Connection)
|
|
156
|
+
self.db.close()
|
|
157
|
+
|
|
158
|
+
def __del__(self):
|
|
159
|
+
self.close_connect()
|
|
160
|
+
|
|
161
|
+
@classmethod
|
|
162
|
+
def sqlite3_merge(cls, inpath, attachpath, tablename="modify_title_info_zt"):
|
|
163
|
+
"""
|
|
164
|
+
合并两个db3
|
|
165
|
+
:param inpath:
|
|
166
|
+
:param attachpath:
|
|
167
|
+
:return:
|
|
168
|
+
"""
|
|
169
|
+
conn = sqlite3.connect(inpath)
|
|
170
|
+
conn.text_factory = str
|
|
171
|
+
cur = conn.cursor()
|
|
172
|
+
attach = 'attach database "' + attachpath + '" as w;'
|
|
173
|
+
sql1 = 'insert into {} select * from w.{};'.format(tablename, tablename)
|
|
174
|
+
cur.execute(attach)
|
|
175
|
+
cur.execute(sql1)
|
|
176
|
+
conn.commit()
|
|
177
|
+
cur.close()
|
|
178
|
+
conn.close()
|
|
179
|
+
|
|
180
|
+
def create_table(self, tablename, fields: list):
|
|
181
|
+
if self.cursor == '':
|
|
182
|
+
self.get_cursor()
|
|
183
|
+
sql = "PRAGMA foreign_keys = false;"
|
|
184
|
+
self.execute(sql)
|
|
185
|
+
sql = f'DROP TABLE IF EXISTS "{tablename}";'
|
|
186
|
+
self.execute(sql)
|
|
187
|
+
strings = ""
|
|
188
|
+
for field in fields:
|
|
189
|
+
strings = strings + f'"{field}" TEXT,'
|
|
190
|
+
strings = strings.rstrip(',')
|
|
191
|
+
sql = f'CREATE TABLE "{tablename}"({strings});'
|
|
192
|
+
self.execute(sql)
|
|
193
|
+
sql = 'PRAGMA foreign_keys = true;'
|
|
194
|
+
self.execute(sql)
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import pymysql
|
|
2
|
+
|
|
3
|
+
from re_common.baselibrary.database.moudle import SqlMoudle
|
|
4
|
+
from re_common.baselibrary.database.sql_factory import SqlFactory
|
|
5
|
+
from re_common.baselibrary.utils.version_compare import compare_bool
|
|
6
|
+
|
|
7
|
+
_json_escape_table = [chr(x) for x in range(128)]
|
|
8
|
+
_json_escape_table[0] = "\\0"
|
|
9
|
+
_json_escape_table[ord("\\")] = "\\\\"
|
|
10
|
+
_json_escape_table[ord("\n")] = "\\n"
|
|
11
|
+
_json_escape_table[ord("\r")] = "\\r"
|
|
12
|
+
_json_escape_table[ord("\032")] = "\\Z"
|
|
13
|
+
_json_escape_table[ord('"')] = '\\"'
|
|
14
|
+
_json_escape_table[ord("'")] = "\'"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class Mysql(SqlFactory):
|
|
18
|
+
|
|
19
|
+
@property
|
|
20
|
+
def db(self):
|
|
21
|
+
# 就是conn
|
|
22
|
+
if not self.is_ping():
|
|
23
|
+
assert self._mysqlmoudle, AttributeError("MySQL model不存在 无法重新连接 请设置moudle")
|
|
24
|
+
self.reConnect()
|
|
25
|
+
return self._db # type: pymysql.cursors.Cursor
|
|
26
|
+
|
|
27
|
+
@db.setter
|
|
28
|
+
def db(self, value):
|
|
29
|
+
self._db = value
|
|
30
|
+
|
|
31
|
+
@property
|
|
32
|
+
def cursor(self):
|
|
33
|
+
if self._cursor.connection:
|
|
34
|
+
return self._cursor # type: pymysql.cursors.Cursor
|
|
35
|
+
else:
|
|
36
|
+
self._cursor = self._db.cursor()
|
|
37
|
+
return self._cursor
|
|
38
|
+
|
|
39
|
+
@cursor.setter
|
|
40
|
+
def cursor(self, value):
|
|
41
|
+
self._cursor = value
|
|
42
|
+
|
|
43
|
+
@property
|
|
44
|
+
def mysqlmoudle(self):
|
|
45
|
+
return self._mysqlmoudle # type: SqlMoudle
|
|
46
|
+
|
|
47
|
+
@mysqlmoudle.setter
|
|
48
|
+
def mysqlmoudle(self, moudle):
|
|
49
|
+
self._mysqlmoudle = moudle
|
|
50
|
+
|
|
51
|
+
def get_new_cursor(self):
|
|
52
|
+
"""
|
|
53
|
+
获取一个新的游标
|
|
54
|
+
:return:
|
|
55
|
+
"""
|
|
56
|
+
# 检查db的存在及是否断掉 不知道为什么 不可以加括号 但编辑器会警告
|
|
57
|
+
return self._db.cursor()
|
|
58
|
+
|
|
59
|
+
def link(self, mysqlmoudle: SqlMoudle):
|
|
60
|
+
"""
|
|
61
|
+
连接数据库
|
|
62
|
+
:param mysqlmoudle:
|
|
63
|
+
:return:
|
|
64
|
+
"""
|
|
65
|
+
self._mysqlmoudle = mysqlmoudle
|
|
66
|
+
# 返回连接对象
|
|
67
|
+
self._db = pymysql.connect(**mysqlmoudle.to_dict())
|
|
68
|
+
self._cursor = self._db.cursor()
|
|
69
|
+
|
|
70
|
+
return self
|
|
71
|
+
|
|
72
|
+
def reConnect(self):
|
|
73
|
+
"""
|
|
74
|
+
重新连接数据库
|
|
75
|
+
:return:
|
|
76
|
+
"""
|
|
77
|
+
try:
|
|
78
|
+
self._db.ping()
|
|
79
|
+
self._db.close()
|
|
80
|
+
except:
|
|
81
|
+
self.link(self._mysqlmoudle)
|
|
82
|
+
self.link(self._mysqlmoudle)
|
|
83
|
+
|
|
84
|
+
def is_ping(self):
|
|
85
|
+
if self._db:
|
|
86
|
+
try:
|
|
87
|
+
return self._db.ping()
|
|
88
|
+
except:
|
|
89
|
+
self.link(self._mysqlmoudle)
|
|
90
|
+
else:
|
|
91
|
+
raise AttributeError("数据库连接对象不存在,请调用reConnect重新连接")
|
|
92
|
+
|
|
93
|
+
def commit(self):
|
|
94
|
+
"""
|
|
95
|
+
事务提交
|
|
96
|
+
:return:
|
|
97
|
+
"""
|
|
98
|
+
# 对象判断
|
|
99
|
+
assert isinstance(self._db, pymysql.connections.Connection)
|
|
100
|
+
self._db.commit()
|
|
101
|
+
|
|
102
|
+
def execute(self, sql, args=None):
|
|
103
|
+
"""
|
|
104
|
+
执行sql语句
|
|
105
|
+
:param sql:
|
|
106
|
+
:return:
|
|
107
|
+
"""
|
|
108
|
+
try:
|
|
109
|
+
assert isinstance(self._cursor, pymysql.cursors.Cursor)
|
|
110
|
+
# 游标是否被关闭,执行该函数保证有游标
|
|
111
|
+
self._cursor.execute(sql, args=args)
|
|
112
|
+
self.commit()
|
|
113
|
+
except Exception as e:
|
|
114
|
+
raise e
|
|
115
|
+
|
|
116
|
+
def fetchall(self):
|
|
117
|
+
"""
|
|
118
|
+
select 后调用获取全部返回结果
|
|
119
|
+
:return:
|
|
120
|
+
"""
|
|
121
|
+
result = self._cursor.fetchall()
|
|
122
|
+
return result
|
|
123
|
+
|
|
124
|
+
def close(self):
|
|
125
|
+
if self._db and isinstance(self._db, pymysql.connections.Connection):
|
|
126
|
+
self._db.close()
|
|
127
|
+
|
|
128
|
+
def rollback(self):
|
|
129
|
+
self.db.rollback()
|
|
130
|
+
|
|
131
|
+
@classmethod
|
|
132
|
+
def escape(cls, strings):
|
|
133
|
+
# 这个不能用于多线程,否则会出现多线程抢占连接
|
|
134
|
+
if compare_bool(pymysql.__version__, "1.0.0"):
|
|
135
|
+
from pymysql.converters import escape_string
|
|
136
|
+
return escape_string(strings)
|
|
137
|
+
else:
|
|
138
|
+
from pymysql import escape_string
|
|
139
|
+
return escape_string(strings)
|
|
140
|
+
|
|
141
|
+
def json_escape(self, strings):
|
|
142
|
+
"""
|
|
143
|
+
json化需要
|
|
144
|
+
:param strings:
|
|
145
|
+
:return:
|
|
146
|
+
"""
|
|
147
|
+
return strings.translate(_json_escape_table)
|
|
148
|
+
|
|
149
|
+
def __repr__(self):
|
|
150
|
+
return 'Mysql().link(%s)' % self._mysqlmoudle
|
|
151
|
+
|
|
152
|
+
def __str__(self):
|
|
153
|
+
return "Mysql %s" % self._mysqlmoudle
|
|
154
|
+
|
|
155
|
+
def __del__(self):
|
|
156
|
+
self.close()
|
|
157
|
+
|
|
158
|
+
|
|
159
|
+
def json_update(dicts):
|
|
160
|
+
duplicte_list = []
|
|
161
|
+
for k, v in dicts.items():
|
|
162
|
+
if isinstance(v, str):
|
|
163
|
+
duplicte_list.append(f"'$.{Mysql.escape(k)}','{Mysql.escape(v)}'")
|
|
164
|
+
else:
|
|
165
|
+
duplicte_list.append(f"'$.{Mysql.escape(k)}','{v}'")
|
|
166
|
+
duplicte = ",".join(duplicte_list)
|
|
167
|
+
# 处理 符合 %s 占位符里面的 正常数据非占位符存在 % 的情况
|
|
168
|
+
duplicte_format = duplicte.replace("%", "%%")
|
|
169
|
+
return duplicte_format
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
from re_common.baselibrary.baseabs import BaseAbs
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class SqlFactory(BaseAbs):
|
|
5
|
+
"""
|
|
6
|
+
sql 工厂类
|
|
7
|
+
"""
|
|
8
|
+
@staticmethod
|
|
9
|
+
def mysql_factory(type='mysql'):
|
|
10
|
+
if type == 'mysql':
|
|
11
|
+
"""
|
|
12
|
+
mysql类 返回一个mysql的对象
|
|
13
|
+
"""
|
|
14
|
+
from re_common.baselibrary.database.mysql import Mysql
|
|
15
|
+
return Mysql()
|
|
16
|
+
assert 0, "err sql type please check: %s" % type
|
|
17
|
+
|
|
18
|
+
@staticmethod
|
|
19
|
+
def sqlite_factory(type='sqlite3'):
|
|
20
|
+
if type == 'sqlite3':
|
|
21
|
+
"""
|
|
22
|
+
sqlite3类 返回一个sqlite3的对象
|
|
23
|
+
"""
|
|
24
|
+
from re_common.baselibrary.database.msqlite3 import Sqlite3
|
|
25
|
+
return Sqlite3()
|
|
26
|
+
assert 0, "err sqllite type please check: %s" % type
|