accretiveΒΆ
π A Python library package which provides accretive data structures - collections which can grow but never shrink.
Key Features βΒΆ
π Accretive Dictionary: Like a regular dict, but entries cannot be modified or removed once added. Also has variants with defaults and validation.
ποΈ Accretive Namespace: Similar to SimpleNamespace, but attributes become immutable after assignment.
𧱠Additional Types: Classes (including abstract base classes), modules, and objects with accretive behavior.
ποΈ Flexible Initialization: Support for unprotected attributes during initialization; useful for compatibility with class decorators, such as dataclasses.
Note on Immutability π’ΒΆ
Enforcement of immutability is quite difficult in Python. While this library encourages immutability by default, it can be circumvented by anyone who has intermediate knowledge of Python machinery and who is determined to circumvent the immutability. Use the library in the spirit of making programs safer, but understand that it cannot truly prevent unwanted state tampering.
Installation π¦ΒΆ
pip install accretive
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 )
>>> ns.cherries = 42 # β
Allows new attributes.
>>> ns.apples = 14 # β Attempted reassignment raises error.
Traceback (most recent call last):
...
accretive.exceptions.AttributeImmutabilityError: Cannot reassign or delete existing attribute 'apples'.
>>> del ns.apples # β Attempted deletion raises error.
Traceback (most recent call last):
...
accretive.exceptions.AttributeImmutabilityError: Cannot reassign or delete existing attribute 'apples'.
>>> ns
accretive.namespaces.Namespace( apples = 12, bananas = 6, cherries = 42 )
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 )
>>> dct[ 'cherries' ] = 42 # β
Allows new entries.
>>> dct.update( blueberries = 96, strawberries = 24 )
accretive.dictionaries.Dictionary( {'apples': 12, 'bananas': 6, 'cherries': 42, 'blueberries': 96, 'strawberries': 24} )
>>> dct[ 'bananas' ] = 11 # β Attempted alteration raises error.
Traceback (most recent call last):
...
accretive.exceptions.EntryImmutabilityError: Cannot alter or remove existing entry for 'bananas'.
>>> del dct[ 'bananas' ] # β Attempted removal raises error.
Traceback (most recent call last):
...
accretive.exceptions.EntryImmutabilityError: Cannot alter or remove existing entry for 'bananas'.
>>> dct
accretive.dictionaries.Dictionary( {'apples': 12, 'bananas': 6, 'cherries': 42, 'blueberries': 96, 'strawberries': 24} )
Use Cases π―ΒΆ
π Configuration Registries: Registries which can accumulate entries but never remove them, thereby guaranteeing sticky state.
π Plugin Systems: Register extensions which are then guaranteed to be available from the time of registration to the end of the process.
π Immutable Collections: Many scenarios requiring grow-only collections with immutability guarantees.