Skip to main content

bunch container objects like Matlab structs

Project description

wield.bunch

Utility classes providing "Bunch" containers. These are dictionaries with attribute access of the elements. They wrap any lower level dictionaries they output, so the interface mimics the "struct" container from Matlab.

Bunch

The Bunch class is a lightweight wrapper for dictionaries to allow attribute access as a means to access elements with less syntax. Bunch does not copy assigned dictionaries, it wraps them. This is unlike some other implementations such as gwinc.Struct.

d = dict(A=1, B=2, d2=dict(C=1))

b1 = Bunch(d)
print(b1.A)
print(b1.d2.C)

or

b2 = dict(A=1, B=2, d2=dict(C=1))
print(b2.A)
print(b2.d2.C)

A useful pattern while developing and debugging code, particularly while refactoring large blocks of code into functions is

def code_block(arg1, arg2, arg3, ...):
    ...
    a = 1
    b = 2
    ...
    return Bunch(locals())
    
ret = code_block(...)
ret.a

which is a lightweight way to access elements from the code_block that is promoted into a function.

DeepBunch

There are a collection of more advanced containers DeepBunch allows speculative access of elements, such that if it is missing an attribute, a temporary is created such that

A = DeepBunch()
A.B.C.D = 1

is acceptable.

and

if A.B.C.E:
  raise Exception('The above test evaluates to False')

and in this case, no intermediate dictionaries are actually created until a value is assigned to a leaf.

HDFDeepBunch

h5py is not a required dependency of wield.bunch, but if it is installed then one can import the HDFDeepBunch, which provides a similar interface to the DeepBunch, but uses HDF5 files as a back-end, with internal handling of numpy arrays.

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

wield.bunch-0.9.9.tar.gz (23.1 kB view details)

Uploaded Source

Built Distribution

wield.bunch-0.9.9-py3-none-any.whl (28.5 kB view details)

Uploaded Python 3

File details

Details for the file wield.bunch-0.9.9.tar.gz.

File metadata

  • Download URL: wield.bunch-0.9.9.tar.gz
  • Upload date:
  • Size: 23.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/0.0.0 pkginfo/1.8.2 readme-renderer/35.0 requests/2.25.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.64.0 importlib-metadata/4.11.4 keyring/23.5.1 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.13

File hashes

Hashes for wield.bunch-0.9.9.tar.gz
Algorithm Hash digest
SHA256 855fc2883c0eb14aebefe14c4374e52fdf419870499561cf5c37ad139a58778c
MD5 8bc7644e84c81ce738b8096a6ccdaa84
BLAKE2b-256 52b7c2f9fb205eb5008184d564b4f7654a87dbb366f76d2b0dd6e4ffd139a680

See more details on using hashes here.

File details

Details for the file wield.bunch-0.9.9-py3-none-any.whl.

File metadata

  • Download URL: wield.bunch-0.9.9-py3-none-any.whl
  • Upload date:
  • Size: 28.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/0.0.0 pkginfo/1.8.2 readme-renderer/35.0 requests/2.25.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.64.0 importlib-metadata/4.11.4 keyring/23.5.1 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.13

File hashes

Hashes for wield.bunch-0.9.9-py3-none-any.whl
Algorithm Hash digest
SHA256 fa8b350ea8cf4ee4aadfa29ad196b73cae5ae9a18a25f1d960c60554577e2828
MD5 8c182845ccb3e850b9ada4aa722829b3
BLAKE2b-256 a67588f405d339fb6c8e3123a099ec5e67300a97eaea521b706cfb29d45f52d0

See more details on using hashes here.

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