collections
prefect.utilities.collections
Utilities for extensions of and operations on Python collections.
Functions
dict_to_flatdict
Converts a (nested) dictionary to a flattened representation.
Each key of the flat dict will be a CompoundKey tuple containing the “chain of keys” for the corresponding value.
Args:
dct
: The dictionary to flatten
Returns:
- A flattened dict of the same type as dct
flatdict_to_dict
Converts a flattened dictionary back to a nested dictionary.
Args:
dct
: The dictionary to be nested. Each key should be a tuple of keys as generated bydict_to_flatdict
Returns A nested dict of the same type as dct
isiterable
Return a boolean indicating if an object is iterable.
Excludes types that are iterable but typically used as singletons:
- str
- bytes
- IO objects
ensure_iterable
listrepr
extract_instances
Extract objects from a file and returns a dict of type -> instances
Args:
objects
: An iterable of objectstypes
: A type or tuple of types to extract, defaults to all objects
Returns:
- If a single type is given: a list of instances of that type
- If a tuple of types is given: a mapping of type to a list of instances
batched_iterable
Yield batches of a certain size from an iterable
Args:
iterable
: An iterablesize
: The batch size to return
visit_collection
Visits and potentially transforms every element of an arbitrary Python collection.
If an element is a Python collection, it will be visited recursively. If an element
is not a 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:
- When
return_data
isTrue
, a copy of each collection is created only ifvisit_fn
modifies an element within that collection. This approach minimizes performance penalties by avoiding unnecessary copying. - When
return_data
isFalse
, no copies are created, and only side effects fromvisit_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.
Supported types:
- List (including iterators)
- Tuple
- Set
- Dict (note: keys are also visited recursively)
- Dataclass
- Pydantic model
- Prefect annotations
Note that visit_collection will not consume generators or async generators, as it would prevent the caller from iterating over them.
Args:
expr
: A Python object or expression.visit_fn
: A function that will be applied to every non-collection element ofexpr
. The function can accept one or two arguments. If two arguments are accepted, the second argument will be the context dictionary.return_data
: IfTrue
, a copy ofexpr
containing data modified byvisit_fn
will be returned. This is slower thanreturn_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 integerN
, visitation will only descend toN
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 thevisit_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 aBaseAnnotation
type. This requires the caller to passcontext={}
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.
Returns:
- The modified collection if
return_data
isTrue
, otherwiseNone
.
remove_nested_keys
Recurses a dictionary returns a copy without all keys that match an entry in
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.
Returns:
obj
without keys matching an entry inkeys_to_remove
ifobj
is a dictionary.obj
ifobj
is not a dictionary.
distinct
get_from_dict
Fetch a value from a nested dictionary or list using a sequence of keys.
This function allows to fetch a value from a deeply nested structure of dictionaries and lists using either a dot-separated string or a list of keys. If a requested key does not exist, the function returns the provided default value.
Args:
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.
Returns:
- The fetched value if the key exists, or the default value if it does not.
Examples:
set_in_dict
Sets a value in a nested dictionary using a sequence of keys.
This function allows to set a value in a deeply nested structure of dictionaries and lists using either a dot-separated string or a list of keys. If a requested key does not exist, the function will create it as a new dictionary.
Args:
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.
Returns:
- The modified dictionary with the value set at the specified key path.
Raises:
KeyError
: If the key path exists and is not a dictionary.
deep_merge
Recursively merges merge
into dct
.
Args:
dct
: The dictionary to merge into.merge
: The dictionary to merge from.
Returns:
- A new dictionary with the merged contents.
deep_merge_dicts
Recursively merges multiple dictionaries.
Args:
dicts
: The dictionaries to merge.
Returns:
- A new dictionary with the merged contents.
Classes
AutoEnum
An enum class that automatically generates value from variable names.
This guards against common errors where variable names are updated but values are not.
In addition, because AutoEnums inherit from str
, they are automatically
JSON-serializable.
See https://docs.python.org/3/library/enum.html#using-automatic-values
Methods:
auto
Exposes enum.auto()
to avoid requiring a second import to use AutoEnum
StopVisiting
A special exception used to stop recursive visits in visit_collection
.
When raised, the expression is returned without modification and recursive visits in that path will end.