# ❄️ cryostasis ❄️ -- Freeze arbitrary Python objects
## Summary
`cryostasis` is a package that allows to turn arbitrary Python objects immutable.
The package is very lightweight and does not have any dependencies.
It offers the `freeze` and `deepfreeze` functions.
When an object is frozen any modification to its attributes or items (i.e. assignment or deletion using the []-operator) will raise an `ImmutableError`.
All existing attributes, methods and items will still be accessible on the frozen instance though.
You can think of using `cryostasis.freeze` as a more thorough variant of `dataclass(frozen=True)`:
- unlike the `dataclass` decorator, `freeze` can also be used on instances of builtin types such as lists or dictionaries
- also unlike the `dataclass`decorator, `deepfreeze` will freeze the instance and all of its attributes and items recursively
Frozen instances can be reverted back to their original state using the `thaw` and `deepthaw` functions.
## Use Cases
As I already alluded to, you can use `freeze` / `deepfreeze` wherever you would have used a frozen dataclass but want to be a bit more thorough.
More generally, `cryostasis` can be used in any scenario in which a central mutable instance is passed around to multiple sites.
This could be, for example, a configuration-driven application, where you want to make sure that no site accidentally modifies the central configuration from their local scope.
## Examples
`freeze` works by freezing an object in-place. For convenience, `freeze` also return a reference to the object.
Attributes and items remain accessible but any modifications to them will raise an `cryostasis.ImmutableError`.
On builtin, mutable types (e.g. `list`) the methods that modify the internal state of the instance also raise the same exception.
```python
from cryostasis import freeze
class Dummy:
def __init__(self, value):
self.value = value
d = Dummy(value=5)
d.value = 42 # ok
freeze(d)
d.value = 9001 # raises ImmutableError
del d.value # raises ImmutableError
l = freeze([1,2,3])
l[0] # ok -- returns 1
l[0] = 5 # raises ImmutableError
del l[0] # raises ImmutableError
l.append(42) # raises ImmutableError
```
`deepfreeze` works the same way (it calls `freeze` internally) except that is recurses through the instances attributes and items, freezing all of them while traversing.
```python
from cryostasis import deepfreeze
class Dummy:
def __init__(self, value):
self.value = value
self.a_dict = dict(a=1, b=2, c=[])
d = Dummy(value=[1,2,3])
deepfreeze(d)
d.value # ok -- returns <Frozen([1,2,3])>
d.value = 9001 # raises ImmutableError
del d.value # raises ImmutableError
d.value[0] # ok -- returns 1
d.value[0] = 42 # raises ImmutableError
del d.value[0] # raises ImmutableError
d.a_dict['c'] # ok -- returns <Frozen([])>
d.a_dict['c'].append(0) # raises ImmutableError
```
## Report Issues
This project lives on [GitHub](https://github.com/IljaManakov/cryostasis).
If you encounter any issues or have feature requests, please open an issue using the project's [Issues](https://github.com/IljaManakov/cryostasis/issues) tab.
Raw data
{
"_id": null,
"home_page": null,
"name": "cryostasis",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": "static, dataclasses, frozen, constant, const",
"author": null,
"author_email": "Ilja Manakov <ilja.manakov@gmx.de>",
"download_url": null,
"platform": null,
"description": "# \u2744\ufe0f cryostasis \u2744\ufe0f -- Freeze arbitrary Python objects\r\n\r\n\r\n## Summary\r\n\r\n`cryostasis` is a package that allows to turn arbitrary Python objects immutable.\r\nThe package is very lightweight and does not have any dependencies.\r\nIt offers the `freeze` and `deepfreeze` functions.\r\nWhen an object is frozen any modification to its attributes or items (i.e. assignment or deletion using the []-operator) will raise an `ImmutableError`.\r\nAll existing attributes, methods and items will still be accessible on the frozen instance though.\r\n\r\nYou can think of using `cryostasis.freeze` as a more thorough variant of `dataclass(frozen=True)`:\r\n- unlike the `dataclass` decorator, `freeze` can also be used on instances of builtin types such as lists or dictionaries\r\n- also unlike the `dataclass`decorator, `deepfreeze` will freeze the instance and all of its attributes and items recursively\r\n\r\nFrozen instances can be reverted back to their original state using the `thaw` and `deepthaw` functions.\r\n\r\n## Use Cases\r\n\r\nAs I already alluded to, you can use `freeze` / `deepfreeze` wherever you would have used a frozen dataclass but want to be a bit more thorough.\r\nMore generally, `cryostasis` can be used in any scenario in which a central mutable instance is passed around to multiple sites.\r\nThis could be, for example, a configuration-driven application, where you want to make sure that no site accidentally modifies the central configuration from their local scope.\r\n\r\n## Examples\r\n\r\n`freeze` works by freezing an object in-place. For convenience, `freeze` also return a reference to the object.\r\nAttributes and items remain accessible but any modifications to them will raise an `cryostasis.ImmutableError`.\r\nOn builtin, mutable types (e.g. `list`) the methods that modify the internal state of the instance also raise the same exception.\r\n\r\n```python\r\nfrom cryostasis import freeze\r\n\r\nclass Dummy:\r\n def __init__(self, value):\r\n self.value = value\r\n\r\nd = Dummy(value=5)\r\nd.value = 42 # ok\r\nfreeze(d)\r\nd.value = 9001 # raises ImmutableError\r\ndel d.value # raises ImmutableError\r\n\r\nl = freeze([1,2,3])\r\nl[0] # ok -- returns 1\r\nl[0] = 5 # raises ImmutableError\r\ndel l[0] # raises ImmutableError\r\nl.append(42) # raises ImmutableError\r\n```\r\n\r\n`deepfreeze` works the same way (it calls `freeze` internally) except that is recurses through the instances attributes and items, freezing all of them while traversing.\r\n\r\n```python\r\nfrom cryostasis import deepfreeze\r\n\r\nclass Dummy:\r\n def __init__(self, value):\r\n self.value = value\r\n self.a_dict = dict(a=1, b=2, c=[])\r\n\r\nd = Dummy(value=[1,2,3])\r\ndeepfreeze(d)\r\nd.value # ok -- returns <Frozen([1,2,3])>\r\nd.value = 9001 # raises ImmutableError\r\ndel d.value # raises ImmutableError\r\nd.value[0] # ok -- returns 1\r\nd.value[0] = 42 # raises ImmutableError\r\ndel d.value[0] # raises ImmutableError\r\nd.a_dict['c'] # ok -- returns <Frozen([])>\r\nd.a_dict['c'].append(0) # raises ImmutableError\r\n```\r\n\r\n## Report Issues\r\n\r\nThis project lives on [GitHub](https://github.com/IljaManakov/cryostasis).\r\nIf you encounter any issues or have feature requests, please open an issue using the project's [Issues](https://github.com/IljaManakov/cryostasis/issues) tab.\r\n",
"bugtrack_url": null,
"license": "MIT License",
"summary": null,
"version": "0.1.0",
"project_urls": {
"Homepage": "https://github.com/IljaManakov/cryostasis",
"Issues": "https://github.com/IljaManakov/cryostasis/issues"
},
"split_keywords": [
"static",
" dataclasses",
" frozen",
" constant",
" const"
],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "1153a07508f98132c231f931a038adee37c0c8ff28adc31b69f2d330c87b5464",
"md5": "36bd97deb2e8152c9decfdc30340aec9",
"sha256": "7387e8b37965dcf337197921379af1b098abe336346e583babf6328db0c2a4ec"
},
"downloads": -1,
"filename": "cryostasis-0.1.0-cp310-cp310-macosx_10_9_universal2.whl",
"has_sig": false,
"md5_digest": "36bd97deb2e8152c9decfdc30340aec9",
"packagetype": "bdist_wheel",
"python_version": "cp310",
"requires_python": null,
"size": 10703,
"upload_time": "2024-10-20T14:41:06",
"upload_time_iso_8601": "2024-10-20T14:41:06.479413Z",
"url": "https://files.pythonhosted.org/packages/11/53/a07508f98132c231f931a038adee37c0c8ff28adc31b69f2d330c87b5464/cryostasis-0.1.0-cp310-cp310-macosx_10_9_universal2.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "62160c02de04e110725401a68fe7dd48eac50bbcc1efe832190885f74dab9d22",
"md5": "a01272c84d51bcc2604d87c88e22232a",
"sha256": "7bed5fb8dbc61bd62185a76a85efa034621341c2dbaa2e9ecbe31300b1eaf892"
},
"downloads": -1,
"filename": "cryostasis-0.1.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_28_x86_64.whl",
"has_sig": false,
"md5_digest": "a01272c84d51bcc2604d87c88e22232a",
"packagetype": "bdist_wheel",
"python_version": "cp310",
"requires_python": null,
"size": 15762,
"upload_time": "2024-10-20T14:41:04",
"upload_time_iso_8601": "2024-10-20T14:41:04.035691Z",
"url": "https://files.pythonhosted.org/packages/62/16/0c02de04e110725401a68fe7dd48eac50bbcc1efe832190885f74dab9d22/cryostasis-0.1.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_28_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "095e02a3f736087cb24aec72e6b4cf4d3fc4de9554bc3da8a1108afa97b83d79",
"md5": "b40767fac2d50c38ac2528d7a9973348",
"sha256": "18ebc3e86b3e674971efa2087e5888a3d56145ccb26f9ff03e7b7d78233afa1e"
},
"downloads": -1,
"filename": "cryostasis-0.1.0-cp311-cp311-macosx_10_9_universal2.whl",
"has_sig": false,
"md5_digest": "b40767fac2d50c38ac2528d7a9973348",
"packagetype": "bdist_wheel",
"python_version": "cp311",
"requires_python": null,
"size": 10752,
"upload_time": "2024-10-20T14:41:06",
"upload_time_iso_8601": "2024-10-20T14:41:06.286581Z",
"url": "https://files.pythonhosted.org/packages/09/5e/02a3f736087cb24aec72e6b4cf4d3fc4de9554bc3da8a1108afa97b83d79/cryostasis-0.1.0-cp311-cp311-macosx_10_9_universal2.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "030912988afde5a6400cbe83ce9e6510b02330b8c752b4699b6c8fd6c3460859",
"md5": "53e886e79c475ea2a1d37f2fbbdc6f3c",
"sha256": "b3ce3f74d7252dc3c490bd46d7dc71669ce92aca063b27aa1b8301d2ae8ddacf"
},
"downloads": -1,
"filename": "cryostasis-0.1.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_28_x86_64.whl",
"has_sig": false,
"md5_digest": "53e886e79c475ea2a1d37f2fbbdc6f3c",
"packagetype": "bdist_wheel",
"python_version": "cp311",
"requires_python": null,
"size": 15919,
"upload_time": "2024-10-20T14:41:15",
"upload_time_iso_8601": "2024-10-20T14:41:15.255044Z",
"url": "https://files.pythonhosted.org/packages/03/09/12988afde5a6400cbe83ce9e6510b02330b8c752b4699b6c8fd6c3460859/cryostasis-0.1.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_28_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "41602865d352eb503c689d77d6cc2e3aab472f9e9efe16721d5344f61b971893",
"md5": "f94948b475ce7328a7d651d63ef1c74e",
"sha256": "6421007ffc500d800d6bad40407236e2e98b7d28eef81e8dc5133e6b619436be"
},
"downloads": -1,
"filename": "cryostasis-0.1.0-cp311-cp311-win_amd64.whl",
"has_sig": false,
"md5_digest": "f94948b475ce7328a7d651d63ef1c74e",
"packagetype": "bdist_wheel",
"python_version": "cp311",
"requires_python": null,
"size": 12728,
"upload_time": "2024-10-20T14:41:02",
"upload_time_iso_8601": "2024-10-20T14:41:02.858609Z",
"url": "https://files.pythonhosted.org/packages/41/60/2865d352eb503c689d77d6cc2e3aab472f9e9efe16721d5344f61b971893/cryostasis-0.1.0-cp311-cp311-win_amd64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "d48cd5f6d3bc4e343b96d7f337329fbbdeb4fccad59e66636b678aaf8de57c45",
"md5": "7e429896354ff398d7007e5743d6c377",
"sha256": "5f189dbcb558b23908e532186b1538134f20e3849037b3f1837e947b57227c82"
},
"downloads": -1,
"filename": "cryostasis-0.1.0-cp312-cp312-macosx_10_13_universal2.whl",
"has_sig": false,
"md5_digest": "7e429896354ff398d7007e5743d6c377",
"packagetype": "bdist_wheel",
"python_version": "cp312",
"requires_python": null,
"size": 10816,
"upload_time": "2024-10-20T14:41:08",
"upload_time_iso_8601": "2024-10-20T14:41:08.852308Z",
"url": "https://files.pythonhosted.org/packages/d4/8c/d5f6d3bc4e343b96d7f337329fbbdeb4fccad59e66636b678aaf8de57c45/cryostasis-0.1.0-cp312-cp312-macosx_10_13_universal2.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "3583de886ae4c46db1b2f0ae423499f785f4de1dccd39216a7b4cc4f184f1943",
"md5": "0ddcc931e7693414cdc9a16908971b44",
"sha256": "3a3f351ada298ad3d2277d94efa6ce12cf9418fd52950eb148af0d0c469f8eab"
},
"downloads": -1,
"filename": "cryostasis-0.1.0-cp312-cp312-win_amd64.whl",
"has_sig": false,
"md5_digest": "0ddcc931e7693414cdc9a16908971b44",
"packagetype": "bdist_wheel",
"python_version": "cp312",
"requires_python": null,
"size": 12752,
"upload_time": "2024-10-20T14:41:06",
"upload_time_iso_8601": "2024-10-20T14:41:06.229059Z",
"url": "https://files.pythonhosted.org/packages/35/83/de886ae4c46db1b2f0ae423499f785f4de1dccd39216a7b4cc4f184f1943/cryostasis-0.1.0-cp312-cp312-win_amd64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "e70fcaeda8218a12a301a791b51aed2c037d5f2393f06bba0d618b44565f86cf",
"md5": "315af2eeba1d72e457fdc97c5052f181",
"sha256": "648348b23f66576d7ec9e8cb4dd7540bb15e22188abb8ca657fd760c26049de7"
},
"downloads": -1,
"filename": "cryostasis-0.1.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_28_x86_64.whl",
"has_sig": false,
"md5_digest": "315af2eeba1d72e457fdc97c5052f181",
"packagetype": "bdist_wheel",
"python_version": "cp39",
"requires_python": null,
"size": 15591,
"upload_time": "2024-10-20T14:41:06",
"upload_time_iso_8601": "2024-10-20T14:41:06.203564Z",
"url": "https://files.pythonhosted.org/packages/e7/0f/caeda8218a12a301a791b51aed2c037d5f2393f06bba0d618b44565f86cf/cryostasis-0.1.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_28_x86_64.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "da865bdaef19bb37c717ae1c57ed3f738517863d549f4af63b09fe0afec12a90",
"md5": "7bc03d3f798207dbc6cbf0851d2fd2e4",
"sha256": "0ea4531cb471279863bffa3f5d5d7a9348df59f8fbfe15a28fc197f2be9b75c4"
},
"downloads": -1,
"filename": "cryostasis-0.1.0-cp39-cp39-win_amd64.whl",
"has_sig": false,
"md5_digest": "7bc03d3f798207dbc6cbf0851d2fd2e4",
"packagetype": "bdist_wheel",
"python_version": "cp39",
"requires_python": null,
"size": 12717,
"upload_time": "2024-10-20T14:41:05",
"upload_time_iso_8601": "2024-10-20T14:41:05.332701Z",
"url": "https://files.pythonhosted.org/packages/da/86/5bdaef19bb37c717ae1c57ed3f738517863d549f4af63b09fe0afec12a90/cryostasis-0.1.0-cp39-cp39-win_amd64.whl",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-10-20 14:41:06",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "IljaManakov",
"github_project": "cryostasis",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"tox": true,
"lcname": "cryostasis"
}