Skip to main content

Build Status PyPI version

This package provides immutable data classes for Python2.7 and Python 3.5+

Synopsis

You declare a class by listing its fields, and the type of each field

>>> from tdds import Field, Record, seq_of

>>> class Track(Record):
...     title = str
...     total_seconds = int
...
...     @property
...     def duration_str(self):
...         return '%d:%02d' % (self.total_seconds / 60, self.total_seconds % 60)

>>> class Album(Record):
...     artist = str
...     title = str
...     year = Field(int, check=lambda value: 1900 < value < 2050)
...     tracks = seq_of(Track)

You instantiate it and access its fields normally

>>> album = Album(
...     artist='Wah-wah',
...     title='Gyroscope',
...     year=2000,
...     tracks=[
...         Track(title="Elevon", total_seconds=209),
...         Track(title="Gear", total_seconds=514),
...         Track(title="Stringer", total_seconds=413),
...         ],
...     )

>>> album.title
'Gyroscope'

>>> album.tracks[0].duration_str
'3:29'

Record objects are immutable, hashable and comparable.

>>> album.artist = 'Schwah-schwah'
Traceback (most recent call last):
  ...
tdds.basics.RecordsAreImmutable: Album objects are immutable

>>> Track(title='Hull', total_seconds=7) == Track(title='Hull', total_seconds=7)
True

>>> Track(title='Hull', total_seconds=7) == Track(title='Hold', total_seconds=8)
False

The constructor checks the type of each of the given values, and refuses to proceed if the types aren't as declared

>>> Track(title='Fireworks', total_seconds='9')
Traceback (most recent call last):
  ...
tdds.basics.FieldTypeError: Track.total_seconds should be of type int, not str ('9')

There are functions to convert to and from Plain Old Data Structures, i.e. just lists and dicts. This is useful e.g. for JSON serialisation.

>>> pods = album.record_pods()

>>> print(json.dumps(pods, indent=4, sort_keys=True))
{
    "artist": "Wah-wah",
    "title": "Gyroscope",
    "tracks": [
        {
            "title": "Elevon",
            "total_seconds": 209
        },
        {
            "title": "Gear",
            "total_seconds": 514
        },
        {
            "title": "Stringer",
            "total_seconds": 413
        }
    ],
    "year": 2000
}

>>> Album.from_pods(album.record_pods()) == album
True

Records are also picklable

>>> pickle.loads(pickle.dumps(album)) == album
True

The library offers many more features such as automatic type coercion, custom validation functions, enum fields, typed collections, and more. See the tests directory for a specification of sorts.

See also

This project is similar in spirit to these other fine libraries:

but this one is mine.

Metadata

Release files for tdds 1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for tdds 1.0
File Size Uploaded Provenance
tdds-1.0.tar.gz 35.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for tdds 1.0
File Interpreter ABI Platform
tdds-1.0-py3-none-any.whl Python 3 none any Details

Total release size: 81.5 kB

Release files / tdds-1.0.tar.gz

Download URL tdds-1.0.tar.gz
Size 35.7 kB
Tags Source
SHA-256 checksum
How to use checksums
978ebefbfce9b977b720e0b225c0e675e087d2439bcc66d90cb9c7964a9d8320
BLAKE2b-256 checksum
How to use checksums
916c32c2af5b519cbdc9fc3fbea3b662f78718d26ecdcd8a709a9ab0c4e3dfce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.2

Release files / tdds-1.0-py3-none-any.whl

Download URL tdds-1.0-py3-none-any.whl
Size 45.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
a4741fbf7f19f33e785e427819673c84b776e10b68108a57820f3e3cbbf60d3b
BLAKE2b-256 checksum
How to use checksums
cfdad0a5064c5b44eaf0a98233fe2bd35ac6bcf58ae409077a1a85231c0c0d6f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/47.1.1 requests-toolbelt/0.9.1 tqdm/4.46.1 CPython/3.8.2

Security for tdds 1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

1.0 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page