Skip to main content

python package dataclass utils

Project description

datclass

python dataclass nested & extra attrs

Extending the official dataclass to support nested and extended fields.

Install

PyPI python version Downloads

pip install -U datclass
pip install git+ssh://git@github.com/foyoux/datclass.git
pip install git+https://github.com/foyoux/datclass.git

Usage example

Example 1

from dataclasses import dataclass
from typing import List

# Default `dataclass` supports nested and extended fields. Missing fields will be logged.
from datclass import DatClass


# Custom `DatClass`
# from datclass import get_datclass
# Missing fields will not be logged.
# DatClass = get_datclass(log=False)


@dataclass
class User(DatClass):
    name: str
    age: int


@dataclass
class Group(DatClass):
    name: str
    users: List[User]


if __name__ == '__main__':
    user1 = User(name='foo', age=18)
    # Saving a data class to a file.
    user1.to_file('user.json')
    # Adding some control parameters.
    user1.to_file('user.json', indent=4, ignore_none=True, sort_keys=True)

    # Creating a data class from a dict.
    user2 = User(**{'name': 'bar', 'age': 20})
    # To convert a data class to a dictionary, you can support extended fields.
    # You can also use the official asdict function, but it cannot export extended fields.
    dict1 = user2.to_dict()
    # 'ignore_none' is used to ignore values that are None.
    dict2 = user2.to_dict(ignore_none=True)

    # Creating a data class from a string.
    user3 = User.from_str('{"name": "baz", "age": 22}')
    # Convert the data class to a JSON string.
    dict3 = user3.to_str()
    dict4 = user3.to_str(indent=4, ignore_none=True)

    # Creating a data class from a file.
    user4 = User.from_file('user.json')

    # Nested data classes
    grp = Group(name='group1', users=[user1, user2, user3, user4])
    grp.to_file('group.json', indent=4, ignore_none=True, sort_keys=True)

    for user in grp.users:
        print(user.name, user.age)

    # Extending fields
    user = {'name': 'foo', 'age': 18, 'sex': 1}
    user5 = User(**user)
    assert user5.to_dict() == user

Example 2

from dataclasses import dataclass
from typing import ClassVar, Dict

from datclass import DatClass


@dataclass
class User(DatClass):
    name: str
    age: int
    attr_123: str

    # Renaming fields.
    __rename_attrs__: ClassVar[Dict[str, str]] = {
        'Name': 'name',
        'Age': 'age',
        '123#$^%^%*': 'attr_123'
    }

    # This way of writing is also acceptable.
    # __rename_attrs__ = {
    #     'Name': 'name',
    #     'Age': 'age',
    #     '123#$^%^%*': 'attr_123',
    # }


if __name__ == '__main__':
    s = '{"Name": "foo", "Age": 18, "123#$^%^%*": "test rename attrs"}'
    user = User.from_str(s)
    assert user.to_str() == s

Automatically generate DataClass

See the datclass command for details.

$ datclass -h

Example 1

Input user.json

{
  "Name": "foo",
  "Age": 18,
  "123#$^%^%*": "test rename attrs"
}

Execute the command

$ datclass -o user.py user.json

Output user.py

from dataclasses import dataclass

from datclass import DatClass


@dataclass
class Object(DatClass):
    a_123: str = None  # rename from '123#$^%^%*'
    age: int = None  # rename from 'Age'
    name: str = None  # rename from 'Name'

    __rename_attrs__ = {
        'Name': 'name',
        'Age': 'age',
        '123#$^%^%*': 'a_123',
    }

欢迎反馈和建议

Feedback and suggestions welcome

https://github.com/foyoux/datclass/issues/new

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

datclass-0.2.28.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

datclass-0.2.28-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file datclass-0.2.28.tar.gz.

File metadata

  • Download URL: datclass-0.2.28.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for datclass-0.2.28.tar.gz
Algorithm Hash digest
SHA256 1c5643f8caea3439127b1e53c686516e75e814485461e7174ff1f698483082ad
MD5 663daeffe28ca5003d11c52f68a50761
BLAKE2b-256 43b652392616582cf1497ef2589f674911122d75de8f2ef4a04e52583d2e113c

See more details on using hashes here.

File details

Details for the file datclass-0.2.28-py3-none-any.whl.

File metadata

  • Download URL: datclass-0.2.28-py3-none-any.whl
  • Upload date:
  • Size: 12.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.7

File hashes

Hashes for datclass-0.2.28-py3-none-any.whl
Algorithm Hash digest
SHA256 3ff2aa3d20e599a98664a496d978ffa105d3c0765cf02ad13d0e5e2c266ae538
MD5 4902a0b162d078e9f26bd1d4cae5bca4
BLAKE2b-256 4332feac5c9ceda0fa544bfe9341498d73e4d664a91b78c3e27a855f26aa6687

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