Skip to content

content

content #

value_hash — every value hashes by its structure, in one walk.

  1. Dispatch on the value (_digest): a container hashes its kind and its children — a struct by field name, a map by encoded key, an array in order — and a leaf its contents — a file its extension and bytes, a directory its tree, a JSON scalar the bytes it stores as, a Missing (or None, which stores as one) its reason, a plain value its canonical encoding. The class holding a value never enters, so a loaded value, an equal one built in memory and one from a re-executed module hash equal.
  2. A read-only container remembers its hash, since it cannot change; an in-memory one is walked again each time, its read-only children answering from memory.
  3. A file or directory is hashed on every walk, since a workspace may write it in place, through the scratch's stat-checked cache; everything one walk learns is remembered in one write.

value_hash(value) #

The content hash of value, by its structure; equal values hash equal.

assert cg.value_hash(cg.load(root, Session)) == cg.value_hash(Session(**fields))

Parameters:

Name Type Description Default
value object

A CGType value, a Missing, a path, or a plain value canonical encodes — nested in any of those.

required

Raises:

Type Description
TypeError

If value, or anything nested in it, has no content hash.

Source code in capturegraph-lib/capturegraph/scratch/values/content.py
def value_hash(value: object) -> ContentHash:
    """The content hash of ``value``, by its structure; equal values hash equal.

    ```python
    assert cg.value_hash(cg.load(root, Session)) == cg.value_hash(Session(**fields))
    ```

    Args:
        value: A ``CGType`` value, a ``Missing``, a path, or a plain value
            ``canonical`` encodes — nested in any of those.

    Raises:
        TypeError: If ``value``, or anything nested in it, has no content hash.
    """
    with _walk():
        return _hashed(value)