mtsql 1.12.7__py3-none-any.whl → 1.12.8__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.
mt/sql/base.py CHANGED
@@ -6,8 +6,7 @@ import sqlalchemy as sa
6
6
  import sqlalchemy.exc as se
7
7
  import psycopg as ps
8
8
 
9
- from mt import tp, logg, pd, ctx
10
- from mt.halo import Halo
9
+ from mt import tp, logg, pd, ctx, halo
11
10
 
12
11
 
13
12
  __all__ = [
@@ -27,6 +26,7 @@ __all__ = [
27
26
  "temp_table_name",
28
27
  "temp_table_find_new_id",
29
28
  "temp_table_drop",
29
+ "to_temp_table",
30
30
  ]
31
31
 
32
32
 
@@ -166,7 +166,7 @@ def read_sql(
166
166
 
167
167
  if chunksize is not None:
168
168
  s = "read_sql: '{}'".format(text_sql)
169
- spinner = Halo(s, spinner="dots", enabled=bool(logger))
169
+ spinner = halo.Halo(s, spinner="dots", enabled=bool(logger))
170
170
  spinner.start()
171
171
  ts = pd.Timestamp.now()
172
172
  cnt = 0
@@ -453,3 +453,28 @@ def temp_table_drop(
453
453
  name = id if isinstance(id, str) else temp_table_name(id)
454
454
  sql = f"DROP TABLE IF EXISTS {name}"
455
455
  return engine_execute(engine, sql)
456
+
457
+
458
+ @ctx.contextmanager
459
+ def to_temp_table(df: pd.DataFrame, engine: sa.engine.Engine):
460
+ """
461
+ A context manager that uploads a dataframe to a temp table and cleans up the table when done.
462
+
463
+ You can use the class in a with statement to work with the temp table, whose name is returned
464
+ as the context manager object.
465
+
466
+ Parameters
467
+ ----------
468
+ df : pandas.DataFrame
469
+ dataframe to be uploaded to the database as a temporary table
470
+ engine : sqlalchemy.engine.Engine
471
+ engine connrecting to the database
472
+ """
473
+
474
+ temp_table_id = temp_table_find_new_id(engine)
475
+ temp_table_name = temp_table_name(temp_table_id)
476
+ try:
477
+ df.to_sql(temp_table_name, engine)
478
+ yield temp_table_name
479
+ finally:
480
+ temp_table_drop(engine, temp_table_id)
mt/sql/version.py CHANGED
@@ -1,5 +1,5 @@
1
1
  MAJOR_VERSION = 1
2
2
  MINOR_VERSION = 12
3
- PATCH_VERSION = 7
3
+ PATCH_VERSION = 8
4
4
  version = '{}.{}.{}'.format(MAJOR_VERSION, MINOR_VERSION, PATCH_VERSION)
5
5
  __all__ = ['MAJOR_VERSION', 'MINOR_VERSION', 'PATCH_VERSION', 'version']
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mtsql
3
- Version: 1.12.7
3
+ Version: 1.12.8
4
4
  Summary: Extra Python modules to deal with the interaction between pandas dataframes and remote SQL servers, for Minh-Tri Pham
5
5
  Home-page: https://github.com/inteplus/mtsql
6
6
  Author: ['Minh-Tri Pham']
@@ -12,8 +12,8 @@ Requires-Dist: tzlocal
12
12
  Requires-Dist: tqdm
13
13
  Requires-Dist: psycopg[binary]
14
14
  Requires-Dist: redshift_connector>=2.1.5
15
- Requires-Dist: mtbase>=4.31.5
16
- Requires-Dist: mtpandas>=1.16.8
15
+ Requires-Dist: mtbase>=4.32.20
16
+ Requires-Dist: mtpandas>=1.17.10
17
17
  Dynamic: author
18
18
  Dynamic: home-page
19
19
  Dynamic: license-file
@@ -1,17 +1,17 @@
1
1
  mt/sql/__init__.py,sha256=b7zO50apZxt9Hg2eOkJhRLrXgACR8eS5b-Rphdn5qNQ,44
2
- mt/sql/base.py,sha256=CPrI28CZcEEp3cBQBg5_9APotlCiF3U6sg3moeUKZVM,12242
2
+ mt/sql/base.py,sha256=fqNA0gDeUKYdMGPOIo33y_E2s-t0sBNtoR2hYxTdJGM,13037
3
3
  mt/sql/mysql.py,sha256=n2ENDctdUqZuSaDAcrqZYtPtawq3Wx4dOPCRsCB5Q4w,4894
4
4
  mt/sql/psql.py,sha256=dNPOvJBxZ8S88uOmIKnfjo1ssRiMLDLwVeJcVcdcNco,66580
5
5
  mt/sql/sqlite.py,sha256=T2ak_hhNi_zRfpg_gp8JhNHn7D2kl4i-Ey6-9ANMtz0,8678
6
- mt/sql/version.py,sha256=TZkj7gDWCVgkQFfdQQxVsySbeb72IGyS62UsT1inxS4,207
6
+ mt/sql/version.py,sha256=9cPEw4vxFA-JdEQQR7uevWv6fRYCk_Vbs4X79gmGUzg,207
7
7
  mt/sql/redshift/__init__.py,sha256=S-eRxJWcrvncF7LZXuulCdPV-UERu9eiw6uyDb5aGsM,443
8
8
  mt/sql/redshift/commands.py,sha256=xhGUBf3bL66EYdZI5HCUtOx-XqPCnXT_P-LnhPgtzrY,40193
9
9
  mt/sql/redshift/ddl.py,sha256=eUcZj9oIajiE1wKKBAP-V64gYJ7cVnSAt8dLFfluOJA,9777
10
10
  mt/sql/redshift/dialect.py,sha256=oomLiQib0iMhotRY3GjDrQI3iWGXgcIQyK5WTbmj8kc,53431
11
11
  mt/sql/redshift/main.py,sha256=6dwnwNJ1F0_V9o2oqrSOkyN_pAMrgE01CCoqAjoyOME,17116
12
12
  mt/sql/redshift/redshift-ca-bundle.crt,sha256=532qYkOpQOstFE0mdXE1GVtL3v00XDKgZNTr6gK5-KE,8621
13
- mtsql-1.12.7.dist-info/licenses/LICENSE,sha256=PojkRlQzTT5Eg6Nj03XoIVEefN3u8iiIFf1p4rqe_t4,1070
14
- mtsql-1.12.7.dist-info/METADATA,sha256=27DDVa8knElWPBe05BX1GJR2dPhLZ0MvYVULygmMNyg,737
15
- mtsql-1.12.7.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
16
- mtsql-1.12.7.dist-info/top_level.txt,sha256=WcqGFu9cV7iMZg09iam8eNxUvGpLSKKF2Iubf6SJVOo,3
17
- mtsql-1.12.7.dist-info/RECORD,,
13
+ mtsql-1.12.8.dist-info/licenses/LICENSE,sha256=PojkRlQzTT5Eg6Nj03XoIVEefN3u8iiIFf1p4rqe_t4,1070
14
+ mtsql-1.12.8.dist-info/METADATA,sha256=3psQ6918Qf0xhB5-133fvGLzVBZc9eo6Xoj3kjzm4IA,739
15
+ mtsql-1.12.8.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
16
+ mtsql-1.12.8.dist-info/top_level.txt,sha256=WcqGFu9cV7iMZg09iam8eNxUvGpLSKKF2Iubf6SJVOo,3
17
+ mtsql-1.12.8.dist-info/RECORD,,
File without changes