Skip to main content

Python Data Classes

Project description

Python Data Classes support Python 2 and 3 with the human-friendly features.

This project is under active development and has been tested in Python 2.7 and Python 3.6. It is not related to the dataclasses module introduced in Python 3.7.

Feature Support

The core features and tests have been developed. More documents are on the way.

Python Data Classes generally support

  • untyped data, typed data, and typed data list

  • arbitrary attribute names in the dictionary input

  • no python attribute naming conflict or pollution

  • lazy and auto object creation on-the-fly

  • lazy loading of the dict input (default: enabled)

  • synchronous updates of the dict input (default: disabled)

Getting Started

  1. Install the Python Data Classes

pip install pydataclasses
  1. Create the Python Data Classes

from pydataclasses import DataClass


class TT(DataClass):

    attr = None


class ONCE(DataClass):

    data = None


class TWICE(ONCE):

    tear = TT
    tears = [TT]

    def __init__(self, *args, **kwargs):

        super(TWICE, self).__init__(*args, **kwargs)

        self.pair = TWICE
        self.pairs = List[TWICE]
  1. Play with the Python Data Classes

old_data = 'hello'
new_data = 'world'
old_dict = {'data': old_data}
new_dict = {'data': new_data}

# lazy and auto object creation on-the-fly
twice = TWICE()
assert twice.pairs[1].tear.attr is None
twice.pairs[1].tear.attr = old_data
assert old_data == twice.pairs[1].tear.attr

# lazy loading of the dict input (default: enabled)
twice = TWICE(old_dict)
assert old_data == twice.data
assert old_dict == twice.__as_dict__(dict, 0)

# synchronous updates of the dict input (default: disabled)
assert old_dict != new_dict
twice = TWICE(old_dict, __sync__=True)
twice.data = new_data
assert old_dict == new_dict

Best Practices

Please

  • read and refer to pydataclasses.JSONData as an example

  • put a tailored subclass between pydataclasses and your project

  • write (or copy from pydataclasses) as many tests as possible for this subclass

from pydataclasses import DataClass


class ProjectDataClass(DataClass):

    def as_dict(self, dict_class=OrderedDict):
        return self.__as_dict__(dict_class, 0)

Release History

0.0.1 (2018-08-26)

  • Birthday

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

pydataclasses-0.0.1.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydataclasses-0.0.1-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file pydataclasses-0.0.1.tar.gz.

File metadata

  • Download URL: pydataclasses-0.0.1.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.6

File hashes

Hashes for pydataclasses-0.0.1.tar.gz
Algorithm Hash digest
SHA256 4996bd1ec2e4623d75688f59aaf5dfc36e9dfcc8b3650d58587e7221cb1177b3
MD5 be27849023b04c96ebcfcebc6e683c46
BLAKE2b-256 93f56221fc4c0855e5314441dfdfc3c3a22c429b544be271d729e3868b8f7a16

See more details on using hashes here.

File details

Details for the file pydataclasses-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: pydataclasses-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.25.0 CPython/3.6.6

File hashes

Hashes for pydataclasses-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 5426e1df8c3a030d8ca39b97fabfd59f2e378ced5c4d7d93b9daa4d3fb5a5b0e
MD5 d82edaee69fc4bdff884eff2aaf1319a
BLAKE2b-256 bf9d7806c3f7a2e1e853d76aae2b19c1ef451f3c9a03b8e86d2b70fd76ee777e

See more details on using hashes here.

Supported by

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