Skip to main content

Immutable data structures.

Project description

Package Version PyPI - Status Tests Status Code Coverage Percentage Project License Python Versions

🔒 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 📦

Method: Install Python Package

Install via uv pip command:

uv pip install frigid

Or, install via pip:

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.

Contribution 🤝

Contribution to this project is welcome! However, it must follow the code of conduct for the project.

Please file bug reports and feature requests in the issue tracker or submit pull requests to improve the source code or documentation.

For development guidance and standards, please see the development guide.

Additional Indicia

GitHub last commit Copier Hatch pre-commit Pyright Ruff PyPI - Implementation PyPI - Wheel

Other Projects by This Author 🌟

  • python-absence (absence on PyPI)

    🕳️ A Python library package which provides a sentinel for absent values - a falsey, immutable singleton that represents the absence of a value in contexts where None or False may be valid values.

  • python-accretive (accretive on PyPI)

    🌌 A Python library package which provides accretive data structures - collections which can grow but never shrink.

  • python-classcore (classcore on PyPI)

    🏭 A Python library package which provides foundational class factories and decorators for providing classes with attributes immutability and concealment and other custom behaviors.

  • python-dynadoc (dynadoc on PyPI)

    📝 A Python library package which bridges the gap between rich annotations and automatic documentation generation with configurable renderers and support for reusable fragments.

  • python-falsifier (falsifier on PyPI)

    🎭 A very simple Python library package which provides a base class for falsey objects - objects that evaluate to False in boolean contexts.

  • python-icecream-truck (icecream-truck on PyPI)

    🍦 Flavorful Debugging - A Python library which enhances the powerful and well-known icecream package with flavored traces, configuration hierarchies, customized outputs, ready-made recipes, and more.

  • python-mimeogram (mimeogram on PyPI)

    📨 A command-line tool for exchanging collections of files with Large Language Models - bundle multiple files into a single clipboard-ready document while preserving directory structure and metadata… good for code reviews, project sharing, and LLM interactions.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

frigid-4.2.tar.gz (20.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

frigid-4.2-py3-none-any.whl (28.4 kB view details)

Uploaded Python 3

File details

Details for the file frigid-4.2.tar.gz.

File metadata

  • Download URL: frigid-4.2.tar.gz
  • Upload date:
  • Size: 20.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for frigid-4.2.tar.gz
Algorithm Hash digest
SHA256 12e1eebe00fed962dd43aaa4e8b9aa767d81eb2924fc6e106133b686304c13b7
MD5 96ea23fae6b7c7cb163ac028a0995e3d
BLAKE2b-256 4b47d3bc0075fee283ddd212867e4dd4fcc9220f8192f66f9da6245721bdc00c

See more details on using hashes here.

Provenance

The following attestation bundles were made for frigid-4.2.tar.gz:

Publisher: releaser.yaml on emcd/python-frigid

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file frigid-4.2-py3-none-any.whl.

File metadata

  • Download URL: frigid-4.2-py3-none-any.whl
  • Upload date:
  • Size: 28.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for frigid-4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3b454bad4402198ea290d26ee0cc746080a7522f51fd38a1b8281ab432415516
MD5 7d1e4fb89ab1b8261c4fcbdf807fb7fb
BLAKE2b-256 9314f447b81a5ce58e408af1290a4b54e223d4bc09d269b55f8d37b7fde5a90f

See more details on using hashes here.

Provenance

The following attestation bundles were made for frigid-4.2-py3-none-any.whl:

Publisher: releaser.yaml on emcd/python-frigid

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page