itypes
Basic immutable container types for Python.
A simple implementation that's designed for simplicity over performance.
Use these in circumstances where it may result in more comprehensible code, or when you want to create custom types with restricted, immutable interfaces.
For an alternative implementation designed for performance, please see pyrsistent.
Installation
Install using pip:
pip install itypes
Instantiating dictionaries and lists.
>>> import itypes
>>> d = itypes.Dict({'a': 1, 'b': 2, 'c': 3})
>>> l = itypes.List(['a', 'b', 'c'])
On instantiation, nested types are coerced to immutables.
>>> d = itypes.Dict({'a': 123, 'b': ['a', 'b', 'c']})
>>> d['b']
List(['a', 'b', 'c'])
Assignments and deletions return new copies.
Methods: set(key, value), delete(key)
>>> d2 = d.set('c', 456)
>>> d2
Dict({'a': 123, 'b': ['a', 'b', 'c'], 'c': 456})
>>> d3 = d2.delete('a')
>>> d3
Dict({'b': ['a', 'b', 'c'], 'c': 456})
Standard assignments and deletions fail.
>>> d['z'] = 123
TypeError: 'Dict' object doesn't support item assignment
>>> del(d['c'])
TypeError: 'Dict' object doesn't support item deletion
Nested lookups.
Method: get_in(keys, default=None)
>>> d['b'][-1]
'c'
>>> d['b'][5]
IndexError: list index out of range
>>> d.get_in(['b', -1])
'c'
>>> d.get_in(['b', 5])
None
Nested assignments and deletions.
Methods: set_in(keys, value), delete_in(keys)
>>> d2 = d.set_in(['b', 1], 'xxx')
>>> d2
Dict({'a': 123, 'b': ['a', 'xxx', 'c']})
>>> d3 = d2.delete_in(['b', 0])
>>> d3
Dict({'a': 123, 'b': ['xxx', 'c']})
Equality works against standard types.
>>> d = itypes.Dict({'a': 1, 'b': 2, 'c': 3})
>>> d == {'a': 1, 'b': 2, 'c': 3}
True
Objects are hashable.
>>> hash(d)
277752239
Shortcuts for switching between mutable and immutable types.
Functions: to_mutable(instance), to_immutable(value)
>>> value = itypes.to_mutable(d)
>>> value
{'a': 123, 'b': ['a', 'b', 'c']}
>>> itypes.to_immutable(value)
Dict({'a': 123, 'b': ['a', 'b', 'c']})
Subclassing.
Only private attribute names may be set on instances. Use @property for attribute access.
Define a .clone(self, data) method if objects have additional state.
Example:
class Configuration(itypes.Dict):
def __init__(self, title, *args, **kwargs):
self._title = title
super(Configuration, self).__init__(*args, **kwargs)
@property
def title(self):
return self._title
def clone(self, data):
return Configuration(self._title, data)
Using the custom class:
>>> config = Configuration('worker-process', {'hostname': 'example.com', 'dynos': 4})
>>> config.title
'worker-process'
>>> new = config.set('dynos', 2)
>>> new
Configuration({'dynos': 2, 'hostname': 'example.com'})
>>> new.title
'worker-process'
Custom immutable objects.
Subclass itypes.Object for an object that prevents setting public attributes.
>>> class Custom(itypes.Object):
... pass
Only private attribute names may be set on instances. Use @property for attribute access.
>>> class Document(itypes.Object):
... def __init__(self, title, content):
... self._title = title
... self._content = title
... @property
... def title(self):
... return self._title
... @property
... def content(self):
... return self._content
Using immutable objects:
>>> doc = Document(title='Immutability', content='For simplicity')
>>> doc.title
'Immutability'
>>> doc.title = 'Changed'
TypeError: 'Document' object doesn't support property assignment.
Metadata
Release files for itypes 1.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| itypes-1.2.0.tar.gz | 4.4 kB | Without provenance | Details for itypes-1.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| itypes-1.2.0-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details for itypes-1.2.0-py2.py3-none-any.whl |
Total release size: 9.1 kB
Release files / itypes-1.2.0.tar.gz
| Download URL | itypes-1.2.0.tar.gz |
|---|---|
| Size | 4.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
af886f129dea4a2a1e3d36595a2d139589e4dd287f5cab0b40e799ee81570ff1
|
|
BLAKE2b-256 checksum How to use checksums |
0e53764524b3907d0af00523f8794daca181c08ca7cb32ceee25a0754d5e63a5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2
|
Release files / itypes-1.2.0-py2.py3-none-any.whl
| Download URL | itypes-1.2.0-py2.py3-none-any.whl |
|---|---|
| Size | 4.8 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
03da6872ca89d29aef62773672b2d408f490f80db48b23079a4b194c86dd04c6
|
|
BLAKE2b-256 checksum How to use checksums |
3fbb3bd99c7cd34d4a123b2903e16da364f6d2078b1c3a3530a8ad105c668104
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.1.3 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.8.2
|
Security for itypes 1.2.0
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.