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.py
ensure_uri(uri)
Normalize arbitrary uri-like input to an absolute uri with scheme.
Example
assert util.ensure_uri("https://example.com") == "https://example.com"
assert util.ensure_uri("s3://example.com") == "s3://example.com"
assert util.ensure_uri("foo://example.com") == "foo://example.com"
assert util.ensure_uri("-") == "-"
assert util.ensure_uri("./foo").startswith("file:///")
assert util.ensure_uri(Path("./foo")).startswith("file:///")
assert util.ensure_uri("/foo") == "file:///foo"
Args: uri: uri-like string
Returns:
Type | Description |
---|---|
str
|
Absolute uri with scheme |
Raises:
Type | Description |
---|---|
ValueError
|
For invalid uri (e.g. stdin: "-") |
Source code in anystore/util.py
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.py
join_relpaths(*parts)
Join relative paths, strip leading and trailing "/"
Examples:
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*parts
|
str
|
Relative path segments |
()
|
Returns:
Type | Description |
---|---|
str
|
Joined relative path |
Source code in anystore/util.py
join_uri(uri, path)
Ensure correct joining of arbitrary uris with a path.
Example
assert util.join_uri("http://example.org", "foo") == "http://example.org/foo"
assert util.join_uri("http://example.org/", "foo") == "http://example.org/foo"
assert util.join_uri("/tmp", "foo") == "file:///tmp/foo"
assert util.join_uri(Path("./foo"), "bar").startswith("file:///")
assert util.join_uri(Path("./foo"), "bar").endswith("foo/bar")
assert util.join_uri("s3://foo/bar.pdf", "../baz.txt") == "s3://foo/baz.txt"
assert util.join_uri("redis://foo/bar.pdf", "../baz.txt") == "redis://foo/baz.txt"
Parameters:
Name | Type | Description | Default |
---|---|---|---|
uri
|
Any
|
Base uri |
required |
path
|
str
|
Relative path to join on |
required |
Returns:
Type | Description |
---|---|
str
|
Absolute joined uri |
Raises:
Type | Description |
---|---|
ValueError
|
For invalid uri (e.g. stdin: "-") |
Source code in anystore/util.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.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.py
make_signature_key(*args, **kwargs)
Calculate data checksum for arbitrary input (used for caching function calls)
Examples:
Parameters:
Name | Type | Description | Default |
---|---|---|---|
*args
|
Any
|
Arbitrary input arguments |
()
|
**kwargs
|
Any
|
Arbitrary input keyword arguments |
{}
|
Returns:
Type | Description |
---|---|
str
|
Generated sha1 checksum |
Source code in anystore/util.py
path_from_uri(uri)
Get pathlib.Path
object from a file uri
Examples:
>>> path_from_uri("/foo/bar")
Path("/foo/bar")
>>> path_from_uri("file:///foo/bar")
Path("/foo/bar")
Parameters:
Name | Type | Description | Default |
---|---|---|---|
uri
|
Uri
|
Full path-like uri |
required |
Returns:
Type | Description |
---|---|
Path
|
Path object for given uri |