No project description provided
Project description
❄️ 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.
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.
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. If you encounter any issues or have feature requests, please open an issue using the project's Issues tab.
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 Distributions
Built Distributions
Hashes for cryostasis-0.1.0-cp312-cp312-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3a3f351ada298ad3d2277d94efa6ce12cf9418fd52950eb148af0d0c469f8eab |
|
MD5 | 0ddcc931e7693414cdc9a16908971b44 |
|
BLAKE2b-256 | 3583de886ae4c46db1b2f0ae423499f785f4de1dccd39216a7b4cc4f184f1943 |
Hashes for cryostasis-0.1.0-cp312-cp312-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4c4763a80c6d5590f585f1e0a5e87ac1eadf5d6a32f2924353244002707481d3 |
|
MD5 | f2c71cdd9c7445105a2cd327038cacb4 |
|
BLAKE2b-256 | a4333be3a945fa5cb3d158caf37f6236f06b751fe9892c4fb2a6c34aa62fbb6c |
Hashes for cryostasis-0.1.0-cp312-cp312-macosx_10_13_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5f189dbcb558b23908e532186b1538134f20e3849037b3f1837e947b57227c82 |
|
MD5 | 7e429896354ff398d7007e5743d6c377 |
|
BLAKE2b-256 | d48cd5f6d3bc4e343b96d7f337329fbbdeb4fccad59e66636b678aaf8de57c45 |
Hashes for cryostasis-0.1.0-cp311-cp311-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6421007ffc500d800d6bad40407236e2e98b7d28eef81e8dc5133e6b619436be |
|
MD5 | f94948b475ce7328a7d651d63ef1c74e |
|
BLAKE2b-256 | 41602865d352eb503c689d77d6cc2e3aab472f9e9efe16721d5344f61b971893 |
Hashes for cryostasis-0.1.0-cp311-cp311-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b3ce3f74d7252dc3c490bd46d7dc71669ce92aca063b27aa1b8301d2ae8ddacf |
|
MD5 | 53e886e79c475ea2a1d37f2fbbdc6f3c |
|
BLAKE2b-256 | 030912988afde5a6400cbe83ce9e6510b02330b8c752b4699b6c8fd6c3460859 |
Hashes for cryostasis-0.1.0-cp311-cp311-macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 18ebc3e86b3e674971efa2087e5888a3d56145ccb26f9ff03e7b7d78233afa1e |
|
MD5 | b40767fac2d50c38ac2528d7a9973348 |
|
BLAKE2b-256 | 095e02a3f736087cb24aec72e6b4cf4d3fc4de9554bc3da8a1108afa97b83d79 |
Hashes for cryostasis-0.1.0-cp310-cp310-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b5123a328dbe88658b178634481c292d502dbf096259d59bbdd5ac9bf8032a7c |
|
MD5 | 7db3d78c3afe623750a0385c500e51ab |
|
BLAKE2b-256 | ef3ddd92d82db0bbc09d8e819c97d5f724ec3307fdea75c24e8f05673dc3c966 |
Hashes for cryostasis-0.1.0-cp310-cp310-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7bed5fb8dbc61bd62185a76a85efa034621341c2dbaa2e9ecbe31300b1eaf892 |
|
MD5 | a01272c84d51bcc2604d87c88e22232a |
|
BLAKE2b-256 | 62160c02de04e110725401a68fe7dd48eac50bbcc1efe832190885f74dab9d22 |
Hashes for cryostasis-0.1.0-cp310-cp310-macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7387e8b37965dcf337197921379af1b098abe336346e583babf6328db0c2a4ec |
|
MD5 | 36bd97deb2e8152c9decfdc30340aec9 |
|
BLAKE2b-256 | 1153a07508f98132c231f931a038adee37c0c8ff28adc31b69f2d330c87b5464 |
Hashes for cryostasis-0.1.0-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ea4531cb471279863bffa3f5d5d7a9348df59f8fbfe15a28fc197f2be9b75c4 |
|
MD5 | 7bc03d3f798207dbc6cbf0851d2fd2e4 |
|
BLAKE2b-256 | da865bdaef19bb37c717ae1c57ed3f738517863d549f4af63b09fe0afec12a90 |
Hashes for cryostasis-0.1.0-cp39-cp39-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_28_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 648348b23f66576d7ec9e8cb4dd7540bb15e22188abb8ca657fd760c26049de7 |
|
MD5 | 315af2eeba1d72e457fdc97c5052f181 |
|
BLAKE2b-256 | e70fcaeda8218a12a301a791b51aed2c037d5f2393f06bba0d618b44565f86cf |
Hashes for cryostasis-0.1.0-cp39-cp39-macosx_10_9_universal2.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cd314e6fd6dad46f0a68e225761396e00750fe2a16995f8dc4b8a13a2521da50 |
|
MD5 | 2f193bc7e14aaa60dc085f4299425fb2 |
|
BLAKE2b-256 | 394f460dfccaba60d11a31c9f2577ceb0037b016fcbdc731cd2a55cd8ba0bd89 |