Python Utilities for the Super Lazy
This library is forked from an internal project that works with a lot of dataclasses, (AWS API) and I got tired of writing data classes to work with and manipulate them. This library is a wrapper around the main pydantic.create_model function that recursively parses a dict object and transforms them into subclasses. So nested dict objects within dicts get transformed into their own dataclass.
pip install --upgrade lazyclsfrom lazycls import LazyCls, BaseLazy
data = {
'x': ...,
'y': ...
}
obj = LazyCls(
name: str = 'CustomCls',
data: Dict[str, Any] = data,
modulename: str = 'lazycls', # your module name
basecls: Type[BaseModel] = BaseLazy # A custom Base Model class that is used to generate the model
) -> Type[BaseModel]:
"""
obj = lazycls.CustomCls
lazycls.CustomCls.x = ...
lazycls.CustomCls.y = ...
"""Some additional enhancements/utilities include:
-
set_modulename(name)- set the default module name - useful when included in other libs -
clear_lazy_models- clears all the currently created lazy models. Memory management -
classproperty- allows for usage of@classpropertywhich isn't available for Python < 3.9 -
BaseCls- A wrapper aroundBaseModelwith:arbitrary_types_allowed = True.get(name, default)function to retaindict-like properties
-
BaseLazy- Another wrapper aroundBaseModelwith:arbitrary_types_allowed = Trueextra = 'allow'alias_generator = to_camelcaseorjsonserializer by default.get(name, default)function to retaindict-like properties