Skip to main content

Implements decorator `nested` that adds a parent and children to a dataclass.

Project description

nested-dataclasses

Implements decorator nested that adds a parent and children to a dataclass.

Both parent and child class should be decorated with nested.

Usage:

Example usage of the parent attribute on a nested dataclass:

from dataclasses import dataclass
from typing import List

from nested_dataclasses import nested


@nested
@dataclass
class TextCount:
    text: str
    count: int

    @property
    def fraction(self):
        return self.count / self.parent.total_count

@nested
@dataclass
class TextCounts:
    counts: List[TextCount]

    @property
    def total_count(self):
        return sum(count.count for count in self.counts)

hello = TextCount("hello", 3)
hi = TextCount("hi", 1)
counts = TextCounts([hello, hi])

counts.validate()

print(counts.counts[0].fraction)
print(counts.children)
print(counts.to_dict())

Installation

We can be installed with:

pip install nested-dataclasses

Development installation of this project itself

We're installed with pipenv, a handy wrapper around pip and virtualenv. Install that first with pip install pipenv. Then run:

PIPENV_VENV_IN_PROJECT=1 pipenv install --python 3.8 --dev

In case you do not have python 3.8 on your machine, install python using pyenv and try the previous command again. See install pyenv below for instructions.

In order to get nicely formatted python files without having to spend manual work on it, run the following command periodically:

pipenv run black nested_dataclasses

Run the tests regularly. This also checks with pyflakes, black and it reports coverage. Pure luxury:

pipenv run pytest

If you need a new dependency (like requests), add it in setup.py in install_requires. Afterwards, run install again to actually install your dependency:

pipenv install --dev

Releasing

Pipenv installs zest.releaser which allows you to release the package to a git(hub) repo. It has a fullrelease command that asks you a few questions, which you all respond to with <enter>:

pipenv run fullrelease

Install pyenv

We can install pyenv by running the following commands:

sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev
curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash

Also make sure to put pyenv in your .bashrc or .zshrc as instructed by the previous commands.

Changelog of nested-dataclasses

0.1 (2020-06-08)

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

nested-dataclasses-0.2.tar.gz (5.7 kB view details)

Uploaded Source

Built Distribution

nested_dataclasses-0.2-py3-none-any.whl (6.3 kB view details)

Uploaded Python 3

File details

Details for the file nested-dataclasses-0.2.tar.gz.

File metadata

  • Download URL: nested-dataclasses-0.2.tar.gz
  • Upload date:
  • Size: 5.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for nested-dataclasses-0.2.tar.gz
Algorithm Hash digest
SHA256 f7fd5fca422f6f02477ca606c51032a2e73709559ed0b7cb1c8ece7baff8c827
MD5 d5267890f7c7d6dc305131675fb52343
BLAKE2b-256 679e7601c34f2fd4ca4b77f2383a4278ee0b4c2cd07ecb4af10397185f90e5ba

See more details on using hashes here.

File details

Details for the file nested_dataclasses-0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for nested_dataclasses-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3322a7631c68b43509d2e77f53095f891483b415e7b7d98637389a0506652102
MD5 afbe21dba9aa78d73cba1ebe2da00713
BLAKE2b-256 0857b0c6b98047c8b97dcb171f735d5e59a8e06713575d6e779a56f8329707e4

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