anystore.util
            Took
    Shorthand to measure time of a code block
Examples:
Source code in anystore/util.py
                
            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
              
            dict_merge(d1, d2)
    Merge the second dict into the first but omit empty values
Source code in anystore/util.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.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.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.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.py
              
            ensure_uri(uri, http_unquote=True)
    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"
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
                uri
             | 
            
                  Any
             | 
            
               uri-like string  | 
            required | 
                http_unquote
             | 
            
                  bool | None
             | 
            
               Return unquoted uri, manually disable for some http edge cases  | 
            
                  True
             | 
          
Returns:
| Type | Description | 
|---|---|
                  str
             | 
            
               Absolute uri with scheme  | 
          
Raises:
| Type | Description | 
|---|---|
                  ValueError
             | 
            
               For invalid uri (e.g. stdin: "-")  | 
          
Source code in anystore/util.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.py
              
            guess_mimetype(key)
    Guess the mimetype based on a file extension and normalize it via
rigour.mime
            is_empty(value)
    
            join_relpaths(*parts)
    Join relative paths, strip leading and trailing "/"
Examples:
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
                *parts
             | 
            
                  Uri
             | 
            
               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
              
            make_uri_key(uri)
    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"
Source code in anystore/util.py
              
            mask_uri(uri)
    Replace username and password in a URI with asterisks
            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.py
              
            name_from_uri(uri)
    Extract the file name from an uri.
Examples:
Parameters:
| Name | Type | Description | Default | 
|---|---|---|---|
                uri
             | 
            
                  Uri
             | 
            
               (Full) path-like uri  | 
            required | 
Returns:
| Type | Description | 
|---|---|
                  str
             | 
            
               File name  | 
          
Source code in anystore/util.py
              
            
            path_from_uri(uri)
    Get pathlib.Path object from an uri
Examples:
>>> path_from_uri("/foo/bar")
Path("/foo/bar")
>>> path_from_uri("file:///foo/bar")
Path("/foo/bar")
>>> path_from_uri("s3://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  | 
          
Source code in anystore/util.py
              
            pydantic_merge(m1, m2)
    Merge the second pydantic object into the first one