Skip to content

Commit

Permalink
Tmp commit 2
Browse files Browse the repository at this point in the history
  • Loading branch information
sveinugu committed Dec 15, 2023
1 parent ce02a00 commit 6296f0a
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 27 deletions.
3 changes: 3 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ typing-inspect = "^0.8.0"
#orjson = "^3.8.0"
#python-slugify = "^7.0.0"
isort = "^5.12.0"
pathspec = "0.12.1"
tabulate = "^0.9.0"
devtools = "^0.12.2"

[tool.poetry.group.dev.dependencies]
deepdiff = "^6.2.1"
Expand Down
18 changes: 18 additions & 0 deletions src/omnipy/__init__.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
__version__ = '0.12.2'

import importlib
import os
import sys
from typing import Optional

from omnipy.data.dataset import Dataset
from omnipy.data.model import Model
from omnipy.hub.runtime import Runtime
from omnipy.util.helpers import recursive_module_import

# from omnipy.util.helpers import recursive_module_import

ROOT_DIR = os.path.dirname(os.path.abspath(__file__))

Expand All @@ -25,3 +29,17 @@ def _get_runtime() -> Optional['Runtime']:
runtime: Optional['Runtime'] = _get_runtime()

__all__ = [Model, Dataset]


def __getattr__(attr_name: str) -> object:
omnipy = importlib.import_module(__name__)
all_modules = []
recursive_module_import(omnipy, all_modules)
print(all_modules)


#
#
# print(__file__)
# print(__name__)
# print(list(globals().keys()))
11 changes: 10 additions & 1 deletion src/omnipy/data/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from pydantic.fields import Undefined, UndefinedType
from pydantic.generics import GenericModel
from pydantic.utils import lenient_issubclass
from tabulate import tabulate

from omnipy.data.model import _cleanup_name_qualname_and_module, Model
from omnipy.util.helpers import is_optional, is_strict_subclass, remove_forward_ref_notation
Expand Down Expand Up @@ -358,7 +359,7 @@ def save(self, directory: str):

def load(self, directory: str):
serializer_registry = self._get_serializer_registry()
return serializer_registry.load_from_tar_file_path(self, directory, self)
self.include(serializer_registry.load_from_tar_file_path(self, directory, self))

@staticmethod
def _get_serializer_registry():
Expand Down Expand Up @@ -388,6 +389,14 @@ def __repr__(self) -> str:
def __repr_args__(self):
return [(k, v.contents) for k, v in self.data.items()]

def __repr__(self):
return tabulate(
((k, type(v).__name__, len(v) if hasattr(v, '__len__') else 'N/A') \
for k, v in self.items()),
('Data file name', 'Type', 'Length'),
tablefmt="rounded_outline",
)


# TODO: Use json serializer package from the pydantic config instead of 'json'

Expand Down
14 changes: 12 additions & 2 deletions src/omnipy/data/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
TypeVar,
Union)

# from orjson import orjson
from devtools import debug
from pydantic import NoneIsNotAllowedError
from pydantic import Protocol as PydanticProtocol
from pydantic import root_validator, ValidationError
Expand All @@ -24,6 +24,7 @@
from pydantic.main import ModelMetaclass, validate_model
from pydantic.typing import display_as_type, is_none_type
from pydantic.utils import lenient_isinstance, lenient_issubclass
from tabulate import tabulate

from omnipy.data.methodinfo import MethodInfo, SPECIAL_METHODS_INFO
from omnipy.util.contexts import AttribHolder, LastErrorHolder, nothing
Expand Down Expand Up @@ -508,7 +509,8 @@ def _check_for_root_key(self) -> None:
'\t"class MyNumberList(Model[list[int]]): ..."')

def __setattr__(self, attr: str, value: Any) -> None:
if attr in ['__module__'] + list(self.__dict__.keys()) and attr not in [ROOT_KEY]:
if attr in ['__module__'] + list(
self.__dict__.keys()) and attr not in [ROOT_KEY] or attr in ['__repr__']:
super().__setattr__(attr, value)
else:
if attr in ['contents']:
Expand Down Expand Up @@ -617,3 +619,11 @@ def __repr__(self) -> str:

def __repr_args__(self):
return [(None, self.contents)]

def __str__(self):
return tabulate(
((self.__class__.__name__, ''), ('Raw file preview', 'Data structure'),
(str(self.to_data()), debug.format(self))),
maxcolwidths=[(40, 40)],
tablefmt="rounded_outline",
)
24 changes: 23 additions & 1 deletion src/omnipy/util/helpers.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
from collections.abc import Hashable, Iterable
from copy import copy, deepcopy
import inspect
from inspect import getmodule, isclass
import locale as pkg_locale
from types import GenericAlias, UnionType
from types import GenericAlias, ModuleType, UnionType
from typing import (Annotated,
Any,
cast,
Expand Down Expand Up @@ -192,3 +193,24 @@ def last_snapshot_taken_of_same_obj(self, obj: object) -> bool:
def differs_from_last_snapshot(self, obj: object) -> bool:
self._assert_not_empty()
return not all_equals(self._last_snapshot.obj_copy, obj)


def _is_internal_module(module: ModuleType, imported_modules: list[ModuleType]):
return module not in imported_modules and module.__name__.startswith('omnipy')


def recursive_module_import(module: ModuleType, imported_modules: list[ModuleType] = []):
module_vars = vars(module)
imported_modules.append(module)

for val in module_vars.values():
if isclass(val):
for base_cls in val.__bases__:
base_cls_module = getmodule(base_cls)
if base_cls_module and _is_internal_module(base_cls_module, imported_modules):
module_vars = create_merged_dict(
recursive_module_import(base_cls_module, imported_modules),
module_vars,
)

return module_vars
25 changes: 2 additions & 23 deletions src/omnipy/util/mako_helpers.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import ast
from inspect import formatannotation, getmodule, isclass, isgeneratorfunction, Signature
from inspect import formatannotation, isgeneratorfunction, Signature
import os
from types import ModuleType
from typing import Any, get_type_hints

from docstring_parser import DocstringParam, DocstringReturns
from pdocs.doc import Doc, External, Function, Module

from omnipy.util.helpers import create_merged_dict
from omnipy.util.helpers import recursive_module_import

IGNORED = None
IGNORE_PARAMS = ['cls', 'self']
Expand Down Expand Up @@ -109,27 +109,6 @@ def get_type_name_from_annotation(module: ModuleType, annotation, empty_obj):
return type_name


def _is_internal_module(module: ModuleType, imported_modules: list[ModuleType]):
return module not in imported_modules and module.__name__.startswith('omnipy')


def recursive_module_import(module: ModuleType, imported_modules: list[ModuleType] = []):
module_vars = vars(module)
imported_modules.append(module)

for val in module_vars.values():
if isclass(val):
for base_cls in val.__bases__:
base_cls_module = getmodule(base_cls)
if base_cls_module and _is_internal_module(base_cls_module, imported_modules):
module_vars = create_merged_dict(
recursive_module_import(base_cls_module, imported_modules),
module_vars,
)

return module_vars


def convert_to_qual_name_type_hint_str(module: ModuleType, type_hint: Any) -> str:
def fixed_get_type_hints(obj: Any) -> str:
"""
Expand Down

0 comments on commit 6296f0a

Please sign in to comment.