Typed, declarative data structures
Project description
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file tdds-1.0.tar.gz
.
File metadata
- Download URL: tdds-1.0.tar.gz
- Upload date:
- Size: 35.7 kB
- Tags: Source
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 978ebefbfce9b977b720e0b225c0e675e087d2439bcc66d90cb9c7964a9d8320 |
|
MD5 | a426d095d8a7d11f6d28448efae40c42 |
|
BLAKE2b-256 | 916c32c2af5b519cbdc9fc3fbea3b662f78718d26ecdcd8a709a9ab0c4e3dfce |
File details
Details for the file tdds-1.0-py3-none-any.whl
.
File metadata
- Download URL: tdds-1.0-py3-none-any.whl
- Upload date:
- Size: 45.8 kB
- Tags: Python 3
- Uploaded using 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
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a4741fbf7f19f33e785e427819673c84b776e10b68108a57820f3e3cbbf60d3b |
|
MD5 | fdf426854fa1d13cc996af69d68705c0 |
|
BLAKE2b-256 | cfdad0a5064c5b44eaf0a98233fe2bd35ac6bcf58ae409077a1a85231c0c0d6f |