Skip to content

Commit 99713e0

Browse files
Updated cache operation docstrings.
1 parent b72d779 commit 99713e0

File tree

1 file changed

+22
-10
lines changed

1 file changed

+22
-10
lines changed

src/emsarray/operations/cache.py

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Operations for making caching keys for given datasets and attribute dicts.
2+
Operations for making caching keys for a given dataset.
33
"""
44
import hashlib
55
import marshal
@@ -20,7 +20,7 @@ def hash_attributes(hash: "hashlib._Hash", attributes: dict) -> None:
2020
The hash instance to update with the given attribute dict.
2121
This must follow the interface defined in :mod:`hashlib`.
2222
attributes: dict
23-
Expects a marshal compatible dictionary
23+
Expects a marshal compatible dictionary.
2424
"""
2525
# Prepend the marshal encoding version
2626
marshal_version = 4
@@ -44,7 +44,7 @@ def hash_string(hash: "hashlib._Hash", value: str) -> None:
4444
The hash instance to update with the given attribute dict.
4545
This must follow the interface defined in :mod:`hashlib`.
4646
attributes: str
47-
Expects a string that can be encoded in utf-8
47+
Expects a string that can be encoded in utf-8.
4848
"""
4949
# Prepend the str length
5050
hash_int(hash, len(value))
@@ -61,10 +61,8 @@ def hash_int(hash: "hashlib._Hash", value: int) -> None:
6161
The hash instance to update with the given attribute dict.
6262
This must follow the interface defined in :mod:`hashlib`.
6363
attributes: int
64-
Expects an int that can be represented in a numpy int32
64+
Expects an int that can be represented in a numpy int32.
6565
"""
66-
# Prepend the int bit length
67-
hash.update(numpy.int32(value.bit_length()).tobytes())
6866
hash.update(numpy.int32(value).tobytes())
6967

7068

@@ -75,9 +73,9 @@ def make_cache_key(dataset: xarray.Dataset, hash: "hashlib._Hash | None" = None)
7573
Parameters
7674
----------
7775
dataset : xarray.Dataset
78-
The dataset to generate a cache key from
79-
hash : hash instance
80-
An instance of a hashlib hash class
76+
The dataset to generate a cache key from.
77+
hash : hashlib._Hash
78+
An instance of a hashlib hash class.
8179
Defaults to `hashlib.blake2b`, which is secure enough and fast enough for most purposes.
8280
The hash algorithm does not need to be cryptographically secure,
8381
so faster algorithms such as `xxhash` can be swapped in if desired.
@@ -86,7 +84,21 @@ def make_cache_key(dataset: xarray.Dataset, hash: "hashlib._Hash | None" = None)
8684
-------
8785
cache_key : str
8886
A string suitable for use as a cache key.
89-
The string will be safe for use as part filename if data are to be cached to disk.
87+
The string will be safe for use as part of a filename if data is to be cached to disk.
88+
89+
Examples
90+
--------
91+
92+
.. code-block:: python
93+
94+
import emsarray
95+
from emsarray.operations.cache import make_cache_key
96+
97+
# Make a cache key from the dataset
98+
dataset = emsarray.tuorial.open_dataset("austen")
99+
cache_key = make_cache_key(dataset)
100+
>>> cache_key
101+
'580853c44e732878937598e86d0b26cb81e18d986072c0790a122244e9d3f480'
90102
91103
Notes
92104
-----

0 commit comments

Comments
 (0)