anystore.util
clean_dict(data)
Ensure dict return, clean up defaultdicts, drop None values and ensure
str keys (for serialization)
Examples:
>>> clean_dict({1: 2})
{"1": 2}
>>> clean_dict({"a": ""})
{}
>>> clean_dict({"a": None})
{}
>>> clean_dict("foo")
{}
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
Arbitrary input data |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
A cleaned dict with string keys (or an empty one) |
Source code in anystore/util/data.py
dict_merge(d1, d2)
Merge the second dict into the first but omit empty values
Source code in anystore/util/data.py
dump_json(obj, clean=False, newline=False)
Dump a python dictionary to json bytes via orjson
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
SDict
|
The data object (dictionary with string keys) |
required |
clean
|
bool | None
|
Apply clean_dict |
False
|
newline
|
bool | None
|
Add a linebreak |
False
|
Source code in anystore/util/data.py
dump_json_model(obj, clean=False, newline=False)
Dump a pydantic obj to json bytes via orjson
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
BaseModel
|
The pydantic object |
required |
clean
|
bool | None
|
Apply clean_dict |
False
|
newline
|
bool | None
|
Add a linebreak |
False
|
Source code in anystore/util/data.py
dump_yaml(obj, clean=False, newline=False)
Dump a python dictionary to bytes
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
SDict
|
The data object (dictionary with string keys) |
required |
clean
|
bool | None
|
Apply clean_dict |
False
|
newline
|
bool | None
|
Add a linebreak |
False
|
Source code in anystore/util/data.py
dump_yaml_model(obj, clean=False, newline=False)
Dump a pydantic obj to yaml bytes
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
obj
|
BaseModel
|
The pydantic object |
required |
clean
|
bool | None
|
Apply clean_dict |
False
|
newline
|
bool | None
|
Add a linebreak |
False
|
Source code in anystore/util/data.py
is_empty(value)
model_dump(obj, clean=False)
Serialize a pydantic object to a dict by alias and json mode
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
clean
|
bool | None
|
Apply clean_dict |
False
|
Source code in anystore/util/data.py
pydantic_merge(m1, m2)
Merge the second pydantic object into the first one
Source code in anystore/util/data.py
make_checksum(io, algorithm=DEFAULT_HASH_ALGORITHM)
Calculate checksum for bytes input for given algorithm
Example
This can be used for file handlers:
Note
See make_data_checksum for easier
implementation for arbitrary input data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
io
|
BinaryIO
|
File-like open handler |
required |
algorithm
|
str
|
Algorithm from |
DEFAULT_HASH_ALGORITHM
|
Returns:
| Type | Description |
|---|---|
str
|
Generated checksum |
Source code in anystore/util/checksum.py
make_data_checksum(data, algorithm=DEFAULT_HASH_ALGORITHM)
Calculate checksum for input data based on given algorithm
Examples:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Any
|
Arbitrary input object |
required |
algorithm
|
str
|
Algorithm from |
DEFAULT_HASH_ALGORITHM
|
Returns:
| Type | Description |
|---|---|
str
|
Generated checksum |
Source code in anystore/util/checksum.py
make_fast_hash(io)
Make a fast checksum for comparison. Don't use this for real data integrity or cryptographic checks.
Uses imohash: samples the beginning, middle and end of the stream and hashes those samples with MurmurHash3-128.
Source code in anystore/util/checksum.py
make_signature_key(*args, algorithm=DEFAULT_HASH_ALGORITHM, **kwargs)
Calculate data checksum for arbitrary input (used for caching function calls)
Examples:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*args
|
Any
|
Arbitrary input arguments |
()
|
algorithm
|
str
|
Algorithm from |
DEFAULT_HASH_ALGORITHM
|
**kwargs
|
Any
|
Arbitrary input keyword arguments |
{}
|
Returns:
| Type | Description |
|---|---|
str
|
Generated checksum |
Source code in anystore/util/checksum.py
make_uri_key(uri, algorithm=DEFAULT_HASH_ALGORITHM)
Make a verbose key usable for caching. It strips the scheme, uses host and path as key parts and creates a checksum for the uri (including fragments, params, etc.). This is useful for invalidating a cache store partially by deleting keys by given host or path prefixes.
Examples:
>>> make_uri_key("https://example.org/foo/bar#fragment?a=b&c")
"example.org/foo/bar/ecdb319854a7b223d72e819949ed37328fe034a0"
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri
|
Uri
|
Input URI |
required |
algorithm
|
str
|
Algorithm from |
DEFAULT_HASH_ALGORITHM
|
Source code in anystore/util/checksum.py
Took
Shorthand to measure time of a code block
Examples:
Source code in anystore/util/misc.py
ensure_uuid(uuid=None)
get_extension(uri)
Extract file extension from given uri.
Examples:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
uri
|
Uri
|
Full path-like uri |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
Extension or |
Source code in anystore/util/misc.py
guess_mimetype(key)
Guess the mimetype based on a file extension and normalize it via
rigour.mime
mask_uri(uri)
Replace username and password in a URI with asterisks
rm_rf(uri)
like rm -rf, ignoring errors.