Skip to main content

Injectively, deterministically maps objects to hashable, immutable objects

Project description

PyPI Package PyPI Downloads License Python Versions libraries.io sourcerank GitHub stars CI status GitHub last commit Checked with Mypy Code style: black

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}), ('args', 'object'))

It even works on custom types.

>>> # Make a custom type
>>> class Struct:
...     def frobnicate(self):
...         print(123)
>>> s = Struct()
>>> s.attr = 4
>>> freeze(s)
('args', 'Struct', 'state', (('attr', 4),))

And methods, functions, lambdas, etc.

>>> freeze(lambda x: x + 123)
(('code', (('name', '<lambda>'), ('varnames', ('x',)), ('constants', (None, 123)), ('bytecode', b'|\x00d\x01\x17\x00S\x00'))),)
>>> import functools
>>> freeze(functools.partial(print, 123))
('constructor', 'partial', 'args', 'print', 'state', ('print', (123,), (), None))
>>> freeze(Struct.frobnicate)
(('code', (('name', 'frobnicate'), ('varnames', ('self',)), ('constants', (None, 123)), ('bytecode', b't\x00d\x01\x83\x01\x01\x00d\x00S\x00'))),)

If the source code of Struct.frobnicate changes between successive invocations, then the freeze value will change. This is useful for caching unchanged functions.

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__

Installing

If you don’t have pip installed, see the pip install guide.

$ pip install charmonium.freeze

See CONTRIBUTING.md for instructions on setting up a development environment.

Debugging

Use the following lines to see how freeze decomposes an object. It shows the object tree that freeze walks until it reaches primitive values on the leaves

import logging
import os
logger = logging.getLogger("charmonium.freeze")
logger.setLevel(logging.DEBUG)
fh = logging.FileHandler("freeze.log")
fh.setLevel(logging.DEBUG)
fh.setFormatter(logging.Formatter("%(message)s"))
logger.addHandler(fh)
logger.debug("Program %d", os.getpid())

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.5.3.tar.gz (11.9 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.5.3-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: charmonium.freeze-0.5.3.tar.gz
  • Upload date:
  • Size: 11.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.9.10 Linux/5.13.0-36-generic

File hashes

Hashes for charmonium.freeze-0.5.3.tar.gz
Algorithm Hash digest
SHA256 ed23b98a3b8889b3cad5aa6e1b82ef45886c1cc31c51d21383da65e2c1868b92
MD5 e1a01874a296c6f949e703158cbf4d1e
BLAKE2b-256 d99e39b00f65d7ab1a1d4da36da0f33a9bda5651b40dd50fb1a85edbace5c942

See more details on using hashes here.

File details

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

File metadata

  • Download URL: charmonium.freeze-0.5.3-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.9.10 Linux/5.13.0-36-generic

File hashes

Hashes for charmonium.freeze-0.5.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6efe342c1a53af07c2f3577660feef2def960b764253984c393753b3d906b1c2
MD5 0b6652bf525e0d5067f6b121f491bc0a
BLAKE2b-256 33ec3755c825e6cfca98b6838d3b8235fca413f8583c1dff053b62a88d22ea4f

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