hhsqllib 0.0__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.
hhsqllib-0.0/PKG-INFO ADDED
@@ -0,0 +1,68 @@
1
+ Metadata-Version: 2.1
2
+ Name: hhsqllib
3
+ Version: 0.0
4
+ Summary: sqllib
5
+ Home-page: https://your.project.url
6
+ Author: hh
7
+ Author-email: hehuang0717@outlook.com
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.9
12
+ Description-Content-Type: text/markdown
13
+ Requires-Dist: addict==2.4.0
14
+ Requires-Dist: attrs==23.2.0
15
+ Requires-Dist: certifi==2024.6.2
16
+ Requires-Dist: charset-normalizer==3.3.2
17
+ Requires-Dist: click==8.1.7
18
+ Requires-Dist: cloudpickle==3.0.0
19
+ Requires-Dist: colorama==0.4.6
20
+ Requires-Dist: contourpy==1.2.1
21
+ Requires-Dist: cramjam==2.8.3
22
+ Requires-Dist: cycler==0.12.1
23
+ Requires-Dist: dask==2024.5.2
24
+ Requires-Dist: datashape==0.5.2
25
+ Requires-Dist: empyrical==0.5.5
26
+ Requires-Dist: fastparquet==2024.2.0
27
+ Requires-Dist: fonttools==4.51.0
28
+ Requires-Dist: fsspec==2024.3.1
29
+ Requires-Dist: greenlet==3.0.3
30
+ Requires-Dist: idna==3.7
31
+ Requires-Dist: importlib_metadata==7.1.0
32
+ Requires-Dist: importlib_resources==6.4.0
33
+ Requires-Dist: kiwisolver==1.4.5
34
+ Requires-Dist: locket==1.0.0
35
+ Requires-Dist: lxml==5.2.2
36
+ Requires-Dist: matplotlib==3.8.4
37
+ Requires-Dist: mssql==1.0.1
38
+ Requires-Dist: multipledispatch==1.0.0
39
+ Requires-Dist: mysql==0.0.3
40
+ Requires-Dist: mysqlclient==2.2.4
41
+ Requires-Dist: networkx==3.2.1
42
+ Requires-Dist: numpy==1.26.4
43
+ Requires-Dist: packaging==24.0
44
+ Requires-Dist: pandas==2.2.2
45
+ Requires-Dist: pandas-datareader==0.10.0
46
+ Requires-Dist: partd==1.4.2
47
+ Requires-Dist: pillow==10.3.0
48
+ Requires-Dist: pyarrow==16.0.0
49
+ Requires-Dist: pymssql==2.3.0
50
+ Requires-Dist: PyMySQL==1.1.0
51
+ Requires-Dist: pyodbc==5.1.0
52
+ Requires-Dist: pyparsing==3.1.2
53
+ Requires-Dist: python-dateutil==2.9.0.post0
54
+ Requires-Dist: pytz==2024.1
55
+ Requires-Dist: PyYAML==6.0.1
56
+ Requires-Dist: requests==2.32.3
57
+ Requires-Dist: scipy==1.13.1
58
+ Requires-Dist: six==1.16.0
59
+ Requires-Dist: SQLAlchemy==2.0.30
60
+ Requires-Dist: toolz==0.12.1
61
+ Requires-Dist: tqdm==4.66.4
62
+ Requires-Dist: typing_extensions==4.11.0
63
+ Requires-Dist: tzdata==2024.1
64
+ Requires-Dist: urllib3==2.2.1
65
+ Requires-Dist: zipp==3.18.1
66
+
67
+ This is for lazy loading sql database
68
+ I modified the sangreal-wind because the inconsistency between the sangreal-wind and others
hhsqllib-0.0/README.md ADDED
@@ -0,0 +1,2 @@
1
+ This is for lazy loading sql database
2
+ I modified the sangreal-wind because the inconsistency between the sangreal-wind and others
File without changes
@@ -0,0 +1,291 @@
1
+ # -*- coding: <encoding name> -*
2
+ import reprlib
3
+ from collections import Iterable
4
+ import pandas as pd
5
+ from sqlalchemy import MetaData, create_engine,cast,NVARCHAR
6
+ from sqlalchemy.engine import reflection
7
+ from sqlalchemy.exc import InvalidRequestError
8
+ from sqlalchemy.ext.automap import automap_base
9
+ from sqlalchemy.ext.declarative import declarative_base
10
+ from sqlalchemy.orm.attributes import InstrumentedAttribute
11
+ from sqlalchemy.orm import Session,Query
12
+
13
+ class HQuery(Query):
14
+ def to_df(self, **kwargs):
15
+ """[pandas.read_sql]
16
+
17
+ Arguments:
18
+ Query {[type]} -- [description]
19
+
20
+ Returns:
21
+ [pd.DataFrame or generate] -- [description]
22
+ """
23
+ return pd.read_sql(sql=self.statement, con=self.session.bind, **kwargs)
24
+
25
+
26
+ class HSession(Session):
27
+ def __init__(self,
28
+ bind=None,
29
+ autoflush=True,
30
+ future=True,
31
+ expire_on_commit=True,
32
+ autocommit=False,
33
+ twophase=False,
34
+ binds=None,
35
+ enable_baked_queries=True,
36
+ info=None,
37
+ query_cls=HQuery):
38
+ super().__init__(
39
+ bind=bind,
40
+ autoflush=autoflush,
41
+ future=future,
42
+ expire_on_commit=expire_on_commit,
43
+ autocommit=autocommit,
44
+ twophase=twophase,
45
+ binds=binds,
46
+ enable_baked_queries=enable_baked_queries,
47
+ info=info,
48
+ query_cls=query_cls)
49
+
50
+
51
+ class DataBase:
52
+ """class for easy to use orm of your database/
53
+
54
+ Raises:
55
+ ValueError -- [bind must be sth like sqlalchemy's engine]
56
+
57
+ Returns:
58
+ [instance of DataBase] -- []
59
+ """
60
+
61
+ def __init__(self, bind, schema=None):
62
+ if bind is None:
63
+ return
64
+ if schema == 'None':
65
+ schema = None
66
+ if isinstance(bind, str):
67
+ bind = create_engine(bind)
68
+ self._bind = bind
69
+ self._schema = schema
70
+ self._metadata = MetaData(self._schema)
71
+ self.Base = declarative_base(metadata=self._metadata)
72
+ self._session = HSession(self._bind)
73
+ self.tables = self._get_tables(self._bind, schema)
74
+ for table in self.tables:
75
+ setattr(self, table, 'None')
76
+ # 可能有大小写问题
77
+ setattr(self, table.lower(), 'None')
78
+ setattr(self, table.upper(), 'None')
79
+
80
+ def inject(self, bind, schema=None):
81
+ self.__init__(bind, schema)
82
+ return
83
+
84
+ def __getattribute__(self, table_name):
85
+ if object.__getattribute__(self, table_name) == 'None':
86
+ try:
87
+ setattr(self, table_name, self._reflect_table(table_name))
88
+ except InvalidRequestError:
89
+ try:
90
+ setattr(self, table_name,
91
+ self._reflect_table(table_name.lower()))
92
+ except InvalidRequestError:
93
+ setattr(self, table_name,
94
+ self._reflect_table(table_name.upper()))
95
+ return object.__getattribute__(self, table_name)
96
+
97
+ def __getattr__(self, name):
98
+ raise AttributeError(
99
+ f'<{name}> is not the right table name, such as {reprlib.repr(self.tables)}.'
100
+ )
101
+
102
+ def __repr__(self):
103
+ return str(self._bind).replace(
104
+ type(self._bind).__name__,
105
+ type(self).__name__)
106
+
107
+ @property
108
+ def bind(self):
109
+ return self._bind
110
+
111
+ @property
112
+ def schema(self):
113
+ return self._schema
114
+
115
+ def _reflect_table(self, table_name):
116
+
117
+ # self._metadata.reflect(bind = self.bind ,only=[table_name])
118
+ self._metadata.reflect(bind=self._bind,schema=self._schema,only=[table_name])
119
+ Base = automap_base(metadata=self._metadata)
120
+ Base.prepare()
121
+ try:
122
+ table = Base.classes[table_name]
123
+ column_list = tuple(table.__dict__.keys())
124
+ # 对列名大小写进行处理
125
+ for column in column_list:
126
+ c = getattr(table, column)
127
+ if isinstance(c, InstrumentedAttribute):
128
+ setattr(table, column.upper(), c)
129
+ setattr(table, column.lower(), c)
130
+ return table
131
+ except KeyError:
132
+ raise ValueError(f"There must be a primary key in {table_name}! \
133
+ or <{table_name}> is not the right table name, such as {reprlib.repr(self.tables)}."
134
+ )
135
+
136
+ def reflect(self):
137
+ self._metadata.reflect(bind = self.bind )
138
+ Base = automap_base(metadata=self._metadata)
139
+ Base.prepare()
140
+ for k, v in Base.classes.items():
141
+ setattr(self, k, v)
142
+ setattr(self, k.lower(), v)
143
+ setattr(self, k.upper(), v)
144
+
145
+ @staticmethod
146
+ def _get_tables(bind, schema):
147
+ insp = reflection.Inspector.from_engine(bind=bind)
148
+ tables = insp.get_table_names(schema=schema)
149
+ return tables
150
+
151
+ def query(self, *columns):
152
+ """[session.query]
153
+
154
+ Returns:
155
+ [Query] -- [sqlalchemy Query cls]
156
+ """
157
+
158
+ return self._session.query(*columns)
159
+
160
+ def update(self, t_obj):
161
+ """[update table]
162
+
163
+ Arguments:
164
+ t_obj {[objs of DeclarativeMeta]} -- [update the table]
165
+ """
166
+
167
+ if isinstance(t_obj, Iterable):
168
+ self._session.add_all(t_obj)
169
+ else:
170
+ self._session.add(t_obj)
171
+
172
+ def insert(self, table, insert_obj, ignore=True, index=None):
173
+ """[insert bulk data]
174
+
175
+ Arguments:
176
+ table {[DeclarativeMeta cls]} -- [reflection of table]
177
+ insert_obj {[pd.DataFrame or list of dicts]} -- [insert_obj]
178
+ ignore {[bool]} -- [whether or not ignore duplicate insert]
179
+ index {[str]} -- [the index you want to ignore]
180
+
181
+ Keyword Arguments:
182
+ ignore {bool} -- [wether ignore exception or not] (default: {True})
183
+
184
+ Raises:
185
+ ValueError -- [f"The {reprlib.repr(insert_obj)} must be list of dicts type!"]
186
+
187
+ Returns:
188
+ [type] -- [description]
189
+ """
190
+
191
+ if isinstance(insert_obj, pd.DataFrame):
192
+ if insert_obj.empty:
193
+ raise ValueError('The input DataFrame is empty, please check!')
194
+ insert_obj = insert_obj.to_dict('records')
195
+ elif not isinstance(insert_obj, list):
196
+ raise ValueError(
197
+ f"The {reprlib.repr(insert_obj)} must be list of dicts type!")
198
+ if self._bind.dialect.name == 'mysql':
199
+ ignore_str = 'IGNORE' if ignore else ''
200
+ elif self._bind.dialect.name == 'sqlite':
201
+ ignore_str = 'OR REPLACE' if ignore else ''
202
+ elif self._bind.dialect.name == 'oracle':
203
+ if ignore and index is None:
204
+ for i in table.__table__.indexes:
205
+ if i.unique:
206
+ index = i.name
207
+ break
208
+ if index is None:
209
+ index = list(table.__table__.constraints)[0].name
210
+ ignore_str = f'/*+ IGNORE_ROW_ON_DUPKEY_INDEX ({table.__table__.name}, {index}) */' if ignore else ''
211
+ elif self._bind.dialect.name == 'mssql':
212
+ ignore_str = ""
213
+ if ignore:
214
+ raise ValueError(
215
+ "You can set 'IGNORE_DUP_KEY = ON' while create the table and set the paramenter 'ignore=False'.")
216
+ return self._session.execute(
217
+ table.__table__.insert().prefix_with(ignore_str).values(insert_obj))
218
+
219
+
220
+ def delete(self, t_obj):
221
+ return self._session.delete(t_obj)
222
+
223
+ def close(self):
224
+ return self._session.close()
225
+
226
+ def merge(self, t_obj):
227
+ """[replace]
228
+
229
+ Arguments:
230
+ t_obj {[objs of DeclarativeMeta]} -- [replace the table]
231
+ """
232
+
233
+ if isinstance(t_obj, Iterable):
234
+ for t in t_obj:
235
+ self._session.merge(t)
236
+ return
237
+ else:
238
+ return self._session.merge(t_obj)
239
+
240
+
241
+ def commit(self):
242
+ return self._session.commit()
243
+
244
+ def flush(self, objects=None):
245
+ return self._session.flush(objects=objects)
246
+
247
+ def rollback(self):
248
+ return self._session.rollback()
249
+
250
+ def refresh(self):
251
+ return self.__init__(self._bind, self._schema)
252
+
253
+ def create_all(self, tables=None, checkfirst=True):
254
+ self._metadata.create_all(
255
+ tables=tables, checkfirst=checkfirst)
256
+ self.refresh()
257
+ return
258
+
259
+
260
+ def get_db(engine, schem):
261
+ try:
262
+ return DataBase(engine, schem)
263
+ except:
264
+ return DataBase(None)
265
+
266
+ """
267
+ 连接wind
268
+ bind = 'mssql+pymssql://xxx:xxxx@xxxx:xxx/nWind?charset=utf8'
269
+ d_t = HDB.ASHAREDESCRIPTION
270
+ stk_desc = HDB.query(d_t.S_INFO_WINDCODE.label('sid'),
271
+ cast(d_t.S_INFO_NAME ,NVARCHAR(100)).label('name')).filter(d_t.S_INFO_NAME == '塔牌集团').to_df()
272
+
273
+ 连接朝阳永续
274
+ bind = 'mssql+pymssql://xxx:xxxx@xxxx:xxx/gogoal2?charset=utf8'
275
+ HDB = get_db(bind, schema)
276
+ d_t = HDB.con_forecast_stk
277
+ stk_desc = HDB.query(d_t .stock_code.label('sid'),
278
+ cast( d_t .stock_name,NVARCHAR(100) ).label('name')).filter( d_t .con_date>'2024-05-01').to_df()
279
+ """
280
+
281
+ if __name__ == '__main__':
282
+ pass
283
+
284
+
285
+
286
+
287
+
288
+
289
+
290
+
291
+
@@ -0,0 +1,68 @@
1
+ Metadata-Version: 2.1
2
+ Name: hhsqllib
3
+ Version: 0.0
4
+ Summary: sqllib
5
+ Home-page: https://your.project.url
6
+ Author: hh
7
+ Author-email: hehuang0717@outlook.com
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: License :: OSI Approved :: MIT License
10
+ Classifier: Operating System :: OS Independent
11
+ Requires-Python: >=3.9
12
+ Description-Content-Type: text/markdown
13
+ Requires-Dist: addict==2.4.0
14
+ Requires-Dist: attrs==23.2.0
15
+ Requires-Dist: certifi==2024.6.2
16
+ Requires-Dist: charset-normalizer==3.3.2
17
+ Requires-Dist: click==8.1.7
18
+ Requires-Dist: cloudpickle==3.0.0
19
+ Requires-Dist: colorama==0.4.6
20
+ Requires-Dist: contourpy==1.2.1
21
+ Requires-Dist: cramjam==2.8.3
22
+ Requires-Dist: cycler==0.12.1
23
+ Requires-Dist: dask==2024.5.2
24
+ Requires-Dist: datashape==0.5.2
25
+ Requires-Dist: empyrical==0.5.5
26
+ Requires-Dist: fastparquet==2024.2.0
27
+ Requires-Dist: fonttools==4.51.0
28
+ Requires-Dist: fsspec==2024.3.1
29
+ Requires-Dist: greenlet==3.0.3
30
+ Requires-Dist: idna==3.7
31
+ Requires-Dist: importlib_metadata==7.1.0
32
+ Requires-Dist: importlib_resources==6.4.0
33
+ Requires-Dist: kiwisolver==1.4.5
34
+ Requires-Dist: locket==1.0.0
35
+ Requires-Dist: lxml==5.2.2
36
+ Requires-Dist: matplotlib==3.8.4
37
+ Requires-Dist: mssql==1.0.1
38
+ Requires-Dist: multipledispatch==1.0.0
39
+ Requires-Dist: mysql==0.0.3
40
+ Requires-Dist: mysqlclient==2.2.4
41
+ Requires-Dist: networkx==3.2.1
42
+ Requires-Dist: numpy==1.26.4
43
+ Requires-Dist: packaging==24.0
44
+ Requires-Dist: pandas==2.2.2
45
+ Requires-Dist: pandas-datareader==0.10.0
46
+ Requires-Dist: partd==1.4.2
47
+ Requires-Dist: pillow==10.3.0
48
+ Requires-Dist: pyarrow==16.0.0
49
+ Requires-Dist: pymssql==2.3.0
50
+ Requires-Dist: PyMySQL==1.1.0
51
+ Requires-Dist: pyodbc==5.1.0
52
+ Requires-Dist: pyparsing==3.1.2
53
+ Requires-Dist: python-dateutil==2.9.0.post0
54
+ Requires-Dist: pytz==2024.1
55
+ Requires-Dist: PyYAML==6.0.1
56
+ Requires-Dist: requests==2.32.3
57
+ Requires-Dist: scipy==1.13.1
58
+ Requires-Dist: six==1.16.0
59
+ Requires-Dist: SQLAlchemy==2.0.30
60
+ Requires-Dist: toolz==0.12.1
61
+ Requires-Dist: tqdm==4.66.4
62
+ Requires-Dist: typing_extensions==4.11.0
63
+ Requires-Dist: tzdata==2024.1
64
+ Requires-Dist: urllib3==2.2.1
65
+ Requires-Dist: zipp==3.18.1
66
+
67
+ This is for lazy loading sql database
68
+ I modified the sangreal-wind because the inconsistency between the sangreal-wind and others
@@ -0,0 +1,9 @@
1
+ README.md
2
+ setup.py
3
+ hhsqllib/__init__.py
4
+ hhsqllib/sqlconnect.py
5
+ hhsqllib.egg-info/PKG-INFO
6
+ hhsqllib.egg-info/SOURCES.txt
7
+ hhsqllib.egg-info/dependency_links.txt
8
+ hhsqllib.egg-info/requires.txt
9
+ hhsqllib.egg-info/top_level.txt
@@ -0,0 +1,53 @@
1
+ addict==2.4.0
2
+ attrs==23.2.0
3
+ certifi==2024.6.2
4
+ charset-normalizer==3.3.2
5
+ click==8.1.7
6
+ cloudpickle==3.0.0
7
+ colorama==0.4.6
8
+ contourpy==1.2.1
9
+ cramjam==2.8.3
10
+ cycler==0.12.1
11
+ dask==2024.5.2
12
+ datashape==0.5.2
13
+ empyrical==0.5.5
14
+ fastparquet==2024.2.0
15
+ fonttools==4.51.0
16
+ fsspec==2024.3.1
17
+ greenlet==3.0.3
18
+ idna==3.7
19
+ importlib_metadata==7.1.0
20
+ importlib_resources==6.4.0
21
+ kiwisolver==1.4.5
22
+ locket==1.0.0
23
+ lxml==5.2.2
24
+ matplotlib==3.8.4
25
+ mssql==1.0.1
26
+ multipledispatch==1.0.0
27
+ mysql==0.0.3
28
+ mysqlclient==2.2.4
29
+ networkx==3.2.1
30
+ numpy==1.26.4
31
+ packaging==24.0
32
+ pandas==2.2.2
33
+ pandas-datareader==0.10.0
34
+ partd==1.4.2
35
+ pillow==10.3.0
36
+ pyarrow==16.0.0
37
+ pymssql==2.3.0
38
+ PyMySQL==1.1.0
39
+ pyodbc==5.1.0
40
+ pyparsing==3.1.2
41
+ python-dateutil==2.9.0.post0
42
+ pytz==2024.1
43
+ PyYAML==6.0.1
44
+ requests==2.32.3
45
+ scipy==1.13.1
46
+ six==1.16.0
47
+ SQLAlchemy==2.0.30
48
+ toolz==0.12.1
49
+ tqdm==4.66.4
50
+ typing_extensions==4.11.0
51
+ tzdata==2024.1
52
+ urllib3==2.2.1
53
+ zipp==3.18.1
@@ -0,0 +1 @@
1
+ hhsqllib
hhsqllib-0.0/setup.cfg ADDED
@@ -0,0 +1,4 @@
1
+ [egg_info]
2
+ tag_build =
3
+ tag_date = 0
4
+
hhsqllib-0.0/setup.py ADDED
@@ -0,0 +1,69 @@
1
+ from setuptools import setup, find_packages
2
+
3
+ setup(name="hhsqllib",
4
+ version="0.0",
5
+ packages=find_packages(),
6
+ install_requires=[
7
+ 'addict==2.4.0',
8
+ 'attrs==23.2.0',
9
+ 'certifi==2024.6.2',
10
+ 'charset-normalizer==3.3.2',
11
+ 'click==8.1.7',
12
+ 'cloudpickle==3.0.0',
13
+ 'colorama==0.4.6',
14
+ 'contourpy==1.2.1',
15
+ 'cramjam==2.8.3',
16
+ 'cycler==0.12.1',
17
+ 'dask==2024.5.2',
18
+ 'datashape==0.5.2',
19
+ 'empyrical==0.5.5',
20
+ 'fastparquet==2024.2.0',
21
+ 'fonttools==4.51.0',
22
+ 'fsspec==2024.3.1',
23
+ 'greenlet==3.0.3',
24
+ 'idna==3.7',
25
+ 'importlib_metadata==7.1.0',
26
+ 'importlib_resources==6.4.0',
27
+ 'kiwisolver==1.4.5',
28
+ 'locket==1.0.0',
29
+ 'lxml==5.2.2',
30
+ 'matplotlib==3.8.4',
31
+ 'mssql==1.0.1',
32
+ 'multipledispatch==1.0.0',
33
+ 'mysql==0.0.3',
34
+ 'mysqlclient==2.2.4',
35
+ 'networkx==3.2.1',
36
+ 'numpy==1.26.4',
37
+ 'packaging==24.0',
38
+ 'pandas==2.2.2',
39
+ 'pandas-datareader==0.10.0',
40
+ 'partd==1.4.2',
41
+ 'pillow==10.3.0',
42
+ 'pyarrow==16.0.0',
43
+ 'pymssql==2.3.0',
44
+ 'PyMySQL==1.1.0',
45
+ 'pyodbc==5.1.0',
46
+ 'pyparsing==3.1.2',
47
+ 'python-dateutil==2.9.0.post0',
48
+ 'pytz==2024.1',
49
+ 'PyYAML==6.0.1',
50
+ 'requests==2.32.3',
51
+ 'scipy==1.13.1',
52
+ 'six==1.16.0',
53
+ 'SQLAlchemy==2.0.30',
54
+ 'toolz==0.12.1',
55
+ 'tqdm==4.66.4',
56
+ 'typing_extensions==4.11.0',
57
+ 'tzdata==2024.1',
58
+ 'urllib3==2.2.1',
59
+ 'zipp==3.18.1'
60
+ ],
61
+ author="hh",
62
+ author_email="hehuang0717@outlook.com",
63
+ description="sqllib",
64
+ long_description=open('README.md').read(),
65
+ long_description_content_type="text/markdown",
66
+ url="https://your.project.url",
67
+ classifiers=["Programming Language :: Python :: 3", "License :: OSI Approved :: MIT License",
68
+ "Operating System :: OS Independent", ],
69
+ python_requires='>=3.9', )