Skip to main content

tri.struct supplies classes that can be used like dictionaries and as objects with attribute access at the same time

Project description

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

tri.struct

tri.struct supplies classes that can be used like dictionaries and as objects with attribute access at the same time. There are two versions:

  • Struct: mutable struct

  • FrozenStruct: immutable struct

Some niceties include:

  • Predictable repr() so it’s easy to write tests

  • merged function call to merge different types of dicts into a new: merged(Struct(a=1), FrozenStruct(b=1), c=1) == Struct(a=1, b=1, c=1))

  • Accelerated implementation in c for improved speed. (With python-only fallback reference implementation)

Example

>>> foo = Struct()
>>> foo.a = 1
>>> foo['a']
1
>>> foo['a'] = 2
>>> foo.a
2

Running tests

You need tox installed then just make test.

License

BSD

Documentation

http://tristruct.readthedocs.org.

Changelog

2.5.7 (2018-11-16)

  • Fixed performance issue with Frozen/FrozenStruct: the hash was recalculated on each use instead of cached.

2.5.6 (2018-06-26)

  • Fixed release functionality

2.5.5 (2018-02-20)

  • Fixed segfault in repr when running under Python 3

2.5.4 (2017-06-13)

  • Added DefaultStruct in the spirit of the standard library defaultdict. Also added a to_default_struct for recursively converting dicts recursively.

2.5.3 (2017-02-10)

  • Fix use-after-free when repring a Struct that contains itself more than once.

2.5.2 (2016-04-07)

  • Fix make and tox targets for build and release.

  • Fix lint issues.

2.5.1 (2015-12-15)

  • Bugfix: Fix compilation of the _cstruct module.

2.5.0 (unreleased)

  • Build changes.

2.4.0 (2015-12-08)

  • Improvement: If a Struct subclass implements the __missing__ method, it will not be called before GetAttr on attribute access, but will be called before GetAttr on dict access.

2.3.1 (2015-12-07)

  • Bugfix: Clear KeyError in CStruct getattr before trying GetAttr, otherwise the KeyError may “leak out”. Also, only clear the error and attempt GetAttr if the original error was a KeyError.

2.3.0 (2015-12-02)

  • Add mixin class Frozen to make read-only versions of a dict-derived class (typically a Struct or a subclass there of.)

  • Use the Frozen mixin to implement FrozenStruct

2.2.0 (2015-11-12)

  • Add keyword arguments to merged function.

2.1.2 (2015-11-11)

  • Redo the C implementation to be a “heaptype”, and remove the hack of setting __dict__ = self. Instead, object will control the type storage, letting us “insert” attributes into the object without polluting the dict.

2.0 (unreleased)

  • slim down interface to again match dict

  • add tri.struct.merged function to join structs

  • add optional C implementation to speed up instantiation

1.0 (2015-09-29)

  • Struct with attribute & dict interface

  • __add__ and __or__ to combine structs

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.struct-2.5.7.tar.gz (18.0 kB view details)

Uploaded Source

File details

Details for the file tri.struct-2.5.7.tar.gz.

File metadata

  • Download URL: tri.struct-2.5.7.tar.gz
  • Upload date:
  • Size: 18.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.6.3

File hashes

Hashes for tri.struct-2.5.7.tar.gz
Algorithm Hash digest
SHA256 1ab4804ca6e174e346df3110fbe580a1ce347b646c97542ec5f34861e528936a
MD5 190e16fe9b5d671bd9773bb56e0200ee
BLAKE2b-256 a6d3e2d04e73251850bf7f4d552e38356c20a8555034434a1a7717088f0c2255

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page