Immutable data structures.
Project description
🔒 A Python library package which provides immutable data structures - collections which cannot be modified after creation.
Key Features ⭐
📖 Immutable Dictionary: Like a regular dict, but entries cannot be modified or removed. Also has variant for validation on initialization. And provides set operations not found on MappingProxyType.
🗃️ Immutable Namespace: Similar to SimpleNamespace, but attributes are immutable from creation.
🧱 Additional Types: Classes (including abstract base classes), modules, and objects with immutable behavior.
🏗️ Flexible Initialization: Support for unprotected attributes during initialization; useful for compatibility with class decorators, such as dataclasses.
🔓 Flexible Mutability: Support for declaring specific attributes as mutable, enabling selective modification while maintaining immutability for other attributes.
Installation 📦
pip install frigid
Note on Immutability 📢
Enforcement of immutability is quite difficult in Python. While this library enforces 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.
Examples 💡
Immutable Namespaces 🗃️
An immutable 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 frigid import Namespace >>> ns = Namespace( apples = 12, bananas = 6 ) >>> ns.cherries = 42 # ❌ Attempted assignment raises error. Traceback (most recent call last): ... frigid.exceptions.AttributeImmutability: Could not assign or delete attribute 'cherries'. >>> del ns.apples # ❌ Attempted deletion raises error. Traceback (most recent call last): ... frigid.exceptions.AttributeImmutability: Could not assign or delete attribute 'apples'. >>> ns frigid.namespaces.Namespace( apples = 12, bananas = 6 )
Immutable Dictionaries 📖
An immutable 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 frigid import Dictionary
>>> dct = Dictionary( apples = 12, bananas = 6)
>>> dct['cherries'] = 42 # ❌ Attempted assignment raises error.
Traceback (most recent call last):
...
frigid.exceptions.EntryImmutability: Cannot assign entry for 'cherries'.
>>> del dct['bananas'] # ❌ Attempted removal raises error.
Traceback (most recent call last):
...
frigid.exceptions.EntryImmutability: Cannot delete entry for 'bananas'.
>>> dct
frigid.dictionaries.Dictionary( {'apples': 12, 'bananas': 6} )
Immutable Objects 🧱
The with_standard_behaviors decorator can be applied to any class to make its instances fully immutable after initialization.
>>> from frigid import with_standard_behaviors >>> @with_standard_behaviors( ) ... class Config: ... def __init__( self, debug = False ): ... self.debug = debug ... >>> config = Config( debug = True ) >>> config.verbose = True # ❌ Attempted addition raises error Traceback (most recent call last): ... frigid.exceptions.AttributeImmutability: Could not assign or delete attribute 'verbose'. >>> config.debug = False # ❌ Attempted reassignment raises error Traceback (most recent call last): ... frigid.exceptions.AttributeImmutability: Could not assign or delete attribute 'debug'.
Use Cases 🎯
🔒 Configuration Objects: Objects which must maintain consistent state throughout program execution.
📊 Value Objects: Objects which represent values and should be immutable, like numbers or strings.
🧱 Immutable Collections: Many scenarios requiring collections with complete immutability guarantees.
More Flair
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file frigid-4.1.tar.gz.
File metadata
- Download URL: frigid-4.1.tar.gz
- Upload date:
- Size: 19.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
aabfa77d2004c2998f3af4aebadcb629485d55c999e172eef1fedc7a176c7164
|
|
| MD5 |
cef7738bfc7b7bc65778490b36a8b5cd
|
|
| BLAKE2b-256 |
975e84a6bb035bdd7f716eddafd91530ca4a31f0de6c4ffc96c51f9bbc8659d4
|
Provenance
The following attestation bundles were made for frigid-4.1.tar.gz:
Publisher:
releaser.yaml on emcd/python-frigid
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
frigid-4.1.tar.gz -
Subject digest:
aabfa77d2004c2998f3af4aebadcb629485d55c999e172eef1fedc7a176c7164 - Sigstore transparency entry: 259878026
- Sigstore integration time:
-
Permalink:
emcd/python-frigid@7cc0b490ba39032bd34b03c14e25a1877004c52a -
Branch / Tag:
refs/tags/v4.1 - Owner: https://github.com/emcd
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
releaser.yaml@7cc0b490ba39032bd34b03c14e25a1877004c52a -
Trigger Event:
push
-
Statement type:
File details
Details for the file frigid-4.1-py3-none-any.whl.
File metadata
- Download URL: frigid-4.1-py3-none-any.whl
- Upload date:
- Size: 28.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
fefcdf9f6befb61d20688a7de3a1257e06456ceac82742fa54282639de5a62b6
|
|
| MD5 |
3fd8844152d8aef993d62bd9a6f7f4f8
|
|
| BLAKE2b-256 |
bfa0185101091e53ad9430ad22ae417721762747593160968e7ae75795bebc5e
|
Provenance
The following attestation bundles were made for frigid-4.1-py3-none-any.whl:
Publisher:
releaser.yaml on emcd/python-frigid
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
frigid-4.1-py3-none-any.whl -
Subject digest:
fefcdf9f6befb61d20688a7de3a1257e06456ceac82742fa54282639de5a62b6 - Sigstore transparency entry: 259878031
- Sigstore integration time:
-
Permalink:
emcd/python-frigid@7cc0b490ba39032bd34b03c14e25a1877004c52a -
Branch / Tag:
refs/tags/v4.1 - Owner: https://github.com/emcd
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
releaser.yaml@7cc0b490ba39032bd34b03c14e25a1877004c52a -
Trigger Event:
push
-
Statement type: