Skip to main content

No project description provided

Project description

https://travis-ci.org/TriOptima/tri.named_struct.svg?branch=master http://codecov.io/github/TriOptima/tri.named_struct/coverage.svg?branch=master

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

http://trinamedstruct.readthedocs.org.

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

tri.named_struct-0.3.0.tar.gz (4.9 kB view hashes)

Uploaded Source

Built Distribution

tri.named_struct-0.3.0-py2.py3-none-any.whl (4.8 kB view hashes)

Uploaded Python 2 Python 3

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