Accretive data structures.
Project description
A Python library package which provides accretive data structures.
Accretive data structures can grow at any time but can never shrink. An accretive dictionary accepts new entries, but cannot have existing entries altered or removed. Similarly, an accretive namespace accepts new attributes, but cannot have existing attributes assigned to new values or deleted.
Accretive data structures are useful as registries, which may be incrementally initialized, but should have immutable state, once initialized. In general, they are a good compromise between the safety of immutability and the convenience of incremental initialization.
In addition to accretive dictionaries (including dictionaries with default entries) and namespaces, this package also provides accretive classes (including abstract base classes), modules, and objects. Subpackages provide variants of all of these with some additional behaviors or constraints. Modules of aliases are also provided to satisfy various import styles and nomenclatural conventions.
Examples
Accretive Namespace
An accretive namespace, similar to types.SimpleNamespace, is available. This namespace can be initialized from multiple iterables and from keyword arguments. (Keyword arguments shown below; see documentation for additional forms of initialization.)
>>> from accretive import Namespace >>> ns = Namespace( apples = 12, bananas = 6, cherries = 42 ) >>> ns accretive.namespaces.Namespace( apples = 12, bananas = 6, cherries = 42 )
Arbitrary attributes can be assigned, as is expected in Python.
>>> ns.blueberries = 96 >>> ns.strawberries = 24 >>> ns accretive.namespaces.Namespace( apples = 12, bananas = 6, cherries = 42, blueberries = 96, strawberries = 24 )
Since the namespace is accretive, attributes cannot be deleted.
>>> del ns.apples Traceback (most recent call last): ... accretive.exceptions.IndelibleAttributeError: Cannot reassign or delete existing attribute 'apples'.
Or reassigned.
>>> ns.apples = 14 Traceback (most recent call last): ... accretive.exceptions.IndelibleAttributeError: Cannot reassign or delete existing attribute 'apples'.
The attributes thus retain their original values.
>>> ns accretive.namespaces.Namespace( apples = 12, bananas = 6, cherries = 42, blueberries = 96, strawberries = 24 )
Accretive Dictionary
An accretive dictionary, similar to dict, is available. This dictionary can be initialized from multiple iterables and from keyword arguments. (Keyword arguments shown below; see documentation for additional forms of initialization.)
>>> from accretive import Dictionary >>> dct = Dictionary( apples = 12, bananas = 6, cherries = 42 ) >>> dct accretive.dictionaries.Dictionary( {'apples': 12, 'bananas': 6, 'cherries': 42} )
Entries can be added to the dictionary after initialization. This includes via a batch operation, such as update, which can accept the same forms of arguments as dictionary initialization.
>>> dct.update( blueberries = 96, strawberries = 24 ) accretive.dictionaries.Dictionary( {'apples': 12, 'bananas': 6, 'cherries': 42, 'blueberries': 96, 'strawberries': 24} )
Since the dictionary is accretive, existing entries cannot be removed.
>>> del dct[ 'bananas' ] Traceback (most recent call last): ... accretive.exceptions.IndelibleEntryError: Cannot update or remove existing entry for 'bananas'.
Or altered.
>>> dct[ 'bananas' ] = 11 Traceback (most recent call last): ... accretive.exceptions.IndelibleEntryError: Cannot update or remove existing entry for 'bananas'.
The entries thus remain unchanged.
>>> dct accretive.dictionaries.Dictionary( {'apples': 12, 'bananas': 6, 'cherries': 42, 'blueberries': 96, 'strawberries': 24} )
Installation
pip install accretive
More Flair
…than the required minimum
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file accretive-1.0rc0.tar.gz
.
File metadata
- Download URL: accretive-1.0rc0.tar.gz
- Upload date:
- Size: 18.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9495b7c1c32e6d4b3bebdc05c66507721adce9cb1ec933817368afaec8b668ee |
|
MD5 | 05554f340d8b1c4ed994aa6a010cec74 |
|
BLAKE2b-256 | ca48b9e91a6b579c960518e340155b8f5b0ea1e2a3ed691a88a4fe7c7b921e47 |
File details
Details for the file accretive-1.0rc0-py3-none-any.whl
.
File metadata
- Download URL: accretive-1.0rc0-py3-none-any.whl
- Upload date:
- Size: 43.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cd46f8d7b85e906f23aba4836bd3475fbbb5ba6d64d3ba546959c75b55ecf125 |
|
MD5 | 22513c7db3a50af1403ece29b951c6ba |
|
BLAKE2b-256 | 3d1ee39c7dffd22a0196dc7c30c327486731628d29f61afe9a10777f65fed43f |