prefect.utilities.collections
dict_to_flatdict
dct
: The dictionary to flattenflatdict_to_dict
dct
: The dictionary to be nested. Each key should be a tuple of keys
as generated by dict_to_flatdict
isiterable
ensure_iterable
listrepr
extract_instances
objects
: An iterable of objectstypes
: A type or tuple of types to extract, defaults to all objectsbatched_iterable
iterable
: An iterablesize
: The batch size to returnvisit_collection
visit_fn
will be called with the element. The return value of
visit_fn
can be used to alter the element if return_data
is set to True
.
Note:
return_data
is True
, a copy of each collection is created only if
visit_fn
modifies an element within that collection. This approach minimizes
performance penalties by avoiding unnecessary copying.return_data
is False
, no copies are created, and only side effects from
visit_fn
are applied. This mode is faster and should be used when no transformation
of the collection is required, because it never has to copy any data.expr
: A Python object or expression.visit_fn
: A function
that will be applied to every non-collection element of expr
. The function can
accept one or two arguments. If two arguments are accepted, the second argument
will be the context dictionary.return_data
: If True
, a copy of expr
containing data modified by visit_fn
will be returned. This is slower than return_data=False
(the default).max_depth
: Controls the depth of recursive visitation. If set to zero, no
recursion will occur. If set to a positive integer N
, visitation will only
descend to N
layers deep. If set to any negative integer, no limit will be
enforced and recursion will continue until terminal items are reached. By
default, recursion is unlimited.context
: An optional dictionary. If passed, the context will be sent
to each call to the visit_fn
. The context can be mutated by each visitor and
will be available for later visits to expressions at the given depth. Values
will not be available “up” a level from a given expression.
The context will be automatically populated with an ‘annotation’ key when
visiting collections within a BaseAnnotation
type. This requires the caller to
pass context={}
and will not be activated by default.remove_annotations
: If set, annotations will be replaced by their contents. By
default, annotations are preserved but their contents are visited._seen
: A set of object ids that have already been visited. This
prevents infinite recursion when visiting recursive data structures.return_data
is True
, otherwise None
.remove_nested_keys
key_to_remove
. Return obj
unchanged if not a dictionary.
Args:
keys_to_remove
: A list of keys to remove from obj obj: The object to remove keys
from.obj
without keys matching an entry in keys_to_remove
if obj
is a
dictionary. obj
if obj
is not a dictionary.distinct
get_from_dict
dct
: The nested dictionary or list from which to fetch the value.keys
: The sequence of keys to use for access. Can be a
dot-separated string or a list of keys. List indices can be included
in the sequence as either integer keys or as string indices in square
brackets.default
: The default value to return if the requested key path does not
exist. Defaults to None.set_in_dict
dct
: The dictionary to set the value in.keys
: The sequence of keys to use for access. Can be a
dot-separated string or a list of keys.value
: The value to set in the dictionary.KeyError
: If the key path exists and is not a dictionary.deep_merge
merge
into dct
.
Args:
dct
: The dictionary to merge into.merge
: The dictionary to merge from.deep_merge_dicts
dicts
: The dictionaries to merge.AutoEnum
str
, they are automatically
JSON-serializable.
See https://docs.python.org/3/library/enum.html#using-automatic-values
Methods:
auto
enum.auto()
to avoid requiring a second import to use AutoEnum
StopVisiting
visit_collection
.
When raised, the expression is returned without modification and recursive visits
in that path will end.