prefect.utilities.collections
¶
Utilities for extensions of and operations on Python collections.
AutoEnum
¶
Bases: str
, Enum
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
Example
class MyEnum(AutoEnum):
RED = AutoEnum.auto() # equivalent to RED = 'RED'
BLUE = AutoEnum.auto() # equivalent to BLUE = 'BLUE'
Source code in prefect/utilities/collections.py
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
|
auto
staticmethod
¶
Exposes enum.auto()
to avoid requiring a second import to use AutoEnum
Source code in prefect/utilities/collections.py
65 66 67 68 69 70 |
|
StopVisiting
¶
Bases: BaseException
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.
Source code in prefect/utilities/collections.py
217 218 219 220 221 222 223 |
|
batched_iterable
¶
Yield batches of a certain size from an iterable
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterable |
Iterable
|
An iterable |
required |
size |
int
|
The batch size to return |
required |
Yields:
Name | Type | Description |
---|---|---|
tuple |
Tuple[T, ...]
|
A batch of the iterable |
Source code in prefect/utilities/collections.py
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 |
|
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.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dct |
dict
|
The dictionary to flatten |
required |
_parent |
Tuple
|
The current parent for recursion |
None
|
Returns:
Type | Description |
---|---|
Dict[Tuple[KT, ...], Any]
|
A flattened dict of the same type as dct |
Source code in prefect/utilities/collections.py
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
|
extract_instances
¶
Extract objects from a file and returns a dict of type -> instances
Parameters:
Name | Type | Description | Default |
---|---|---|---|
objects |
Iterable
|
An iterable of objects |
required |
types |
Union[Type[T], Tuple[Type[T], ...]]
|
A type or tuple of types to extract, defaults to all objects |
object
|
Returns:
Type | Description |
---|---|
Union[List[T], Dict[Type[T], T]]
|
If a single type is given: a list of instances of that type |
Union[List[T], Dict[Type[T], T]]
|
If a tuple of types is given: a mapping of type to a list of instances |
Source code in prefect/utilities/collections.py
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 |
|
flatdict_to_dict
¶
Converts a flattened dictionary back to a nested dictionary.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dct |
dict
|
The dictionary to be nested. Each key should be a tuple of keys
as generated by |
required |
Returns A nested dict of the same type as dct
Source code in prefect/utilities/collections.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
|
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.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dct |
Dict
|
The nested dictionary or list from which to fetch the value. |
required |
keys |
Union[str, List[str]]
|
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. |
required |
default |
Any
|
The default value to return if the requested key path does not exist. Defaults to None. |
None
|
Returns:
Type | Description |
---|---|
Any
|
The fetched value if the key exists, or the default value if it does not. |
get_from_dict({'a': {'b': {'c': [1, 2, 3, 4]}}}, 'a.b.c[1]') 2 get_from_dict({'a': {'b': [0, {'c': [1, 2]}]}}, ['a', 'b', 1, 'c', 1]) 2 get_from_dict({'a': {'b': [0, {'c': [1, 2]}]}}, 'a.b.1.c.2', 'default') 'default'
Source code in prefect/utilities/collections.py
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 |
|
isiterable
¶
Return a boolean indicating if an object is iterable.
Excludes types that are iterable but typically used as singletons: - str - bytes - IO objects
Source code in prefect/utilities/collections.py
138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
|
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.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
keys_to_remove |
List[Hashable]
|
A list of keys to remove from obj obj: The object to remove keys from. |
required |
Returns:
Type | Description |
---|---|
|
Source code in prefect/utilities/collections.py
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 |
|
visit_collection
¶
This function visits 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.
Note that when using return_data
a copy of each collection is created to avoid
mutating the original object. This may have significant performance penalties and
should only be used if you intend to transform the collection.
Supported types: - List - Tuple - Set - Dict (note: keys are also visited recursively) - Dataclass - Pydantic model - Prefect annotations
Parameters:
Name | Type | Description | Default |
---|---|---|---|
expr |
Any
|
a Python object or expression |
required |
visit_fn |
Callable[[Any], Awaitable[Any]]
|
an async function that will be applied to every non-collection element of expr. |
required |
return_data |
bool
|
if |
False
|
max_depth |
int
|
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. |
-1
|
context |
Optional[dict]
|
An optional dictionary. If passed, the context will be sent to each
call to the The context will be automatically populated with an 'annotation' key when
visiting collections within a |
None
|
remove_annotations |
bool
|
If set, annotations will be replaced by their contents. By default, annotations are preserved but their contents are visited. |
False
|
Source code in prefect/utilities/collections.py
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
|