Skip to main content

Immutable struct built on top of collections.namedtuple with sane defaults

Goals

  • Immutable, dictionary-like data structure (Note: istruct is not an immutable version of the existing struct in Python)

  • Minimal

  • Support required and optional fields (with default values)

  • Strictly disallow positional arguments

Installation

pip install istruct

Quick Start

First, create an istruct object called person where first_name and last_name are required whereas middle_name, dob and email are optional (with default values specified).

>>> from istruct import istruct
>>> person = istruct("first_name", "last_name", middle_name="", dob="2000-01-01", email=None)

Then, create an instance of person with first_name, last_name and middle_name.

>>> p = person(first_name="Jim", last_name="Raynor", middle_name="Eugene")
>>> p
istruct(first_name='Jim', last_name='Raynor', email=None, dob='2000-01-01', middle_name='Eugene')

You can retrieve field values like you would normally do.

>>> p.first_name
'Jim'
>>> p.dob
'2000-01-01'

p is immutable, meaning that it cannot be modified after created. Thus, set/delete operations like below would fail, raising an AttributeError.

>>> p.first_name = "James"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: can't set attribute
>>> del p.email
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: can't delete attribute

istruct only accepts named/keyword arguments. It strictly disallows positional arguments by design.

>>> p = person("Jim", "Raynor")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/microamp/src/microamp/istruct/istruct/__init__.py", line 52, in _istruct
    "(%d found)" % (len(positional),))
TypeError: No positional arguments are allowed in istruct (2 found)

istruct would raise a TypeError when one or more required fields are omitted.

>>> p = person(last_name="Raynor")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/Users/microamp/src/microamp/istruct/istruct/__init__.py", line 56, in _istruct
    return nt(**merge_dicts(kwargs, attrs))
TypeError: __new__() missing 1 required positional argument: 'first_name'

Versions Tested

  • Python 2.7

  • Python 3.2

  • Python 3.3

  • Python 3.4

  • Python 3.5

  • Python 3.6

  • PyPy

  • PyPy3

TODO

  • Find ways to annotate types

License

MIT

Metadata

Release files for istruct 0.1.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for istruct 0.1.6
File Size Uploaded Provenance
istruct-0.1.6.tar.gz 2.8 kB Without provenance Details

Release files / istruct-0.1.6.tar.gz

Download URL istruct-0.1.6.tar.gz
Size 2.8 kB
Tags Source
SHA-256 checksum
How to use checksums
d20576560a3cf26f5a29035f74ed8b4621ad692fdb08ef3a1c07e48112af6782
BLAKE2b-256 checksum
How to use checksums
37a671bd4d1b14a5b6827e12ce66bc8397d4a75d60c5ecfd708af2be1be1df97
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for istruct 0.1.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.6 This release

1 release file No provenance

0.1.3

0.1.2

0.1.1

0.1

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page