ecodev-core 0.0.44__py3-none-any.whl → 0.0.46__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.

Potentially problematic release.


This version of ecodev-core might be problematic. Click here for more details.

ecodev_core/__init__.py CHANGED
@@ -44,7 +44,7 @@ from ecodev_core.db_upsertion import field
44
44
  from ecodev_core.db_upsertion import sfield
45
45
  from ecodev_core.db_upsertion import upsert_data
46
46
  from ecodev_core.db_upsertion import upsert_deletor
47
- from ecodev_core.db_upsertion import upsert_dict_data
47
+ from ecodev_core.db_upsertion import upsert_df_data
48
48
  from ecodev_core.db_upsertion import upsert_selector
49
49
  from ecodev_core.deployment import Deployment
50
50
  from ecodev_core.email_sender import send_email
@@ -102,5 +102,5 @@ __all__ = [
102
102
  'get_access_token', 'safe_get_user', 'backup', 'group_by', 'get_excelfile', 'upsert_new_user',
103
103
  'datify', 'safe_drop_columns', 'get_value', 'is_null', 'send_email', 'first_func_or_default',
104
104
  'sort_by_keys', 'sort_by_values', 'Settings', 'load_yaml_file', 'Deployment', 'Version',
105
- 'sfield', 'field', 'upsert_data', 'upsert_deletor', 'get_row_versions', 'get_versions',
106
- 'db_to_value', 'upsert_dict_data', 'upsert_selector']
105
+ 'sfield', 'field', 'upsert_df_data', 'upsert_deletor', 'get_row_versions', 'get_versions',
106
+ 'db_to_value', 'upsert_data', 'upsert_selector']
@@ -22,6 +22,7 @@ from ecodev_core.version import Version
22
22
  BATCH_SIZE = 5000
23
23
  FILTER_ON = 'filter_on'
24
24
  INFO = 'info'
25
+ SA_COLUMN_KWARGS = 'sa_column_kwargs'
25
26
 
26
27
 
27
28
  def sfield(**kwargs):
@@ -29,14 +30,26 @@ def sfield(**kwargs):
29
30
  Field constructor for columns not to be versioned. Those are the columns on which to select.
30
31
  They morally are a sort of unique identifier of a row (like id but more business meaningful)
31
32
  """
32
- return Field(**kwargs, sa_column_kwargs={INFO: {FILTER_ON: True}})
33
+ sa_column_kwargs = _get_sa_column_kwargs(kwargs, sfield=True)
34
+ return Field(**kwargs, sa_column_kwargs=sa_column_kwargs)
33
35
 
34
36
 
35
37
  def field(**kwargs):
36
38
  """
37
39
  Field constructor for columns to be versioned.
38
40
  """
39
- return Field(**kwargs, sa_column_kwargs={INFO: {FILTER_ON: False}})
41
+ sa_column_kwargs = _get_sa_column_kwargs(kwargs, sfield=False)
42
+ return Field(**kwargs, sa_column_kwargs=sa_column_kwargs)
43
+
44
+
45
+ def _get_sa_column_kwargs(kwargs, sfield: bool) -> dict:
46
+ """
47
+ Combine existing sa_column_kwargs with the new field necessary for versioning
48
+ """
49
+ if not (additional_vals := kwargs.get(SA_COLUMN_KWARGS)):
50
+ return {INFO: {FILTER_ON: sfield}}
51
+ kwargs.pop(SA_COLUMN_KWARGS)
52
+ return additional_vals | {INFO: {FILTER_ON: sfield}}
40
53
 
41
54
 
42
55
  def upsert_selector(values: SQLModel, db_schema: SQLModelMetaclass) -> SelectOfScalar:
@@ -83,24 +96,27 @@ def upsert_deletor(values: SQLModel, session: Session):
83
96
  session.commit()
84
97
 
85
98
 
86
- def upsert_data(df: Union[pd.DataFrame], db_schema: SQLModelMetaclass, session: Session) -> None:
99
+ def upsert_df_data(df: Union[pd.DataFrame], db_schema: SQLModelMetaclass, session: Session) -> None:
87
100
  """
88
101
  Upsert the passed df into db_schema db.
89
102
  """
90
- upsert_dict_data([x.to_dict() for _, x in df.iterrows()], db_schema, session)
103
+ upsert_data([x.to_dict() for _, x in df.iterrows()], session, raw_db_schema=db_schema)
91
104
 
92
105
 
93
- def upsert_dict_data(data: list[dict], db_schema: SQLModelMetaclass, session: Session) -> None:
106
+ def upsert_data(data: list[dict | SQLModelMetaclass],
107
+ session: Session,
108
+ raw_db_schema: SQLModelMetaclass | None = None) -> None:
94
109
  """
95
110
  Upsert the passed list of dicts (corresponding to db_schema) into db_schema db.
96
111
  """
112
+ db_schema = raw_db_schema or data[0].__class__
97
113
  selector = partial(upsert_selector, db_schema=db_schema)
98
114
  updator = partial(upsert_updator, db_schema=db_schema)
99
115
  batches = [data[i:i + BATCH_SIZE] for i in range(0, len(data), BATCH_SIZE)]
100
116
 
101
117
  for batch in progressbar.progressbar(batches, redirect_stdout=False):
102
118
  for row in batch:
103
- new_object = db_schema(**row)
119
+ new_object = db_schema(**row) if isinstance(row, dict) else row
104
120
  if in_db := session.exec(selector(new_object)).first():
105
121
  session.exec(updator(new_object, in_db.id, session))
106
122
  else:
ecodev_core/version.py CHANGED
@@ -38,7 +38,7 @@ class Version(SQLModel, table=True): # type: ignore
38
38
  Attributes are:
39
39
  - table: the table to version
40
40
  - column: the column of table to version
41
- - row_id: the the row id of the column of table to version
41
+ - row_id: the row id of the column of table to version
42
42
  - col_type: the column type
43
43
  - value: the value to store (previous row/column/table version)
44
44
  """
@@ -97,15 +97,15 @@ def _col_type_to_db(col_type: type | EnumType) -> ColType:
97
97
  """
98
98
  Forge ColType out of passed col_type
99
99
  """
100
- if col_type == int:
100
+ if issubclass(int, col_type):
101
101
  return ColType.INT
102
- if col_type == bool:
102
+ if issubclass(bool, col_type):
103
103
  return ColType.BOOL
104
- if col_type == float:
104
+ if issubclass(float, col_type):
105
105
  return ColType.FLOAT
106
- if col_type == str:
106
+ if issubclass(str, col_type):
107
107
  return ColType.STR
108
- if col_type == datetime:
108
+ if issubclass(datetime, col_type):
109
109
  return ColType.DATE
110
110
  return ColType.ENUM
111
111
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: ecodev-core
3
- Version: 0.0.44
3
+ Version: 0.0.46
4
4
  Summary: Low level sqlmodel/fastapi/pydantic building blocks
5
5
  License: MIT
6
6
  Author: Thomas Epelbaum
@@ -1,4 +1,4 @@
1
- ecodev_core/__init__.py,sha256=gSEDFqBQLI2tx2macOfnea0z1KdwntTpxbwtf0FiqjI,5785
1
+ ecodev_core/__init__.py,sha256=cIICQkHHfjmPIpLnpGlJ6GzGDvFT51bDeI8c1Kg3MH0,5781
2
2
  ecodev_core/app_activity.py,sha256=KBtI-35LBLPDppFB7xjxWthXQrY3Z_aGDnC-HrW8Ea0,4641
3
3
  ecodev_core/app_rights.py,sha256=RZPdDtydFqc_nFj96huKAc56BS0qS6ScKv4Kghqd6lc,726
4
4
  ecodev_core/app_user.py,sha256=r1bqA4H08x53XmxmjwyGKl_PFjYQazzBbVErdkztqeE,2947
@@ -11,7 +11,7 @@ ecodev_core/db_connection.py,sha256=bc5MujZ57f204wTsuNVdn1JdP-zBzkDJxHmdxBDTiNs,
11
11
  ecodev_core/db_filters.py,sha256=T_5JVF27UEu7sC6NOm7-W3_Y0GLfbWQO_EeTXcD2cv8,5041
12
12
  ecodev_core/db_insertion.py,sha256=RSCyAlUObbBlWJuMRX-YFY4VgtWqYLdwRqMWw--x95Y,3646
13
13
  ecodev_core/db_retrieval.py,sha256=IxyF3ZtKgACLiNFggK7boKggvMRKYDRD2IimxU4dap4,7345
14
- ecodev_core/db_upsertion.py,sha256=Gds1rgbrzFh0rkEY0gTwHLRnY-5izZvFmvZRekV47Zs,3936
14
+ ecodev_core/db_upsertion.py,sha256=W7_TEMZh97FUQ9nJnOXAiwH1u5KLmF8pinjjM6NUaDc,4602
15
15
  ecodev_core/deployment.py,sha256=z8ACI00EtKknXOB8xyPwYIXTvPjIDOH9z9cBGEU0YrA,281
16
16
  ecodev_core/email_sender.py,sha256=XD7jAVXhGzvbiHqMhK9_aTEIS70Lw_CmPeAxRZGji-Y,1610
17
17
  ecodev_core/enum_utils.py,sha256=BkQ4YQ97tXBYmMcQiSIi0mbioD5CgVU79myg1BBAXuA,556
@@ -25,8 +25,8 @@ ecodev_core/read_write.py,sha256=YIGRERvFHU7vy-JIaCWAza4CPMysLRUHKJxN-ZgFmu0,120
25
25
  ecodev_core/safe_utils.py,sha256=Q8N15El1tSxZJJsy1i_1CCycuBN1_98QQoHmYJRcLIY,6904
26
26
  ecodev_core/settings.py,sha256=ARAPkXxggVUsYqSQIAgCK8C2DKSMPia1CekULn428bA,1562
27
27
  ecodev_core/sqlmodel_utils.py,sha256=t57H3QPtKRy4ujic1clMK_2L4p0yjGJLZbDjHPZ8M94,453
28
- ecodev_core/version.py,sha256=BUMmO_dxwyTPGIbnbOTFIED4JMmOmN8khQzP2ea-Obg,4377
29
- ecodev_core-0.0.44.dist-info/LICENSE.md,sha256=8dqVJEbwXjPWjjRKjdLMym5k9Gi8hwtrHh84sti6KIs,1068
30
- ecodev_core-0.0.44.dist-info/METADATA,sha256=P-ssz6zvzY4RZmknnMlA2lr15jHnu2b0XBwuHtpdZLc,3509
31
- ecodev_core-0.0.44.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
32
- ecodev_core-0.0.44.dist-info/RECORD,,
28
+ ecodev_core/version.py,sha256=5lQ5Nzi38ziFzHICoRoGyLzidgFcUCnLdxlpK6s8Y0U,4423
29
+ ecodev_core-0.0.46.dist-info/LICENSE.md,sha256=8dqVJEbwXjPWjjRKjdLMym5k9Gi8hwtrHh84sti6KIs,1068
30
+ ecodev_core-0.0.46.dist-info/METADATA,sha256=DAo4XMyD0emMg2xvp-QBWdc5-ZvcwmwZjoANZVvK5dI,3509
31
+ ecodev_core-0.0.46.dist-info/WHEEL,sha256=sP946D7jFCHeNz5Iq4fL4Lu-PrWrFsgfLXbbkciIZwg,88
32
+ ecodev_core-0.0.46.dist-info/RECORD,,