MCPcopy Create free account
hub / github.com/dask/dask / collections_to_expr

Function collections_to_expr

dask/base.py:428–480  ·  view source on GitHub ↗

Convert many collections into a single dask expression. Typically, users should not be required to interact with this function. Parameters ---------- collections : Iterable An iterable of dask collections to be combined. optimize_graph : bool, optional If t

(
    collections: Iterable,
    optimize_graph: bool = True,
)

Source from the content-addressed store, hash-verified

426
427
428def collections_to_expr(
429 collections: Iterable,
430 optimize_graph: bool = True,
431) -> Expr:
432 """
433 Convert many collections into a single dask expression.
434
435 Typically, users should not be required to interact with this function.
436
437 Parameters
438 ----------
439 collections : Iterable
440 An iterable of dask collections to be combined.
441 optimize_graph : bool, optional
442 If this is True and collections are encountered which are backed by
443 legacy HighLevelGraph objects, the returned Expression will run a low
444 level task optimization during materialization.
445 """
446 is_iterable = False
447 if isinstance(collections, (tuple, list, set)):
448 is_iterable = True
449 else:
450 collections = [collections]
451 if not collections:
452 raise ValueError("No collections provided")
453 from dask._expr import CompositeExpr, Expr, HLGExpr, _ExprSequence
454
455 graphs = []
456 for coll in collections:
457 from dask.delayed import Delayed
458
459 if isinstance(coll, Delayed):
460 graphs.append(HLGExpr.from_collection(coll, optimize_graph=optimize_graph))
461 else:
462 expr = getattr(coll, "expr", None)
463 if isinstance(expr, Expr):
464 graphs.append(expr)
465 continue
466
467 dask_exprs = getattr(coll, "__dask_exprs__", None)
468 if dask_exprs is not None:
469 exprs = dask_exprs()
470 if exprs is not None:
471 exprs = tuple(exprs)
472 if exprs:
473 graphs.append(CompositeExpr(coll, *exprs))
474 continue
475 graphs.append(HLGExpr.from_collection(coll, optimize_graph=optimize_graph))
476
477 if len(graphs) > 1 or is_iterable:
478 return _ExprSequence(*graphs)
479 else:
480 return graphs[0]
481
482
483def _rebuild_composite_collection(

Calls 3

CompositeExprClass · 0.90
_ExprSequenceClass · 0.90
from_collectionMethod · 0.80