Skip to content

content_hash

content_hash #

ContentHash — what names an object; CallKey — what names a memoized call.

CallKey = NewType('CallKey', ContentHash) module-attribute #

The hash identifying one memoized call: a function identity applied to argument hashes.

ContentHash dataclass #

A 32-byte blake3 digest, rendered as 64 lowercase hex characters.

Source code in capturegraph-lib/capturegraph/recipes/scratch/hashing/content_hash.py
@dataclass(frozen=True, slots=True)
class ContentHash:
    """A 32-byte blake3 digest, rendered as 64 lowercase hex characters."""

    digest: bytes

    @classmethod
    def from_hex(cls, hex: str) -> ContentHash:
        """The hash spelled by ``hex``.

        Raises:
            ValueError: If ``hex`` is not 64 hex characters.
        """
        if len(hex) != 64:
            raise ValueError(f"invalid content hash {hex!r}: expected 64 hex characters")
        return cls(bytes.fromhex(hex))

    @property
    def hex(self) -> str:
        """The digest as 64 lowercase hex characters."""
        return self.digest.hex()

    def __str__(self) -> str:
        """The hex digest."""
        return self.hex

    def __repr__(self) -> str:
        """``ContentHash(<hex>)``."""
        return f"ContentHash({self.hex})"

hex property #

The digest as 64 lowercase hex characters.

__repr__() #

ContentHash(<hex>).

Source code in capturegraph-lib/capturegraph/recipes/scratch/hashing/content_hash.py
def __repr__(self) -> str:
    """``ContentHash(<hex>)``."""
    return f"ContentHash({self.hex})"

__str__() #

The hex digest.

Source code in capturegraph-lib/capturegraph/recipes/scratch/hashing/content_hash.py
def __str__(self) -> str:
    """The hex digest."""
    return self.hex

from_hex(hex) classmethod #

The hash spelled by hex.

Raises:

Type Description
ValueError

If hex is not 64 hex characters.

Source code in capturegraph-lib/capturegraph/recipes/scratch/hashing/content_hash.py
@classmethod
def from_hex(cls, hex: str) -> ContentHash:
    """The hash spelled by ``hex``.

    Raises:
        ValueError: If ``hex`` is not 64 hex characters.
    """
    if len(hex) != 64:
        raise ValueError(f"invalid content hash {hex!r}: expected 64 hex characters")
    return cls(bytes.fromhex(hex))