Skip to main content

No project description provided

Project description

Injectively, deterministically maps objects to hashable, immutable objects.

frozenset is to set as freeze is to Any.

That is, type(a) is type(b) and a != b implies freeze(a) != freeze(b).

Moreover, this function is deterministic, so it can be used to compare states across subsequent process invocations (with the same interpreter major and minor version).

>>> obj = [1, 2, 3, {4, 5, 6}, object()]
>>> hash(obj)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
>>> from charmonium.freeze import freeze
>>> frozen_obj = freeze(obj)
>>> frozen_obj
(1, 2, 3, frozenset({4, 5, 6}), ('object',))

It even works on custom types.

>>> # Make a custom type
>>> class Struct:
...     pass
>>> s = Struct()
>>> s.attr = 4
>>> freeze(s)
('Struct', (('attr', 4),))

Special cases

  • freeze on functions returns their bytecode, constants, and closure-vars. This means that freeze_state(f) == freeze_state(g) implies f(x) == g(x). The remarkable thing is that this is true across subsequent invocations of the same process. If the user edits the script and changes the function, then it’s freeze_state will change too.

  • freeze on objects returns the objects that would be used by pickle from __reduce__, __reduce_ex__, __getnewargs__, __getnewargs_ex__, and __getstate__. The simplest of these to customize your object __gestate__. See the pickle documentation for details.

  • In the cases where __getstate__ is already defined for pickle, and this definition is not suitable for freeze_state, one may override this with __getfrozenstate__ which takes precedence.

Although, this function is not infallible for user-defined types; I will do my best, but sometimes these laws will be violated. These cases include:

  • Cases where __eq__ makes objects equal despite differing attributes or inversely make objects inequal despite equal attributes.

    • This can be mitigated if __getstate__ or __getfrozenstate__

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

charmonium.freeze-0.4.2.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

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

charmonium.freeze-0.4.2-py3-none-any.whl (7.9 kB view details)

Uploaded Python 3

File details

Details for the file charmonium.freeze-0.4.2.tar.gz.

File metadata

  • Download URL: charmonium.freeze-0.4.2.tar.gz
  • Upload date:
  • Size: 8.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.10 CPython/3.9.6 Linux/5.11.0-41-generic

File hashes

Hashes for charmonium.freeze-0.4.2.tar.gz
Algorithm Hash digest
SHA256 d96b061f32f09daed327b09c6e26e8f7ab2c11e127b112a9f1416c3c2a67b943
MD5 77d7196ddd49a1de3bde98279bb8ecff
BLAKE2b-256 bcb7c5eed98e78694f085f268a2509eb778790d089073905d6b7d3e4f247d3c7

See more details on using hashes here.

File details

Details for the file charmonium.freeze-0.4.2-py3-none-any.whl.

File metadata

  • Download URL: charmonium.freeze-0.4.2-py3-none-any.whl
  • Upload date:
  • Size: 7.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.10 CPython/3.9.6 Linux/5.11.0-41-generic

File hashes

Hashes for charmonium.freeze-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9b01f88e63c84ff38f159271a0a5abbc44603cd40379ebb5cf76715cab1b9bd3
MD5 df27460ac9a38bff5eaa92062a98284c
BLAKE2b-256 a6d955b3c688166848507db1bb289f03ae7135ff36ec921899b8f54c315517da

See more details on using hashes here.

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