nemo_microservices._utils._utils#

Module Contents#

Functions#

coerce_boolean

coerce_float

coerce_integer

deepcopy_minimal

Minimal reimplementation of copy.deepcopy() that will only copy certain object types:

extract_files

Recursively extract files from the given dictionary based on specified paths.

file_from_path

flatten

get_async_library

get_required_header

human_join

is_dict

is_given

is_iterable

is_list

is_mapping

is_mapping_t

is_sequence

is_sequence_t

is_tuple

is_tuple_t

json_safe

Translates a mapping / sequence recursively in the same fashion as pydantic v2’s model_dump(mode="json").

lru_cache

A version of functools.lru_cache that retains the type signature for the wrapped function arguments.

maybe_coerce_boolean

maybe_coerce_float

maybe_coerce_integer

quote

Add single quotation marks around the given string. Does not do any escaping.

removeprefix

Remove a prefix from a string.

removesuffix

Remove a suffix from a string.

required_args

Decorator to enforce a given set of arguments or variants of arguments are passed to the decorated function.

strip_not_given

Remove all top-level keys where their values are instances of NotGiven

Data#

API#

nemo_microservices._utils._utils.CallableT#

‘TypeVar(…)’

nemo_microservices._utils._utils.coerce_boolean(val: str) bool#
nemo_microservices._utils._utils.coerce_float(val: str) float#
nemo_microservices._utils._utils.coerce_integer(val: str) int#
nemo_microservices._utils._utils.deepcopy_minimal(
item: nemo_microservices._utils._utils._T,
) nemo_microservices._utils._utils._T#

Minimal reimplementation of copy.deepcopy() that will only copy certain object types:

  • mappings, e.g. dict

  • list

This is done for performance reasons.

nemo_microservices._utils._utils.extract_files(
query: Mapping[str, object],
*,
paths: Sequence[Sequence[str]],
) list[tuple[str, nemo_microservices._types.FileTypes]]#

Recursively extract files from the given dictionary based on specified paths.

A path may look like this [‘foo’, ‘files’, ‘’, ‘data’].

Note: this mutates the given dictionary.

nemo_microservices._utils._utils.file_from_path(path: str) nemo_microservices._types.FileTypes#
nemo_microservices._utils._utils.flatten(
t: Iterable[Iterable[nemo_microservices._utils._utils._T]],
) list[nemo_microservices._utils._utils._T]#
nemo_microservices._utils._utils.get_async_library() str#
nemo_microservices._utils._utils.get_required_header(
headers: nemo_microservices._types.HeadersLike,
header: str,
) str#
nemo_microservices._utils._utils.human_join(
seq: Sequence[str],
*,
delim: str = ', ',
final: str = 'or',
) str#
nemo_microservices._utils._utils.is_dict(
obj: object,
) typing_extensions.TypeGuard[dict[object, object]]#
nemo_microservices._utils._utils.is_given(
obj: nemo_microservices._utils._utils._T | nemo_microservices._types.NotGiven | nemo_microservices._types.Omit,
) typing_extensions.TypeGuard[nemo_microservices._utils._utils._T]#
nemo_microservices._utils._utils.is_iterable(
obj: object,
) typing_extensions.TypeGuard[Iterable[object]]#
nemo_microservices._utils._utils.is_list(obj: object) typing_extensions.TypeGuard[list[object]]#
nemo_microservices._utils._utils.is_mapping(
obj: object,
) typing_extensions.TypeGuard[Mapping[str, object]]#
nemo_microservices._utils._utils.is_mapping_t(
obj: nemo_microservices._utils._utils._MappingT | object,
) typing_extensions.TypeGuard[nemo_microservices._utils._utils._MappingT]#
nemo_microservices._utils._utils.is_sequence(
obj: object,
) typing_extensions.TypeGuard[Sequence[object]]#
nemo_microservices._utils._utils.is_sequence_t(
obj: nemo_microservices._utils._utils._SequenceT | object,
) typing_extensions.TypeGuard[nemo_microservices._utils._utils._SequenceT]#
nemo_microservices._utils._utils.is_tuple(
obj: object,
) typing_extensions.TypeGuard[tuple[object, ...]]#
nemo_microservices._utils._utils.is_tuple_t(
obj: nemo_microservices._utils._utils._TupleT | object,
) typing_extensions.TypeGuard[nemo_microservices._utils._utils._TupleT]#
nemo_microservices._utils._utils.json_safe(data: object) object#

Translates a mapping / sequence recursively in the same fashion as pydantic v2’s model_dump(mode="json").

nemo_microservices._utils._utils.lru_cache(
*,
maxsize: int | None = 128,
) Callable[[nemo_microservices._utils._utils.CallableT], nemo_microservices._utils._utils.CallableT]#

A version of functools.lru_cache that retains the type signature for the wrapped function arguments.

nemo_microservices._utils._utils.maybe_coerce_boolean(val: str | None) bool | None#
nemo_microservices._utils._utils.maybe_coerce_float(val: str | None) float | None#
nemo_microservices._utils._utils.maybe_coerce_integer(val: str | None) int | None#
nemo_microservices._utils._utils.quote(string: str) str#

Add single quotation marks around the given string. Does not do any escaping.

nemo_microservices._utils._utils.removeprefix(string: str, prefix: str) str#

Remove a prefix from a string.

Backport of str.removeprefix for Python < 3.9

nemo_microservices._utils._utils.removesuffix(string: str, suffix: str) str#

Remove a suffix from a string.

Backport of str.removesuffix for Python < 3.9

nemo_microservices._utils._utils.required_args(
*variants: Sequence[str],
) Callable[[nemo_microservices._utils._utils.CallableT], nemo_microservices._utils._utils.CallableT]#

Decorator to enforce a given set of arguments or variants of arguments are passed to the decorated function.

Useful for enforcing runtime validation of overloaded functions.

Example usage:

@overload
def foo(*, a: str) -> str: ...


@overload
def foo(*, b: bool) -> str: ...


# This enforces the same constraints that a static type checker would
# i.e. that either a or b must be passed to the function
@required_args(["a"], ["b"])
def foo(*, a: str | None = None, b: bool | None = None) -> str: ...
nemo_microservices._utils._utils.strip_not_given(obj: object | None) object#

Remove all top-level keys where their values are instances of NotGiven