cryostasis


Namecryostasis JSON
Version 0.2.0 PyPI version JSON
download
home_pageNone
SummaryNone
upload_time2025-02-28 22:07:22
maintainerNone
docs_urlNone
authorNone
requires_pythonNone
licenseMIT License
keywords static dataclasses frozen constant const
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # ❄️ 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.2.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": null,
            "digests": {
                "blake2b_256": "2fd15c998d27cbb8c5a0b41895b4f6caffc6da223f5ee76ce2639ea300e99d54",
                "md5": "94d88f64c126ffca92361ea14001d336",
                "sha256": "9c98c0bad193db9809bcc888fd7af546bb2257acaa3d83c3559aaa6fc747c508"
            },
            "downloads": -1,
            "filename": "cryostasis-0.2.0-cp310-cp310-macosx_10_9_universal2.whl",
            "has_sig": false,
            "md5_digest": "94d88f64c126ffca92361ea14001d336",
            "packagetype": "bdist_wheel",
            "python_version": "cp310",
            "requires_python": null,
            "size": 14264,
            "upload_time": "2025-02-28T22:07:22",
            "upload_time_iso_8601": "2025-02-28T22:07:22.846759Z",
            "url": "https://files.pythonhosted.org/packages/2f/d1/5c998d27cbb8c5a0b41895b4f6caffc6da223f5ee76ce2639ea300e99d54/cryostasis-0.2.0-cp310-cp310-macosx_10_9_universal2.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "c1a2757a722aed26465b318d05d3ba2e1e5717ebd8159987dbdb67005fe45bb9",
                "md5": "30d7de488dd30a13406bddad53d8a4b3",
                "sha256": "b5786df4669c25d013256656f804068914dc74e4ae0a505265313f0fb6e3d1a3"
            },
            "downloads": -1,
            "filename": "cryostasis-0.2.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_28_x86_64.whl",
            "has_sig": false,
            "md5_digest": "30d7de488dd30a13406bddad53d8a4b3",
            "packagetype": "bdist_wheel",
            "python_version": "cp310",
            "requires_python": null,
            "size": 19151,
            "upload_time": "2025-02-28T22:07:24",
            "upload_time_iso_8601": "2025-02-28T22:07:24.745512Z",
            "url": "https://files.pythonhosted.org/packages/c1/a2/757a722aed26465b318d05d3ba2e1e5717ebd8159987dbdb67005fe45bb9/cryostasis-0.2.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_28_x86_64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "bd7d41f4f690507a612a8e21e6e4ac3ecd5c920314212791846ba0535a8c96b9",
                "md5": "d21265e23fa4cee84890b718eaaa1f41",
                "sha256": "693c74215b44dd7c783d4d8e6a2bbbcf97a8e499397a1e1b46d77f3815f61798"
            },
            "downloads": -1,
            "filename": "cryostasis-0.2.0-cp310-cp310-win_amd64.whl",
            "has_sig": false,
            "md5_digest": "d21265e23fa4cee84890b718eaaa1f41",
            "packagetype": "bdist_wheel",
            "python_version": "cp310",
            "requires_python": null,
            "size": 16549,
            "upload_time": "2025-02-28T22:07:20",
            "upload_time_iso_8601": "2025-02-28T22:07:20.360470Z",
            "url": "https://files.pythonhosted.org/packages/bd/7d/41f4f690507a612a8e21e6e4ac3ecd5c920314212791846ba0535a8c96b9/cryostasis-0.2.0-cp310-cp310-win_amd64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "ab149f6df6b232bb5e3f60a506178f60a01c5a1ae8b609cb8a73994d8f7e1eb5",
                "md5": "7e8b3a80aa9336ea1e967fc7a36e77ce",
                "sha256": "2b3f234739f560375f845ad32801292bafa31d486495c1e877dd693eaa67f187"
            },
            "downloads": -1,
            "filename": "cryostasis-0.2.0-cp311-cp311-macosx_10_9_universal2.whl",
            "has_sig": false,
            "md5_digest": "7e8b3a80aa9336ea1e967fc7a36e77ce",
            "packagetype": "bdist_wheel",
            "python_version": "cp311",
            "requires_python": null,
            "size": 14321,
            "upload_time": "2025-02-28T22:07:26",
            "upload_time_iso_8601": "2025-02-28T22:07:26.236773Z",
            "url": "https://files.pythonhosted.org/packages/ab/14/9f6df6b232bb5e3f60a506178f60a01c5a1ae8b609cb8a73994d8f7e1eb5/cryostasis-0.2.0-cp311-cp311-macosx_10_9_universal2.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "6b44201a9b0597941d136970a74f3b3730f4238fa754f2b0961162c0cf6ff543",
                "md5": "93681343f5e1cdafab2b54abe93f75ce",
                "sha256": "33ffcc93dee6adba19b645d6b02fd49567492c8a225fe7b70624ceba77281cc3"
            },
            "downloads": -1,
            "filename": "cryostasis-0.2.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_28_x86_64.whl",
            "has_sig": false,
            "md5_digest": "93681343f5e1cdafab2b54abe93f75ce",
            "packagetype": "bdist_wheel",
            "python_version": "cp311",
            "requires_python": null,
            "size": 19291,
            "upload_time": "2025-02-28T22:07:22",
            "upload_time_iso_8601": "2025-02-28T22:07:22.079774Z",
            "url": "https://files.pythonhosted.org/packages/6b/44/201a9b0597941d136970a74f3b3730f4238fa754f2b0961162c0cf6ff543/cryostasis-0.2.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_28_x86_64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "ff15363f941a92a6a041fdbf4bea11708a973c683ec2f157f104266d02c8086e",
                "md5": "0b9ff206635d576d62c6b1cbcac188a8",
                "sha256": "e99e532effea71fe711b78d0cafe1251b198d5e0a3b15ae9d20aa6ef6d8c6c29"
            },
            "downloads": -1,
            "filename": "cryostasis-0.2.0-cp311-cp311-win_amd64.whl",
            "has_sig": false,
            "md5_digest": "0b9ff206635d576d62c6b1cbcac188a8",
            "packagetype": "bdist_wheel",
            "python_version": "cp311",
            "requires_python": null,
            "size": 16565,
            "upload_time": "2025-02-28T22:07:22",
            "upload_time_iso_8601": "2025-02-28T22:07:22.616437Z",
            "url": "https://files.pythonhosted.org/packages/ff/15/363f941a92a6a041fdbf4bea11708a973c683ec2f157f104266d02c8086e/cryostasis-0.2.0-cp311-cp311-win_amd64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "5a5be7925f8cca65446b2e8e9352cc47296d739c3bc5cc0fd0b75876b9170155",
                "md5": "7df3c2f8685f6a2c067d5800f78dc496",
                "sha256": "4e7a61dd06b116917c5b171c18b41a360f7ee9a66efbe2ab95a43c6bf1464bf6"
            },
            "downloads": -1,
            "filename": "cryostasis-0.2.0-cp312-cp312-macosx_10_13_universal2.whl",
            "has_sig": false,
            "md5_digest": "7df3c2f8685f6a2c067d5800f78dc496",
            "packagetype": "bdist_wheel",
            "python_version": "cp312",
            "requires_python": null,
            "size": 14399,
            "upload_time": "2025-02-28T22:07:22",
            "upload_time_iso_8601": "2025-02-28T22:07:22.150470Z",
            "url": "https://files.pythonhosted.org/packages/5a/5b/e7925f8cca65446b2e8e9352cc47296d739c3bc5cc0fd0b75876b9170155/cryostasis-0.2.0-cp312-cp312-macosx_10_13_universal2.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "8fbba225247a8d0609c9de5651e93909f5af65139dca8b5df9c846cd0561e38d",
                "md5": "0f34230079484617e611c091b3aae105",
                "sha256": "0a0b4426f546afaaf5c182cc2e5a7e764ea0072b41079cd954c60d1a84f615af"
            },
            "downloads": -1,
            "filename": "cryostasis-0.2.0-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_28_x86_64.whl",
            "has_sig": false,
            "md5_digest": "0f34230079484617e611c091b3aae105",
            "packagetype": "bdist_wheel",
            "python_version": "cp312",
            "requires_python": null,
            "size": 19607,
            "upload_time": "2025-02-28T22:07:21",
            "upload_time_iso_8601": "2025-02-28T22:07:21.830271Z",
            "url": "https://files.pythonhosted.org/packages/8f/bb/a225247a8d0609c9de5651e93909f5af65139dca8b5df9c846cd0561e38d/cryostasis-0.2.0-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_28_x86_64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "e696248b5b94bc13e6f225512d11fe6892414c72f939a0631b74dd597c995865",
                "md5": "d98c5cca268e30388f9b55114c1811d4",
                "sha256": "12db753fda733711dbde1e68b629b9462da1f606e161ccde491ae509bded4844"
            },
            "downloads": -1,
            "filename": "cryostasis-0.2.0-cp312-cp312-win_amd64.whl",
            "has_sig": false,
            "md5_digest": "d98c5cca268e30388f9b55114c1811d4",
            "packagetype": "bdist_wheel",
            "python_version": "cp312",
            "requires_python": null,
            "size": 16590,
            "upload_time": "2025-02-28T22:07:21",
            "upload_time_iso_8601": "2025-02-28T22:07:21.731230Z",
            "url": "https://files.pythonhosted.org/packages/e6/96/248b5b94bc13e6f225512d11fe6892414c72f939a0631b74dd597c995865/cryostasis-0.2.0-cp312-cp312-win_amd64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "2ca63590e4cd425bae9a9849d2066fd839b0d3e8672784badaeb1fda13b846c7",
                "md5": "9bd25aa8808cfb18dd28b24e56d4de74",
                "sha256": "073680a955640fe72c3438be5cce5a08299d2f0acd50762bc3125c5e3d985b5c"
            },
            "downloads": -1,
            "filename": "cryostasis-0.2.0-cp39-cp39-macosx_10_9_universal2.whl",
            "has_sig": false,
            "md5_digest": "9bd25aa8808cfb18dd28b24e56d4de74",
            "packagetype": "bdist_wheel",
            "python_version": "cp39",
            "requires_python": null,
            "size": 14250,
            "upload_time": "2025-02-28T22:07:22",
            "upload_time_iso_8601": "2025-02-28T22:07:22.931580Z",
            "url": "https://files.pythonhosted.org/packages/2c/a6/3590e4cd425bae9a9849d2066fd839b0d3e8672784badaeb1fda13b846c7/cryostasis-0.2.0-cp39-cp39-macosx_10_9_universal2.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "29b046944ac998c4ba36d6ec32084d966dc15b0073a21226b544f53adbf89784",
                "md5": "f77dccedb599075b5b40bb42da277540",
                "sha256": "ec4aff99ccf09d583321602b10e62afaf6bca32fef79c6968002a513d0b6f4b6"
            },
            "downloads": -1,
            "filename": "cryostasis-0.2.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_28_x86_64.whl",
            "has_sig": false,
            "md5_digest": "f77dccedb599075b5b40bb42da277540",
            "packagetype": "bdist_wheel",
            "python_version": "cp39",
            "requires_python": null,
            "size": 18989,
            "upload_time": "2025-02-28T22:08:30",
            "upload_time_iso_8601": "2025-02-28T22:08:30.509697Z",
            "url": "https://files.pythonhosted.org/packages/29/b0/46944ac998c4ba36d6ec32084d966dc15b0073a21226b544f53adbf89784/cryostasis-0.2.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_28_x86_64.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": null,
            "digests": {
                "blake2b_256": "7526561593c5c2c4ac5a31e641011c1bb79aee1945943623f44224c070ed195e",
                "md5": "5a765eb8741601d60e67f9d3f38f4f6b",
                "sha256": "091984756439e679432edbac162546634f448a1b13e022d44cb2258560c7bb87"
            },
            "downloads": -1,
            "filename": "cryostasis-0.2.0-cp39-cp39-win_amd64.whl",
            "has_sig": false,
            "md5_digest": "5a765eb8741601d60e67f9d3f38f4f6b",
            "packagetype": "bdist_wheel",
            "python_version": "cp39",
            "requires_python": null,
            "size": 16540,
            "upload_time": "2025-02-28T22:07:23",
            "upload_time_iso_8601": "2025-02-28T22:07:23.782934Z",
            "url": "https://files.pythonhosted.org/packages/75/26/561593c5c2c4ac5a31e641011c1bb79aee1945943623f44224c070ed195e/cryostasis-0.2.0-cp39-cp39-win_amd64.whl",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2025-02-28 22:07:22",
    "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"
}
        
Elapsed time: 1.08115s