This is a pre-production deployment of Warehouse, however changes made here WILL affect the production instance of PyPI.
Latest Version Dependencies status unknown Test status unknown Test coverage unknown
Project Description

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).

In [1]: from istruct import istruct

In [2]: 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.

In [3]: p = person(first_name="Jim", last_name="Raynor", middle_name="Eugene")

In [4]: p
Out[4]: 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.

In [5]: p.first_name
Out[5]: 'Jim'

In [6]: p.dob
Out[6]: '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.

In [7]: p.first_name = "James"
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-13-681faedb279a> in <module>()
----> 1 p.first_name = "James"

AttributeError: can't set attribute

In [8]: del p.email
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-12-d5c698764d13> in <module>()
----> 1 del p.email

AttributeError: can't delete attribute

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

In [9]: p = person("Jim", "Raynor")
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-c0bdbd269e9f> in <module>()
----> 1 p = person("Jim", "Raynor")

/home/microamp/devel/projs/istruct/istruct/__init__.py in _istruct(*positional, **attrs)
     49         if positional:
     50             raise TypeError("No positional arguments are allowed in istruct "
---> 51                             "(%d found)" % (len(positional),))
     52
     53         nt = namedtuple(__name__,

TypeError: No positional arguments are allowed in istruct (2 found)

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

In [10]: p = person(last_name="Raynor")
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-15-451d2add9ee8> in <module>()
----> 1 p = person(last_name="Raynor")

/home/microamp/devel/projs/istruct/istruct.py in _istruct(**attrs)
     25     def _istruct(**attrs):
     26         nt = namedtuple(name(), merge_tuples(args, tuple(kwargs.keys())))
---> 27         return nt(**merge_dicts(kwargs, attrs))
     28
     29     return _istruct

TypeError: __new__() missing 1 required positional argument: 'first_name'

Versions Tested

  • Python 2.7
  • Python 3.2
  • Python 3.3
  • Python 3.4
  • PyPy
  • PyPy3

TODO

  • Find ways to annotate types

License

MIT

Release History

Release History

0.1.5

This version

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.1.3

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.1.2

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.1.1

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

0.1

History Node

TODO: Figure out how to actually get changelog content.

Changelog content for this version goes here.

Donec et mollis dolor. Praesent et diam eget libero egestas mattis sit amet vitae augue. Nam tincidunt congue enim, ut porta lorem lacinia consectetur. Donec ut libero sed arcu vehicula ultricies a non tortor. Lorem ipsum dolor sit amet, consectetur adipiscing elit.

Show More

Download Files

Download Files

TODO: Brief introduction on what you do with files - including link to relevant help section.

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
istruct-0.1.5.tar.gz (3.4 kB) Copy SHA256 Checksum SHA256 Source Feb 9, 2015

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS HPE HPE Development Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting