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.1.tar.gz (27.0 kB view details)

Uploaded Source

Built Distribution

lazycls-0.1.1-py3-none-any.whl (31.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for lazycls-0.1.1.tar.gz
Algorithm Hash digest
SHA256 0f3ab4b1bd4a6dab9573948ab239340ea23519c46bb718c4d03c947316ad6d6c
MD5 6d20706d8d20979c5dafb5abdebd51f7
BLAKE2b-256 d3eee9d1b6cb525e98a8caff47c5ae921862f12af800447d60c03a87f7383a83

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for lazycls-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c3cfcdd9a842042afde998919b096f5cfbd5987044d46a5c7f18e93ba8b2fefc
MD5 00d2118ec9118e479970ba1f6af95b41
BLAKE2b-256 c00cff41eeec36ebe2244a29021b0910f0b468b4f49367db7da549fe442336b9

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