marsh.utils
¶
Collection of general utilities used throughout the framework.
- class SingletonMeta[source]¶
Converts a class into a singleton.
When the constructor of the class is called the first time, an instance is constructed. After this, the constructor will always return that instance unless it is garbage collected.
To allow for garbage collection of unused instances a weak reference is kept of the instance.
- class CacheInfo(hits=0, misses=0, maxsize=None, currsize=0)[source]¶
Tuple with information for a cache.
- cache(*, maxsize: Optional[int] = None, typed: bool = False, safe: bool = True, binding: Optional[Literal['weak', 'ignore']] = None) Callable[[_C], _C] [source]¶
- cache(fn: _C, /, *, maxsize: Optional[int] = None, typed: bool = False, safe: bool = True, binding: Optional[Literal['weak', 'ignore']] = None) _C
Extended LRU cache decorator for functions.
Allows for safe usage of lru cache, where the
TypeError
produced by non-hashable types in the input arguments are caught before a new attempt is made to call the function, this time bypassing the cache compmletely.Note
The bypass is triggered for
TypeError
. If the function itself can raise this error it will be called twice before the final error is propagated.The wrapped function fulfills the
CacheType
protocol.- Parameters:
fn (
Optional
[TypeVar
(_C
, bound=Callable
)]) – IfNone
, the wrapper is returned, otherwise this argument is wrapped and returned.typed (
bool
) – Differentiate between different types of values that produce the same hash.safe (
bool
) – Make a second attempt at calling the function and bypassing the cache if the first attempt fails with aTypeError
.binding (
Optional
[Literal
[‘weak’, ‘ignore’]]) – Should only be used when a method to a class is decorated with this function.'weak'
will make sure that a weak reference toself
/cls
/metacls
is stored in the cache instead of a strong reference. This allows for objects with lru_cache to be garbage collected when no longer used.'ignore'
allows the first argument to bypass the cache, only performing a lookup on the other arguments.
- Return type:
Union
[TypeVar
(_C
, bound=Callable
),Callable
[[TypeVar
(_C
, bound=Callable
)],TypeVar
(_C
, bound=Callable
)]]- Returns:
The wrapper, unless
fn
is given, which instead wraps the argument and returns it.
- make_hash_key(*args, **kwargs)[source]¶
Make a single cached hashable key from arguments.
All arguments must be hashable.
- Return type:
- Returns:
Hashable object that caches its hash, only caclulating it once.
- make_typed_hash_key(*args, **kwargs)[source]¶
Make a single cached hashable key from arguments.
Includes the type of each value.
All arguments must be hashable.
- Return type:
- Returns:
Hashable object that caches its hash, only caclulating it once.
- class SafeDict[source]¶
Dictionary that works with unhashable keys.
Just like built-in
dict
the order is kept.
- class ValueCache(typed=False)[source]¶
A dict-like cache that allows for non-hashable keys.
The hash for cachable keys are also hashed to improve lookup speed.
- Parameters:
typed (
bool
) – The key type is also taken into consideration when matching keys.
- class CachePool[source]¶
A collection of caches.
- new_callable_cache(*, name: str, maxsize: Optional[int] = None, typed: bool = False, safe: bool = True, binding: Optional[Literal['weak', 'ignore']] = None) Callable[[_C], _C] [source]¶
- new_callable_cache(fn: _C, *, name: str, maxsize: Optional[int] = None, typed: bool = False, safe: bool = True, binding: Optional[Literal['weak', 'ignore']] = None) _C
Wrap a function with
cache()
while simultaneously adding it to this pool.See
cache()
for descriptions of arguments forwarded to the wrapper.
- new_value_cache(name)[source]¶
Create a new value cache and add it to this pool before returning it.
- Parameters:
name (
str
) – The name associated with the new cache.- Return type:
- Returns:
The value cache.
- cache_clear()[source]¶
Clear every cache in this container.
This does not remove the caches from the container, it only calls the
clear_cache()
function on each cache.- Return type:
- class IterableFromIterator(iterator)[source]¶
Lazily stores results of an iterator.
Allows for multiple iterations over the wrapped iterator while only consuming it once.
- class PriorityOrder[source]¶
A mutable sequence of values ordered by priority.
Higher priority items are moved forward while lower priority items are moved backward in the sequence.
- RelativePriority¶
Relative priority input type.
- class WeakTypeCache(skip_types=())[source]¶
A type cache that holds its values as weak references.
When iterated over the types are yielded in reverse order, the most recently added is yielded first.
- Parameters:
skip_types (
Iterable
[Any
]) – Any types to ignore. If attempting to store one of these types it becomes a no-op.
- is_missing(value)[source]¶
Determine if a value is missing.
Supports
marsh.MISSING
,omegaconf.MISSING
anddataclasses.MISSING
.
- is_sequence_type(value)[source]¶
Determine if a value is a sequence type (excluding string, bytes types).
- is_empty_tuple_type(value)[source]¶
Determine if a value is an empty tuple type (
typing.Tuple[()]
,tuple[()]
).
- is_fixed_size_tuple_type(value)[source]¶
Determine if a value is fixed size tuple type.
- Parameters:
value – Any value to evaluate.
- Return type:
- Returns:
True
if value is a a fixed size tuple type, elseFalse
.
- is_primitive(value)[source]¶
Determine if a value is primitive.
PrimitiveElement: int | float | bool | str
- is_primitive_type(value)[source]¶
Determine if a value is primitive type.
PrimitiveElement: int | float | bool | str
- is_namedtuple(value)[source]¶
Determine if a value is a namedtuple.
- Parameters:
value (
Any
) – Any value to evaluate.- Return type:
TypeGuard
[NamedTupleProtocol
]- Returns:
True
if value is a namedtuple, elseFalse
.
- is_namedtuple_type(value)[source]¶
Determine if a value is a namedtuple type.
- Parameters:
value (
Any
) – Any value to evaluate.- Return type:
TypeGuard
[Type
[NamedTupleProtocol
]]- Returns:
True
if value is a namedtuple type, elseFalse
.
- is_typed_namedtuple(value)[source]¶
Determine if a value is a typed namedtuple.
- Parameters:
value (
Any
) – Any value to evaluate.- Return type:
TypeGuard
[NamedTupleProtocol
]- Returns:
True
if value is a namedtuple, elseFalse
.
- is_typed_namedtuple_type(value)[source]¶
Determine if a value is a typed namedtuple type.
- Parameters:
value (
Any
) – Any value to evaluate.- Return type:
TypeGuard
[Type
[NamedTupleProtocol
]]- Returns:
True
if value is a typed namedtuple type, elseFalse
.
- is_callable(value)[source]¶
Determine if a value is callable.
Always returns
False
if the value is a protocol or typing alias.
- is_annotated(value)[source]¶
Determine if a value originates from
typing.Annotated
.- Parameters:
value (
Any
) – Any value to evaluate.- Return type:
- Returns:
True
if value originates fromtyping.Annotated
, elseFalse
.
- get_annotations(value)[source]¶
Extract the additional arguments of an instance of
typing.Annotated
- Parameters:
value (
Any
) – Thetyping.Annotated
instance.- Return type:
- Returns:
The annotations.
- is_protocol(value)[source]¶
Determine if a value is a protocol.
Any class that has
typing.Protocol
in its immediate bases is considered a protocol.
- is_typing_alias(value)[source]¶
Determine if a value is a typing alias.
If the class name ends with
'_GenericAlias'
,'_SpecialForm'
or'_AnnotatedAlias'
the value is considered a typing alias.For python 3.9+ a check is made against
types.GenericAlias
.
- get_type(value)[source]¶
Attempt to retrieve the type of an object.
If the value is a generic typing alias, the origin type is returned. I.e.
get_type(typing.List[str])
returns the builtinslist
type.If the value is an instance of a class, the class is returned.
If the value is a class, it is directly returned.
>>> assert get_type([1, 2, 3]) == get_type(list)
- bytes_to_base64(value)[source]¶
Encode bytes to a base64 string.
Follows the format used by protobuf.
- base64_to_bytes(value)[source]¶
Decode a base64 string to bytes.
Follows the format used by protobuf.
- primitive_to_bool(value)[source]¶
Convert a primitive value to a bool.
Follows the YAML convention instead of using
__bool__
function of the object, seestr_to_bool()
for more info.Raises
ValueError
on failure.
- str_to_bool(value)[source]¶
Cast a string to a boolean.
True: 1 | [Tt][Rr][Uu][Ee] | [Yy] | [Yy][Ee][Ss] | [Oo][Nn]
False: 0 | [Ff][Aa][Ll][Ss][Ee] | [Nn] | [Nn][Oo] | [Oo][Ff][Ff]
Inspired by https://yaml.org/type/bool.html
Raises
ValueError
if the string does not conform to this format.
- str_to_int(value)[source]¶
Cast a string to an int.
Compared to using
int
to cast the value this function allows values such as"0.0"
by first parsing the value as a float before casting it to an int (unless it contains decimals).Raises
ValueError
if the input string can not be parsed as an int.
- float_to_int(value)[source]¶
Cast a float to an int.
Raises
ValueError
if the input float is not an integer (contains decimal values). This differs from usingint
to cast a float since decimal values produce an error instead of being dropped.Raises
ValueError
on failure.
- cast_primitive(type_, value)[source]¶
Cast a primitive value to a specified primitive type.
Uses
primitive_to_bool()
,str_to_int()
andfloat_to_int()
when applicable.Raises
ValueError
if unsuccessful.
- cast_literal(literal, value)[source]¶
Attempts to cast a primtive value to the same type as a primitive literal and match it.
Raises
ValueError
if not able to match.
- match_literal(literals, value)[source]¶
Attempts to cast and match a value to a set of primitive literals.
Raises
ValueError
if not able to match.
- cast_none(value)[source]¶
Attempt to cast a value to
None
.Allowed values are case insensitive strings
"null"
and"none"
or a missing value.Raises
ValueError
on failure.
- extract_description(doc)[source]¶
Omit all but the main text in a docstring.
Attempts to omit things such as arguments and return value from a docstring.
- get_description(cls)[source]¶
Get the docstring for a class.
An attempt is made to omit all but the main text in the docstring. This means that the return value should be missing things such as arguments and return value descriptions.
- as_dataclass(cls)[source]¶
Safely convert a class into a dataclass.
This function can be called multiple times on the same class without it raising an error the second time.
- get_closest(value, candidates)[source]¶
Get the closest value to some input from a set of candidates.
If no close value is found,
None
is returned.
- get_closest_error_message(value, candidates, key='key')[source]¶
Get an error message for a key.
The error message can take two forms, the first being
'unknown "{key}"'
and the second being'unknown "{key}", did you mean "{closest}"?'
which is only returned when a close match is found among the candidates.
- inspect_sequence_type(sequence_type)[source]¶
Get the type of the content of a sequence type.
Expects a single content type. If no content type is found or there are multiple content types
typing.Any
is returned.- Parameters:
type – The type variable of a sequence.
sequence_type (Any) –
- Return type:
- Returns:
The type for the content of the input sequence type.
>>> get_sequence_type(typing.List[int]) <class 'int'> >>> get_sequence_type(typing.Tuple[str, ...]) <class 'str'> >>> get_sequence_type(list) typing.Any >>> get_sequence_type(typing.Tuple[str, int, float]) typing.Any
- inspect_mapping_type(mapping_type)[source]¶
Get the key and value type from a mapping type.
Returns
typing.Any
for types that could not be inferred.- Parameters:
type – The type variable of a mapping.
mapping_type (Any) –
- Return type:
_MappingKeyValueTypesInfo
- Returns:
The key and value type.
>>> get_sequence_type(typing.Dict[str, int]) (<class 'str'>, <class 'int'>) >>> get_sequence_type(typing.Mapping[str, typing.List[int]]]) (<class 'str'>, typing.List[int]) >>> get_sequence_type(dict) (typing.Any, typing.Any)