Skip to content

ratiopath.tiling.utils

row_hash(row, column='id', algorithm=hashlib.sha256)

Hashes a row (dictionary) using SHA256 and adds the hash as a new column.

Parameters:

Name Type Description Default
row dict[str, Any]

The dictionary (row) to hash.

required
column str

The name of the column to store the hash. Defaults to "id".

'id'
algorithm Callable[[bytes], HASH]

The hashing algorithm to use. Defaults to hashlib.sha256.

sha256

Returns:

Type Description
dict[str, Any]

The modified row (dictionary) with the SHA256 hash added.

Source code in ratiopath/tiling/utils.py
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
def row_hash(
    row: dict[str, Any],
    column: str = "id",
    algorithm: Callable[[bytes], hashlib._hashlib.HASH] = hashlib.sha256,  # type: ignore[name-defined]
) -> dict[str, Any]:
    """Hashes a row (dictionary) using SHA256 and adds the hash as a new column.

    Args:
        row: The dictionary (row) to hash.
        column: The name of the column to store the hash. Defaults to "id".
        algorithm: The hashing algorithm to use. Defaults to hashlib.sha256.

    Returns:
        The modified row (dictionary) with the SHA256 hash added.
    """
    row[column] = algorithm(str(row).encode()).hexdigest()
    return row