sera-2 1.18.1__py3-none-any.whl → 1.19.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.
sera/misc/__init__.py CHANGED
@@ -1,5 +1,6 @@
1
1
  from sera.misc._formatter import File, Formatter
2
2
  from sera.misc._utils import (
3
+ LoadTableDataArgs,
3
4
  assert_isinstance,
4
5
  assert_not_null,
5
6
  filter_duplication,
@@ -24,4 +25,5 @@ __all__ = [
24
25
  "load_data",
25
26
  "identity",
26
27
  "get_classpath",
28
+ "LoadTableDataArgs",
27
29
  ]
sera/misc/_utils.py CHANGED
@@ -1,10 +1,12 @@
1
1
  from __future__ import annotations
2
2
 
3
3
  import re
4
+ from dataclasses import dataclass
4
5
  from pathlib import Path
5
- from typing import Any, Callable, Iterable, Optional, Type, TypeVar
6
+ from typing import Any, Callable, Iterable, Optional, Sequence, Type, TypedDict, TypeVar
6
7
 
7
8
  import serde.csv
9
+ import serde.json
8
10
  from sqlalchemy import Engine, text
9
11
  from sqlalchemy.orm import Session
10
12
  from tqdm import tqdm
@@ -113,11 +115,19 @@ def filter_duplication(
113
115
  return new_lst
114
116
 
115
117
 
118
+ class LoadTableDataArgs(TypedDict, total=False):
119
+ table: type
120
+ tables: Sequence[type]
121
+ file: Path
122
+ files: Sequence[Path]
123
+ file_deser: Callable[[Path], list[dict]]
124
+ record_deser: Callable[[dict], Any | list[Any]]
125
+
126
+
116
127
  def load_data(
117
128
  engine: Engine,
118
129
  create_db_and_tables: Callable[[], None],
119
- table_files: list[tuple[type, Path]],
120
- table_desers: dict[type, Callable[[dict], Any]],
130
+ table_data: Sequence[LoadTableDataArgs],
121
131
  verbose: bool = False,
122
132
  ):
123
133
  """
@@ -133,23 +143,59 @@ def load_data(
133
143
  with Session(engine) as session:
134
144
  create_db_and_tables()
135
145
 
136
- for tbl, file in tqdm(table_files, disable=not verbose, desc="Loading data"):
137
- if file.name.endswith(".csv"):
138
- records = serde.csv.deser(file, deser_as_record=True)
146
+ for args in tqdm(table_data, disable=not verbose, desc="Loading data"):
147
+ if "table" in args:
148
+ tbls = [args["table"]]
149
+ elif "tables" in args:
150
+ tbls = args["tables"]
151
+ else:
152
+ raise ValueError("Either 'table' or 'tables' must be provided in args.")
153
+
154
+ if "file" in args:
155
+ assert isinstance(args["file"], Path), "File must be a Path object."
156
+ files = [args["file"]]
157
+ elif "files" in args:
158
+ assert all(
159
+ isinstance(f, Path) for f in args["files"]
160
+ ), "Files must be Path objects."
161
+ files = args["files"]
162
+ else:
163
+ raise ValueError("Either 'file' or 'files' must be provided in args.")
164
+
165
+ raw_records = []
166
+ if "file_deser" not in args:
167
+ for file in files:
168
+ if file.name.endswith(".csv"):
169
+ raw_records.extend(serde.csv.deser(file, deser_as_record=True))
170
+ elif file.name.endswith(".json"):
171
+ raw_records.extend(serde.json.deser(file))
172
+ else:
173
+ raise ValueError(f"Unsupported file format: {file.name}")
139
174
  else:
140
- raise ValueError(f"Unsupported file format: {file.name}")
141
- deser = table_desers[tbl]
142
- records = [deser(row) for row in records]
143
- for r in tqdm(records, desc=f"load {tbl.__name__}", disable=not verbose):
144
- session.merge(r)
175
+ for file in files:
176
+ raw_records.extend(args["file_deser"](file))
177
+
178
+ deser = args["record_deser"]
179
+ records = [deser(row) for row in raw_records]
180
+ for r in tqdm(
181
+ records,
182
+ desc=f"load {', '.join(tbl.__name__ for tbl in tbls)}",
183
+ disable=not verbose,
184
+ ):
185
+ if isinstance(r, Sequence):
186
+ for x in r:
187
+ session.merge(x)
188
+ else:
189
+ session.merge(r)
145
190
  session.flush()
146
191
 
147
192
  # Reset the sequence for each table
148
- session.execute(
149
- text(
150
- f"SELECT setval('{tbl.__tablename__}_id_seq', (SELECT MAX(id) FROM \"{tbl.__tablename__}\"));"
193
+ for tbl in tbls:
194
+ session.execute(
195
+ text(
196
+ f"SELECT setval('{tbl.__tablename__}_id_seq', (SELECT MAX(id) FROM \"{tbl.__tablename__}\"));"
197
+ )
151
198
  )
152
- )
153
199
  session.commit()
154
200
 
155
201
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: sera-2
3
- Version: 1.18.1
3
+ Version: 1.19.0
4
4
  Summary:
5
5
  Author: Binh Vu
6
6
  Author-email: bvu687@gmail.com
@@ -26,9 +26,9 @@ sera/make/make_python_api.py,sha256=iXGbKQ3IJvsY1ur_fhurr_THFNnH66E3Wl85o0emUbw,
26
26
  sera/make/make_python_model.py,sha256=Nc4vDGgM8icgWBqzNnMgEkLadf5EsZwbbHs3WLW9_co,62778
27
27
  sera/make/make_python_services.py,sha256=0ZpWLwQ7Nwfn8BXAikAB4JRpNknpSJyJgY5b1cjtxV4,2073
28
28
  sera/make/make_typescript_model.py,sha256=1ouYFCeqOlwEzsGBiXUn4VZtLJjJW7GSacdOSlQzhjI,67012
29
- sera/misc/__init__.py,sha256=Tali_UBtwemETM30a6sP6BbwBMHr3hklPCX0bgiAcbw,513
29
+ sera/misc/__init__.py,sha256=rOmGMv7QNzpSKZSyxChbRmEnBr3O443UlLGS0FIs3AI,561
30
30
  sera/misc/_formatter.py,sha256=aCGYL08l8f3aLODHxSocxBBwkRYEo3K1QzCDEn3suj0,1685
31
- sera/misc/_utils.py,sha256=f5mOgDlGh-OVwd6DXou2gTo9eRvJGK_aUT7pM3qzr98,4882
31
+ sera/misc/_utils.py,sha256=O0Qh_BZXX7kmzhFBTnnxJT4BUxQbwNovhepSwhh33Ow,6532
32
32
  sera/models/__init__.py,sha256=vJC5Kzo_N7wd16ocNPy1VvAZDGNiWeiAhWJ4ihATKvA,780
33
33
  sera/models/_class.py,sha256=1J4Bd_LanzhhDWwZFHWGtFYD7lupe_alaB3D02ebNDI,2862
34
34
  sera/models/_collection.py,sha256=ZnQEriKC4X88Zz48Kn1AVZKH-1_l8OgWa-zf2kcQOOE,1414
@@ -42,6 +42,6 @@ sera/models/_parse.py,sha256=ciTLzCkO0q6xA1R_rHbnYJYK3Duo2oh56WeuwxXwJaI,12392
42
42
  sera/models/_property.py,sha256=9yMDxrmbyuF6-29lQjiq163Xzwbk75TlmGBpu0NLpkI,7485
43
43
  sera/models/_schema.py,sha256=VxJEiqgVvbXgcSUK4UW6JnRcggk4nsooVSE6MyXmfNY,1636
44
44
  sera/typing.py,sha256=o_DKfSvs8JpNRQ0kdaTc3BbfdkvibY3uY4tJRt-n2fQ,1023
45
- sera_2-1.18.1.dist-info/METADATA,sha256=TATTG19o7HW6O681m1dUdFs92SJ6oqizZUT_vF52zx8,936
46
- sera_2-1.18.1.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
47
- sera_2-1.18.1.dist-info/RECORD,,
45
+ sera_2-1.19.0.dist-info/METADATA,sha256=22HLRJ7LXZYcBduEokfXaTnePGNCUIyARqVDZFP404k,936
46
+ sera_2-1.19.0.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
47
+ sera_2-1.19.0.dist-info/RECORD,,