sera-2 1.18.1__py3-none-any.whl → 1.19.1__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/make/make_python_model.py +9 -1
- sera/misc/__init__.py +2 -0
- sera/misc/_utils.py +73 -15
- sera/typing.py +1 -0
- {sera_2-1.18.1.dist-info → sera_2-1.19.1.dist-info}/METADATA +1 -1
- {sera_2-1.18.1.dist-info → sera_2-1.19.1.dist-info}/RECORD +7 -7
- {sera_2-1.18.1.dist-info → sera_2-1.19.1.dist-info}/WHEEL +0 -0
sera/make/make_python_model.py
CHANGED
@@ -720,6 +720,12 @@ def make_python_data_model(
|
|
720
720
|
|
721
721
|
program.import_("__future__.annotations", True)
|
722
722
|
program.import_("msgspec", False)
|
723
|
+
|
724
|
+
ident_manager = ImportHelper(
|
725
|
+
program,
|
726
|
+
GLOBAL_IDENTS,
|
727
|
+
)
|
728
|
+
|
723
729
|
if cls.db is not None:
|
724
730
|
# if the class is stored in the database, we need to import the database module
|
725
731
|
program.import_(
|
@@ -824,7 +830,9 @@ def make_python_data_model(
|
|
824
830
|
lambda ast: ast.func(
|
825
831
|
"as_composite",
|
826
832
|
vars=as_composite_args,
|
827
|
-
return_type=
|
833
|
+
return_type=PredefinedFn.item_getter(
|
834
|
+
ident_manager.use("Optional"), expr.ExprIdent(cls.name)
|
835
|
+
),
|
828
836
|
comment="Create an embedded instance from the embedded columns in the database table. If all properties of this embedded class are None (indicating that the parent field is None), then this function will return None.",
|
829
837
|
)(
|
830
838
|
lambda ast_l1: ast_l1.if_(
|
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
|
-
|
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,71 @@ def load_data(
|
|
133
143
|
with Session(engine) as session:
|
134
144
|
create_db_and_tables()
|
135
145
|
|
136
|
-
for
|
137
|
-
if
|
138
|
-
|
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"]
|
139
162
|
else:
|
140
|
-
raise ValueError(
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
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}")
|
174
|
+
else:
|
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
|
-
|
149
|
-
|
150
|
-
|
193
|
+
for tbl in tbls:
|
194
|
+
# Check if the table has an auto-incrementing primary key
|
195
|
+
if not hasattr(tbl, "__table__") or not tbl.__table__.primary_key:
|
196
|
+
continue
|
197
|
+
|
198
|
+
pk_columns = tbl.__table__.primary_key.columns
|
199
|
+
has_foreign_key = any(len(col.foreign_keys) > 0 for col in pk_columns)
|
200
|
+
has_auto_increment = any(
|
201
|
+
col.autoincrement and col.type.python_type in (int,)
|
202
|
+
for col in pk_columns
|
203
|
+
)
|
204
|
+
if has_foreign_key or not has_auto_increment:
|
205
|
+
continue
|
206
|
+
session.execute(
|
207
|
+
text(
|
208
|
+
f"SELECT setval('{tbl.__tablename__}_id_seq', (SELECT MAX(id) FROM \"{tbl.__tablename__}\"));"
|
209
|
+
)
|
151
210
|
)
|
152
|
-
)
|
153
211
|
session.commit()
|
154
212
|
|
155
213
|
|
sera/typing.py
CHANGED
@@ -23,12 +23,12 @@ sera/make/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
23
23
|
sera/make/__main__.py,sha256=HRfOR53p351h6KblVvYm3DLhDIfEtk6R0kjl78_S_S8,1453
|
24
24
|
sera/make/make_app.py,sha256=n9NtW73O3s_5Q31VHIRmnd-jEIcpDO7ksAsOdovde2s,5999
|
25
25
|
sera/make/make_python_api.py,sha256=iXGbKQ3IJvsY1ur_fhurr_THFNnH66E3Wl85o0emUbw,26853
|
26
|
-
sera/make/make_python_model.py,sha256=
|
26
|
+
sera/make/make_python_model.py,sha256=RfUATMs6d_glxbKnpLum9tsU2pNOyifvJX76S8pI_DY,62964
|
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=
|
29
|
+
sera/misc/__init__.py,sha256=rOmGMv7QNzpSKZSyxChbRmEnBr3O443UlLGS0FIs3AI,561
|
30
30
|
sera/misc/_formatter.py,sha256=aCGYL08l8f3aLODHxSocxBBwkRYEo3K1QzCDEn3suj0,1685
|
31
|
-
sera/misc/_utils.py,sha256=
|
31
|
+
sera/misc/_utils.py,sha256=_-17XbK6qp3HobcI9iLF4xfaATvFg1ckUzgg7r7Ctmw,7135
|
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
|
@@ -41,7 +41,7 @@ sera/models/_multi_lingual_string.py,sha256=JETN6k00VH4wrA4w5vAHMEJV8fp3SY9bJebs
|
|
41
41
|
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
|
-
sera/typing.py,sha256=
|
45
|
-
sera_2-1.
|
46
|
-
sera_2-1.
|
47
|
-
sera_2-1.
|
44
|
+
sera/typing.py,sha256=m4rir-fB6Cgcm7_ZSXXcNdla2LJgq96WXxtTTrDaJno,1058
|
45
|
+
sera_2-1.19.1.dist-info/METADATA,sha256=vhXuNeYXDuvLW_dW_7jStIcyq0OhEqV9XXp74taTQzY,936
|
46
|
+
sera_2-1.19.1.dist-info/WHEEL,sha256=b4K_helf-jlQoXBBETfwnf4B04YC67LOev0jo4fX5m8,88
|
47
|
+
sera_2-1.19.1.dist-info/RECORD,,
|
File without changes
|