Skip to main content

Dynamic Dataclasses for the Super Lazy

Project description

lazycls

When writing data classes becomes too much work


Motivation

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.


Quickstart

pip install --upgrade lazycls
from 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 = ...
"""

Utilities

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 @classproperty which isn't available for Python < 3.9

  • BaseCls - A wrapper around BaseModel with:

    • arbitrary_types_allowed = True
    • .get(name, default) function to retain dict-like properties
  • BaseLazy - Another wrapper around BaseModel with:

    • arbitrary_types_allowed = True
    • extra = 'allow'
    • alias_generator = to_camelcase
    • orjson serializer by default
    • .get(name, default) function to retain dict-like properties

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

lazycls-0.1.5.tar.gz (164.8 kB view details)

Uploaded Source

Built Distribution

lazycls-0.1.5-py3-none-any.whl (202.0 kB view details)

Uploaded Python 3

File details

Details for the file lazycls-0.1.5.tar.gz.

File metadata

  • Download URL: lazycls-0.1.5.tar.gz
  • Upload date:
  • Size: 164.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for lazycls-0.1.5.tar.gz
Algorithm Hash digest
SHA256 d191ad6ff5a40c21ee3368610cdaa84232948c11cc644cceabe64d3677674f1a
MD5 a2b289f0f0c4c1a66242c1090e3db71b
BLAKE2b-256 799f2a62101ac19fae3d9803d8f6b5127a8506b9d8a4a47d020b28f9e9ad563d

See more details on using hashes here.

File details

Details for the file lazycls-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: lazycls-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 202.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9

File hashes

Hashes for lazycls-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 a8485c672c8f7c1f4d93d6a2bc65c61b3a610e60c0dd2233b4224b144f4ecfa8
MD5 37ec05c5b848e6c9f7b7a74291b4b8c8
BLAKE2b-256 1940ab06d24294af1a0040bd4570009fb630a23c7a0ad2b1b724c10864d84cbe

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page