Skip to content

Commit

Permalink
Tmp efficiency improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
sveinugu committed Apr 26, 2024
1 parent 8d41b7a commit 189f269
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/omnipy/data/model.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections import defaultdict
from collections.abc import Mapping, Sequence
import functools
import inspect
Expand Down Expand Up @@ -68,6 +69,8 @@
'_pydevd_bundle.pydevd_exec2',
]

validate_cls_counts = defaultdict(int)


def _cleanup_name_qualname_and_module(cls, created_model_or_dataset, model, orig_model):
if isinstance(model, str): # ForwardRef
Expand Down Expand Up @@ -409,6 +412,7 @@ def validate(cls: type['Model'], value: Any) -> 'Model':
Hack to allow overwriting of __iter__ method without compromising pydantic validation. Part
of the pydantic API and not the Omnipy API.
"""
validate_cls_counts[cls.__name__] += 1
if is_model_instance(value):
with AttribHolder(
value, '__iter__', GenericModel.__iter__, switch_to_other=True, on_class=True):
Expand Down
17 changes: 17 additions & 0 deletions src/omnipy/modules/json/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
'_JsonBaseT', bound='JsonScalarM | JsonListM | JsonDictM | JsonAnyListM | JsonAnyDictM')


# TODO: Consider implementing scalars as variants of parent model, e.g. JsonListM
class JsonScalarM(Model[JsonScalar]):
...

Expand All @@ -26,6 +27,14 @@ class JsonDictM(Model[dict[str, _JsonBaseT]], Generic[_JsonBaseT]):
...


class JsonListOfNonModelScalars(Model[list[JsonScalar]]):
...


class JsonDictOfNonModelScalars(Model[dict[str, JsonScalar]]):
...


# Note: This intermediate level of JSON models is needed for two reasons. 1) as targets for
# .updateForwardRefs(), as this does not seem to work properly directly on a generic model
# (e.g. `JsonListM['_JsonAnyUnion'].updateForwardRefs()`), at least in pydantic v1.
Expand Down Expand Up @@ -192,6 +201,10 @@ class JsonListOfListsOfScalarsModel(Model[JsonListM[_JsonListOfScalarsM]]):
...


class JsonListOfListsOfNonModelScalars(Model[JsonListM[JsonListOfNonModelScalars]]):
...


class JsonListOfDictsModel(Model[JsonListM[JsonAnyDictM]]):
...

Expand All @@ -200,6 +213,10 @@ class JsonListOfDictsOfScalarsModel(Model[JsonListM[_JsonDictOfScalarsM]]):
...


class JsonListOfDictsOfNonModelScalars(Model[JsonListM[JsonDictOfNonModelScalars]]):
...


# Dict at the top level


Expand Down
17 changes: 12 additions & 5 deletions src/omnipy/modules/tables/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@

from ..json.models import (JsonCustomListModel,
JsonDictM,
JsonDictOfNonModelScalars,
JsonListM,
JsonListOfDictsOfNonModelScalars,
JsonListOfDictsOfScalarsModel,
JsonListOfListsOfScalarsModel)
JsonListOfListsOfNonModelScalars,
JsonListOfListsOfScalarsModel,
JsonListOfNonModelScalars)


class TableOfStrings(Model[list[dict[str, str]]]):
Expand All @@ -23,11 +27,14 @@ class TableOfStringsAndLists(Model[list[dict[str, str | list[str]]]]):
...


class TableWithColNamesModel(Model[JsonListOfListsOfScalarsModel | JsonListOfDictsOfScalarsModel]):
class TableWithColNamesModel(Model[JsonListOfListsOfNonModelScalars
| JsonListOfDictsOfNonModelScalars]):
@classmethod
def _parse_data(cls, data: JsonListOfListsOfScalarsModel | JsonListOfDictsOfScalarsModel):
def _parse_data(
cls, data: JsonListOfListsOfNonModelScalars | JsonListOfDictsOfNonModelScalars
) -> JsonListOfDictsOfScalarsModel:
if len(data) > 0:
if isinstance(data[0], JsonListM):
if isinstance(data[0], JsonListOfNonModelScalars):
first_row_as_colnames = JsonCustomListModel[str](data[0])
rows = data[1:]
# if len(rows) == 0:
Expand All @@ -37,7 +44,7 @@ def _parse_data(cls, data: JsonListOfListsOfScalarsModel | JsonListOfDictsOfScal
col_name in enumerate(first_row_as_colnames)
} for row in rows]
else:
assert isinstance(data[0], JsonDictM)
assert isinstance(data[0], JsonDictOfNonModelScalars)
return data

return data
Expand Down

0 comments on commit 189f269

Please sign in to comment.