No project description provided
Project description
tri.named_struct
tri.named_struct supplies a class that can be used like dictionaries, but with a predefined set of possible key values:
Example
from tri.named_struct import NamedStruct
class MyNamedStruct(NamedStruct):
foo = NamedStructField()
bar = NamedStructField()
m = MyNamedStruct(17, 42)
assert m['foo'] == 17
assert m.foo == 17
assert m == dict(foo=17, bar=42)
m.not_foo # Will raise an AttributeError
Default values can be provided:
from tri.named_struct import NamedStruct
class MyNamedStruct(NamedStruct):
foo = NamedStructField()
bar = NamedStructField()
baz = NamedStructField(default='default')
assert MyNamedStruct(17) == dict(foo=17, bar=None, baz='default')
There is also a functional way to defined a NamedStruct
subclass:
from tri.named_struct import named_struct
MyNamedStruct = named_struct('foo, bar')
m = MyNamedStruct(17, 42)
assert m.foo == 17
assert m.bar == 42
Running tests
You need tox installed then just make test
.
License
BSD
Documentation
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
tri.named_struct-0.3.0.tar.gz
(4.9 kB
view hashes)
Built Distribution
Close
Hashes for tri.named_struct-0.3.0-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fd20bf6252b0e7d305b5e3e3959f840883b97f5132d1f692e7addcc59ef73900 |
|
MD5 | d1a32af121c6c72fa576b2977286f1ba |
|
BLAKE2b-256 | 68066e281135e0afb4e8d97ad88b9d9d880e0963ffea9b78f9b07bc33fd92245 |