Skip to content

Session

🛈 DocStrings

Source code in src/datacook/engine/session.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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
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
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
class Session:
    def __init__(
        self,
        *,
        memory_budget: int | None = None,
        registry: Registry[type[Op]] | None = None,
        executor: concurrent.futures.Executor | None = None,
    ) -> None:
        self.registry = registry if registry is not None else default_ops()
        self._lock = threading.RLock()
        self._nodes: dict[str, Node] = {}
        self._children: dict[str, list[str]] = {}
        self._cache = FrameCache(memory_budget if memory_budget is not None else default_budget())
        self._summaries: dict[str, tuple[int, FrameSummary]] = {}
        self._errors: dict[str, tuple[int, BaseException]] = {}
        self._inflight: dict[str, _Inflight] = {}
        self._generation = 0  # session-wide counter, see `_next_generation`
        self._callbacks: tuple[Callback, ...] = ()
        self._executor = executor
        self._own_executor = False

    # --- lifecycle ---------------------------------------------------------------------
    def close(self) -> None:
        with self._lock:
            executor, own = self._executor, self._own_executor
            self._executor, self._own_executor = None, False
            self._cache.clear()
        if executor is not None and own:
            executor.shutdown(wait=True, cancel_futures=True)

    def __enter__(self) -> Self:
        return self

    def __exit__(self, *exc: object) -> None:
        self.close()

    @property
    def executor(self) -> concurrent.futures.Executor:
        with self._lock:
            if self._executor is None:
                self._executor = concurrent.futures.ThreadPoolExecutor(
                    max_workers=2, thread_name_prefix="datacook-session"
                )
                self._own_executor = True
            return self._executor

    # --- events ------------------------------------------------------------------------
    def subscribe(self, callback: Callback) -> Callable[[], None]:
        """Register an event callback; returns a function that unsubscribes it."""
        with self._lock:
            self._callbacks = (*self._callbacks, callback)

        def unsubscribe() -> None:
            with self._lock:
                self._callbacks = tuple(c for c in self._callbacks if c is not callback)

        return unsubscribe

    def _emit(self, *events: Event) -> None:
        callbacks = self._callbacks
        for event in events:
            for callback in callbacks:
                try:
                    callback(event)
                except Exception:
                    logger.exception("session event callback failed: %s", event)

    # --- tree --------------------------------------------------------------------------
    def get(self, node_id: str) -> Node | None:
        with self._lock:
            return self._nodes.get(node_id)

    def generations(self, node_ids: Iterable[str]) -> dict[str, int]:
        """Current generation of each existing node in `node_ids` (one consistent snapshot)."""
        with self._lock:
            return {nid: n.generation for nid in node_ids if (n := self._nodes.get(nid))}

    def _next_generation(self) -> int:
        """A generation never handed out before in this session (call under the lock)."""
        self._generation += 1
        return self._generation

    def __getitem__(self, node_id: str) -> Node:
        with self._lock:
            return self._nodes[node_id]

    def __contains__(self, node_id: object) -> bool:
        with self._lock:
            return node_id in self._nodes

    def __len__(self) -> int:
        with self._lock:
            return len(self._nodes)

    def nodes(self) -> tuple[Node, ...]:
        """All nodes, parents before children (insertion order)."""
        with self._lock:
            return tuple(self._nodes.values())

    def roots(self) -> tuple[Node, ...]:
        with self._lock:
            return tuple(n for n in self._nodes.values() if not n.parents)

    def children(self, node_id: str) -> tuple[Node, ...]:
        with self._lock:
            return tuple(self._nodes[c] for c in self._children.get(node_id, ()))

    def descendants(self, node_id: str) -> tuple[str, ...]:
        """Ids of all nodes depending on `node_id` (breadth first, without it)."""
        with self._lock:
            return tuple(self._descendants(node_id))

    def _descendants(self, node_id: str) -> list[str]:
        seen: dict[str, None] = {}
        queue = list(self._children.get(node_id, ()))
        while queue:
            nid = queue.pop(0)
            if nid not in seen:
                seen[nid] = None
                queue.extend(self._children.get(nid, ()))
        return list(seen)

    def ancestors(self, node_id: str) -> tuple[Node, ...]:
        """The node and everything it depends on, parents before children."""
        with self._lock:
            order: dict[str, None] = {}

            def visit(nid: str) -> None:
                if nid in order:
                    return
                for p in self._nodes[nid].parents:
                    visit(p)
                order[nid] = None

            visit(node_id)
            return tuple(self._nodes[n] for n in order)

    def op(self, node: Node | str) -> type[Op]:
        record = (self[node] if isinstance(node, str) else node).record
        return self.registry[record.op_id]

    def add(
        self,
        record: OpRecord,
        parents: Sequence[Node | str] = (),
        *,
        name: str | None = None,
        comment: str | None = None,
        node_id: str | None = None,
    ) -> Node:
        op = self.registry[record.op_id]
        parent_ids = tuple(p if isinstance(p, str) else p.id for p in parents)
        if not op.takes(len(parent_ids)):
            msg = f"{op.id} takes {op.inputs_text()} input(s), got {len(parent_ids)}"
            raise ValueError(msg)
        if not isinstance(record.params, op.Params):
            msg = f"params of {op.id} must be {op.Params.__qualname__}"
            raise TypeError(msg)
        with self._lock:
            missing = [p for p in parent_ids if p not in self._nodes]
            if missing:
                msg = f"unknown parent node(s): {missing}"
                raise KeyError(msg)
            nid = node_id or node_mod.new_id()
            if nid in self._nodes:
                msg = f"node id {nid!r} exists"
                raise ValueError(msg)
            node = node_mod.Node(
                nid,
                record,
                parent_ids,
                name=name,
                comment=comment,
                generation=self._next_generation(),
            )
            self._nodes[nid] = node
            self._children[nid] = []
            for p in parent_ids:
                self._children[p].append(nid)
        self._emit(Event(EventKind.ADDED, (nid,), node.generation))
        return node

    def update(
        self,
        node_id: str,
        record: OpRecord | None = None,
        *,
        params: Params | None = None,
        name: str | None = None,
        comment: str | None = None,
    ) -> Node:
        """Edit a node. New params invalidate it and all its descendants."""
        events: list[Event] = []
        with self._lock:
            node = self._nodes[node_id]
            if params is not None:
                record = type(node.record)(node.record.op_id, params)
            changes: dict[str, Any] = {}
            if name is not None:
                changes["name"] = name or None
            if comment is not None:
                changes["comment"] = comment or None
            invalidated: list[str] = []
            if record is not None and record != node.record:
                op = self.registry[record.op_id]
                if not op.takes(len(node.parents)) or not isinstance(record.params, op.Params):
                    msg = f"record does not fit node {node_id} ({op.id})"
                    raise ValueError(msg)
                changes["record"] = record
                invalidated = [node_id, *self._descendants(node_id)]
            if not changes:
                return node
            for nid in invalidated:
                n = self._nodes[nid]
                self._nodes[nid] = n.evolve(generation=self._next_generation())
                self._cache.discard(nid)
                self._summaries.pop(nid, None)
                self._errors.pop(nid, None)
            node = self._nodes[node_id].evolve(**changes)
            self._nodes[node_id] = node
            events.append(Event(EventKind.CHANGED, (node_id,), node.generation))
            if invalidated:
                events.append(Event(EventKind.INVALIDATED, tuple(invalidated), node.generation))
        self._emit(*events)
        return node

    def remove(self, node_id: str) -> tuple[str, ...]:
        """Remove a node and all its descendants; returns the removed ids."""
        with self._lock:
            removed = (node_id, *self._descendants(node_id))
            for nid in removed:
                node = self._nodes.pop(nid)
                self._children.pop(nid, None)
                for p in node.parents:
                    if p in self._children:
                        self._children[p].remove(nid)
                self._cache.discard(nid)
                self._summaries.pop(nid, None)
                self._errors.pop(nid, None)
        self._emit(Event(EventKind.REMOVED, removed))
        return removed

    def reparent(self, node_id: str, parents: Sequence[Node | str]) -> Node:
        """Give a node new parents (as many as before); invalidates it and its descendants.

        The node and its descendants move behind their new parents in `nodes()` order if
        needed, so "parents before children" still holds.
        """
        parent_ids = tuple(p if isinstance(p, str) else p.id for p in parents)
        with self._lock:
            node = self._nodes[node_id]
            if len(parent_ids) != len(node.parents):
                msg = f"{node.op_id} takes {len(node.parents)} input(s), got {len(parent_ids)}"
                raise ValueError(msg)
            missing = [p for p in parent_ids if p not in self._nodes]
            if missing:
                msg = f"unknown parent node(s): {missing}"
                raise KeyError(msg)
            descendants = self._descendants(node_id)
            if node_id in parent_ids or set(parent_ids) & set(descendants):
                msg = f"re-parenting {node_id} would create a cycle"
                raise ValueError(msg)
            if parent_ids == node.parents:
                return node
            for p in dict.fromkeys(node.parents):
                self._children[p] = [c for c in self._children[p] if c != node_id]
            for p in dict.fromkeys(parent_ids):
                self._children[p].append(node_id)
            self._nodes[node_id] = node.evolve(parents=parent_ids)
            invalidated = [node_id, *descendants]
            self._move_behind_parents(node_id, invalidated)
            for nid in invalidated:
                self._nodes[nid] = self._nodes[nid].evolve(generation=self._next_generation())
                self._cache.discard(nid)
                self._summaries.pop(nid, None)
                self._errors.pop(nid, None)
            node = self._nodes[node_id]
        self._emit(
            Event(EventKind.MOVED, (node_id,), node.generation),
            Event(EventKind.INVALIDATED, tuple(invalidated), node.generation),
        )
        return node

    def _move_behind_parents(self, node_id: str, subtree: Sequence[str]) -> None:
        """Keep insertion order topological: move `subtree` to the end if a parent is later."""
        order = {nid: i for i, nid in enumerate(self._nodes)}
        if all(order[p] < order[node_id] for p in self._nodes[node_id].parents):
            return
        members = set(subtree)
        for nid in [n for n in self._nodes if n in members]:  # keeps their relative order
            self._nodes[nid] = self._nodes.pop(nid)

    def remove_step(self, node_id: str) -> Node:
        """Delete one step and hand its children to its parent; returns the removed node.

        Raises `restructure.StepError` for sources, merges and (when the parent's schema is
        known) children that use columns the parent does not have.
        """
        from datacook.engine import restructure

        parent = restructure.check_remove_step(self, node_id)
        node = self[node_id]
        for child in self.children(node_id):
            self.reparent(child.id, [parent if p == node_id else p for p in child.parents])
        self.remove(node_id)
        return node

    def clear(self) -> None:
        for root in self.roots():
            if root.id in self:
                self.remove(root.id)

    # --- cache -------------------------------------------------------------------------
    @property
    def memory_budget(self) -> int:
        return self._cache.budget

    def set_memory_budget(self, budget: int) -> None:
        with self._lock:
            evicted = self._cache.set_budget(budget)
        self._emit_evicted(evicted)

    def memory(self) -> MemoryStats:
        with self._lock:
            keys = self._cache.keys()
            pinned = sum(1 for k in self._nodes if self._cache.is_pinned(k))
            return MemoryStats(self._cache.used, self._cache.budget, len(keys), pinned)

    def cached_ids(self) -> tuple[str, ...]:
        with self._lock:
            return tuple(self._cache.keys())

    def is_cached(self, node_id: str) -> bool:
        with self._lock:
            node = self._nodes.get(node_id)
            return node is not None and self._cache.get(node_id, node.generation) is not None

    def pin(self, node_id: str) -> None:
        """Keep the node's frame in the cache (counted pins)."""
        with self._lock:
            self._cache.pin(node_id)

    def unpin(self, node_id: str) -> None:
        with self._lock:
            evicted = self._cache.unpin(node_id)
        self._emit_evicted(evicted)

    def drop(self, node_id: str) -> None:
        """Drop a node's cached frame (it is recomputed on demand)."""
        with self._lock:
            dropped = self._cache.discard(node_id)
        if dropped:
            self._emit_evicted([node_id])

    def _emit_evicted(self, evicted: Sequence[str]) -> None:
        if evicted:
            self._emit(Event(EventKind.EVICTED, tuple(evicted)))

    # --- results -----------------------------------------------------------------------
    def summary(self, node_id: str) -> FrameSummary | None:
        """Shape, schema and size of the node's frame, if it was computed (no compute)."""
        with self._lock:
            node = self._nodes.get(node_id)
            entry = self._summaries.get(node_id)
            if node is None or entry is None or entry[0] != node.generation:
                return None
            return entry[1]

    def error(self, node_id: str) -> BaseException | None:
        """The exception of the last failed computation of the node's current generation."""
        with self._lock:
            node = self._nodes.get(node_id)
            entry = self._errors.get(node_id)
            if node is None or entry is None or entry[0] != node.generation:
                return None
            return entry[1]

    def window(
        self,
        node_id: str,
        start: int,
        stop: int,
        columns: Sequence[str] | None = None,
    ) -> Frame:
        """Rows `start:stop` of the node's frame (computes it if needed)."""
        return frames.window(self.materialize(node_id), start, stop, columns)

    def materialize(self, node_id: str) -> Frame:
        """The node's frame, from the cache or computed on this thread."""
        return self._materialize(node_id, None)

    def _materialize(self, node_id: str, token: _Token | None) -> Frame:
        while True:
            with self._lock:
                node = self._nodes[node_id]
                frame = self._cache.get(node_id, node.generation)
                if frame is not None:
                    return frame
                inflight = self._inflight.get(node_id)
                if inflight is None or inflight.generation != node.generation:
                    inflight = _Inflight(node.generation, threading.Event())
                    self._inflight[node_id] = inflight
                    break
            # another thread computes the same node and generation: wait for its outcome
            while not inflight.done.wait(0.05):
                if token is not None:
                    token.check()
            if inflight.frame is not None:
                return inflight.frame
            if isinstance(inflight.error, OpError):
                # a new exception per waiter: tracebacks are not shared between threads
                raise OpError(*inflight.error.args) from inflight.error
            if inflight.error is not None:
                raise inflight.error
            # the computing thread was cancelled: look again (and maybe compute ourselves)
        try:
            frame = self._compute(node, token)
        except Cancelled:
            self._abandon(node_id, inflight)
            raise
        except Exception as e:
            with self._lock:
                fresh = self._is_current(node)
                if fresh:
                    self._errors[node_id] = (node.generation, e)
                inflight.error = e
                self._release(node_id, inflight)
            inflight.done.set()
            if fresh:
                self._emit(Event(EventKind.ERROR, (node_id,), node.generation, e))
            raise
        except BaseException:  # SystemExit from custom code, KeyboardInterrupt, ...
            self._abandon(node_id, inflight)
            raise
        # measure outside the lock (walks every column / buffer)
        buffers = frames.buffers(frame)
        summary = frames.summary(frame, buffers)
        evicted: list[str] = []
        with self._lock:
            # cache the result *before* waiters wake up, in one locked section with
            # releasing the in-flight entry, so nobody sees neither and computes again
            fresh = self._is_current(node)
            if fresh:
                evicted = self._cache.put(node_id, node.generation, frame, buffers)
                self._summaries[node_id] = (node.generation, summary)
                self._errors.pop(node_id, None)
            inflight.frame = frame
            self._release(node_id, inflight)
        inflight.done.set()
        if fresh:
            self._emit(Event(EventKind.MATERIALIZED, (node_id,), node.generation))
        self._emit_evicted(evicted)
        return frame

    def _compute(self, node: Node, token: _Token | None) -> Frame:
        inputs = [self._materialize(p, token) for p in node.parents]
        if token is not None:
            token.check()
        return self._apply(node.record, inputs)

    def _is_current(self, node: Node) -> bool:
        current = self._nodes.get(node.id)
        return current is not None and current.generation == node.generation

    def _abandon(self, node_id: str, inflight: _Inflight) -> None:
        """End a computation without an outcome: waiters look again (and may compute)."""
        with self._lock:
            self._release(node_id, inflight)
        inflight.done.set()

    def _release(self, node_id: str, inflight: _Inflight) -> None:
        if self._inflight.get(node_id) is inflight:
            del self._inflight[node_id]

    def _apply(self, record: OpRecord, inputs: Sequence[Frame]) -> Frame:
        op = self.registry[record.op_id]
        args = [frames.ensure_backend(f, op.backend) for f in inputs]
        try:
            return op.apply(record.params, *args)
        except OpError:
            raise
        except Exception as e:
            raise OpError(f"{op.id}: {type(e).__name__}: {e}") from e

    def request(self, node_id: str) -> Request:
        """Materialise on the executor; returns a cancellable handle."""
        with self._lock:
            generation = self._nodes[node_id].generation
        token = _Token()
        future = self.executor.submit(self._materialize, node_id, token)
        return Request(self, node_id, generation, future, token)

    def evaluate(
        self,
        record: OpRecord,
        parents: Sequence[Node | str] = (),
        *,
        head: int | None = None,
    ) -> Frame:
        """Compute a candidate op on committed parents (preview); nothing is stored."""
        return self._evaluate(record, parents, head, None)

    def _evaluate(
        self,
        record: OpRecord,
        parents: Sequence[Node | str],
        head: int | None,
        token: _Token | None,
    ) -> Frame:
        ids = [p if isinstance(p, str) else p.id for p in parents]
        inputs = [self._materialize(p, token) for p in ids]
        if token is not None:
            token.check()
        result = self._apply(record, inputs)
        return result if head is None else frames.window(result, 0, head)

    def request_evaluate(
        self,
        record: OpRecord,
        parents: Sequence[Node | str] = (),
        *,
        head: int | None = None,
    ) -> Request:
        ids = [p if isinstance(p, str) else p.id for p in parents]
        with self._lock:
            missing = [p for p in ids if p not in self._nodes]
            if missing:
                msg = f"unknown parent node(s): {missing}"
                raise KeyError(msg)
            generations = {p: self._nodes[p].generation for p in ids}
        token = _Token()
        future = self.executor.submit(self._evaluate, record, ids, head, token)
        return Request(self, None, None, future, token, generations)

    # --- code --------------------------------------------------------------------------
    def code(self, node_ids: Iterable[str] | None = None) -> str:
        """Python script computing the given nodes (default: the whole tree)."""
        return self.render_code(node_ids).text

    def render_code(self, node_ids: Iterable[str] | None = None) -> codegen.Script:
        """Script plus the line span of each node's block (`codegen.render`)."""
        with self._lock:
            nodes = dict(self._nodes)
        return codegen.render(nodes, self.registry, targets=node_ids)

    def import_script(self, text: str) -> list[Node]:
        """Parse a script (see `parser.parse`) and add its steps as new nodes."""
        from datacook.engine import parser

        parsed = parser.parse(text, self.registry)
        created: list[Node] = []
        for step in parsed.steps:
            parents = [created[i] for i in step.parents]
            created.append(self.add(step.record, parents, name=step.name))
        return created

ancestors

ancestors(node_id: str) -> tuple[Node, ...]

The node and everything it depends on, parents before children.

Source code in src/datacook/engine/session.py
def ancestors(self, node_id: str) -> tuple[Node, ...]:
    """The node and everything it depends on, parents before children."""
    with self._lock:
        order: dict[str, None] = {}

        def visit(nid: str) -> None:
            if nid in order:
                return
            for p in self._nodes[nid].parents:
                visit(p)
            order[nid] = None

        visit(node_id)
        return tuple(self._nodes[n] for n in order)

code

code(node_ids: Iterable[str] | None = None) -> str

Python script computing the given nodes (default: the whole tree).

Source code in src/datacook/engine/session.py
def code(self, node_ids: Iterable[str] | None = None) -> str:
    """Python script computing the given nodes (default: the whole tree)."""
    return self.render_code(node_ids).text

descendants

descendants(node_id: str) -> tuple[str, ...]

Ids of all nodes depending on node_id (breadth first, without it).

Source code in src/datacook/engine/session.py
def descendants(self, node_id: str) -> tuple[str, ...]:
    """Ids of all nodes depending on `node_id` (breadth first, without it)."""
    with self._lock:
        return tuple(self._descendants(node_id))

drop

drop(node_id: str) -> None

Drop a node's cached frame (it is recomputed on demand).

Source code in src/datacook/engine/session.py
def drop(self, node_id: str) -> None:
    """Drop a node's cached frame (it is recomputed on demand)."""
    with self._lock:
        dropped = self._cache.discard(node_id)
    if dropped:
        self._emit_evicted([node_id])

error

error(node_id: str) -> BaseException | None

The exception of the last failed computation of the node's current generation.

Source code in src/datacook/engine/session.py
def error(self, node_id: str) -> BaseException | None:
    """The exception of the last failed computation of the node's current generation."""
    with self._lock:
        node = self._nodes.get(node_id)
        entry = self._errors.get(node_id)
        if node is None or entry is None or entry[0] != node.generation:
            return None
        return entry[1]

evaluate

evaluate(record: OpRecord, parents: Sequence[Node | str] = (), *, head: int | None = None) -> Frame

Compute a candidate op on committed parents (preview); nothing is stored.

Source code in src/datacook/engine/session.py
def evaluate(
    self,
    record: OpRecord,
    parents: Sequence[Node | str] = (),
    *,
    head: int | None = None,
) -> Frame:
    """Compute a candidate op on committed parents (preview); nothing is stored."""
    return self._evaluate(record, parents, head, None)

generations

generations(node_ids: Iterable[str]) -> dict[str, int]

Current generation of each existing node in node_ids (one consistent snapshot).

Source code in src/datacook/engine/session.py
def generations(self, node_ids: Iterable[str]) -> dict[str, int]:
    """Current generation of each existing node in `node_ids` (one consistent snapshot)."""
    with self._lock:
        return {nid: n.generation for nid in node_ids if (n := self._nodes.get(nid))}

import_script

import_script(text: str) -> list[Node]

Parse a script (see parser.parse) and add its steps as new nodes.

Source code in src/datacook/engine/session.py
def import_script(self, text: str) -> list[Node]:
    """Parse a script (see `parser.parse`) and add its steps as new nodes."""
    from datacook.engine import parser

    parsed = parser.parse(text, self.registry)
    created: list[Node] = []
    for step in parsed.steps:
        parents = [created[i] for i in step.parents]
        created.append(self.add(step.record, parents, name=step.name))
    return created

materialize

materialize(node_id: str) -> Frame

The node's frame, from the cache or computed on this thread.

Source code in src/datacook/engine/session.py
def materialize(self, node_id: str) -> Frame:
    """The node's frame, from the cache or computed on this thread."""
    return self._materialize(node_id, None)

nodes

nodes() -> tuple[Node, ...]

All nodes, parents before children (insertion order).

Source code in src/datacook/engine/session.py
def nodes(self) -> tuple[Node, ...]:
    """All nodes, parents before children (insertion order)."""
    with self._lock:
        return tuple(self._nodes.values())

pin

pin(node_id: str) -> None

Keep the node's frame in the cache (counted pins).

Source code in src/datacook/engine/session.py
def pin(self, node_id: str) -> None:
    """Keep the node's frame in the cache (counted pins)."""
    with self._lock:
        self._cache.pin(node_id)

remove

remove(node_id: str) -> tuple[str, ...]

Remove a node and all its descendants; returns the removed ids.

Source code in src/datacook/engine/session.py
def remove(self, node_id: str) -> tuple[str, ...]:
    """Remove a node and all its descendants; returns the removed ids."""
    with self._lock:
        removed = (node_id, *self._descendants(node_id))
        for nid in removed:
            node = self._nodes.pop(nid)
            self._children.pop(nid, None)
            for p in node.parents:
                if p in self._children:
                    self._children[p].remove(nid)
            self._cache.discard(nid)
            self._summaries.pop(nid, None)
            self._errors.pop(nid, None)
    self._emit(Event(EventKind.REMOVED, removed))
    return removed

remove_step

remove_step(node_id: str) -> Node

Delete one step and hand its children to its parent; returns the removed node.

Raises restructure.StepError for sources, merges and (when the parent's schema is known) children that use columns the parent does not have.

Source code in src/datacook/engine/session.py
def remove_step(self, node_id: str) -> Node:
    """Delete one step and hand its children to its parent; returns the removed node.

    Raises `restructure.StepError` for sources, merges and (when the parent's schema is
    known) children that use columns the parent does not have.
    """
    from datacook.engine import restructure

    parent = restructure.check_remove_step(self, node_id)
    node = self[node_id]
    for child in self.children(node_id):
        self.reparent(child.id, [parent if p == node_id else p for p in child.parents])
    self.remove(node_id)
    return node

render_code

render_code(node_ids: Iterable[str] | None = None) -> Script

Script plus the line span of each node's block (codegen.render).

Source code in src/datacook/engine/session.py
def render_code(self, node_ids: Iterable[str] | None = None) -> codegen.Script:
    """Script plus the line span of each node's block (`codegen.render`)."""
    with self._lock:
        nodes = dict(self._nodes)
    return codegen.render(nodes, self.registry, targets=node_ids)

reparent

reparent(node_id: str, parents: Sequence[Node | str]) -> Node

Give a node new parents (as many as before); invalidates it and its descendants.

The node and its descendants move behind their new parents in nodes() order if needed, so "parents before children" still holds.

Source code in src/datacook/engine/session.py
def reparent(self, node_id: str, parents: Sequence[Node | str]) -> Node:
    """Give a node new parents (as many as before); invalidates it and its descendants.

    The node and its descendants move behind their new parents in `nodes()` order if
    needed, so "parents before children" still holds.
    """
    parent_ids = tuple(p if isinstance(p, str) else p.id for p in parents)
    with self._lock:
        node = self._nodes[node_id]
        if len(parent_ids) != len(node.parents):
            msg = f"{node.op_id} takes {len(node.parents)} input(s), got {len(parent_ids)}"
            raise ValueError(msg)
        missing = [p for p in parent_ids if p not in self._nodes]
        if missing:
            msg = f"unknown parent node(s): {missing}"
            raise KeyError(msg)
        descendants = self._descendants(node_id)
        if node_id in parent_ids or set(parent_ids) & set(descendants):
            msg = f"re-parenting {node_id} would create a cycle"
            raise ValueError(msg)
        if parent_ids == node.parents:
            return node
        for p in dict.fromkeys(node.parents):
            self._children[p] = [c for c in self._children[p] if c != node_id]
        for p in dict.fromkeys(parent_ids):
            self._children[p].append(node_id)
        self._nodes[node_id] = node.evolve(parents=parent_ids)
        invalidated = [node_id, *descendants]
        self._move_behind_parents(node_id, invalidated)
        for nid in invalidated:
            self._nodes[nid] = self._nodes[nid].evolve(generation=self._next_generation())
            self._cache.discard(nid)
            self._summaries.pop(nid, None)
            self._errors.pop(nid, None)
        node = self._nodes[node_id]
    self._emit(
        Event(EventKind.MOVED, (node_id,), node.generation),
        Event(EventKind.INVALIDATED, tuple(invalidated), node.generation),
    )
    return node

request

request(node_id: str) -> Request

Materialise on the executor; returns a cancellable handle.

Source code in src/datacook/engine/session.py
def request(self, node_id: str) -> Request:
    """Materialise on the executor; returns a cancellable handle."""
    with self._lock:
        generation = self._nodes[node_id].generation
    token = _Token()
    future = self.executor.submit(self._materialize, node_id, token)
    return Request(self, node_id, generation, future, token)

subscribe

subscribe(callback: Callback) -> Callable[[], None]

Register an event callback; returns a function that unsubscribes it.

Source code in src/datacook/engine/session.py
def subscribe(self, callback: Callback) -> Callable[[], None]:
    """Register an event callback; returns a function that unsubscribes it."""
    with self._lock:
        self._callbacks = (*self._callbacks, callback)

    def unsubscribe() -> None:
        with self._lock:
            self._callbacks = tuple(c for c in self._callbacks if c is not callback)

    return unsubscribe

summary

summary(node_id: str) -> FrameSummary | None

Shape, schema and size of the node's frame, if it was computed (no compute).

Source code in src/datacook/engine/session.py
def summary(self, node_id: str) -> FrameSummary | None:
    """Shape, schema and size of the node's frame, if it was computed (no compute)."""
    with self._lock:
        node = self._nodes.get(node_id)
        entry = self._summaries.get(node_id)
        if node is None or entry is None or entry[0] != node.generation:
            return None
        return entry[1]

update

update(
    node_id: str,
    record: OpRecord | None = None,
    *,
    params: Params | None = None,
    name: str | None = None,
    comment: str | None = None,
) -> Node

Edit a node. New params invalidate it and all its descendants.

Source code in src/datacook/engine/session.py
def update(
    self,
    node_id: str,
    record: OpRecord | None = None,
    *,
    params: Params | None = None,
    name: str | None = None,
    comment: str | None = None,
) -> Node:
    """Edit a node. New params invalidate it and all its descendants."""
    events: list[Event] = []
    with self._lock:
        node = self._nodes[node_id]
        if params is not None:
            record = type(node.record)(node.record.op_id, params)
        changes: dict[str, Any] = {}
        if name is not None:
            changes["name"] = name or None
        if comment is not None:
            changes["comment"] = comment or None
        invalidated: list[str] = []
        if record is not None and record != node.record:
            op = self.registry[record.op_id]
            if not op.takes(len(node.parents)) or not isinstance(record.params, op.Params):
                msg = f"record does not fit node {node_id} ({op.id})"
                raise ValueError(msg)
            changes["record"] = record
            invalidated = [node_id, *self._descendants(node_id)]
        if not changes:
            return node
        for nid in invalidated:
            n = self._nodes[nid]
            self._nodes[nid] = n.evolve(generation=self._next_generation())
            self._cache.discard(nid)
            self._summaries.pop(nid, None)
            self._errors.pop(nid, None)
        node = self._nodes[node_id].evolve(**changes)
        self._nodes[node_id] = node
        events.append(Event(EventKind.CHANGED, (node_id,), node.generation))
        if invalidated:
            events.append(Event(EventKind.INVALIDATED, tuple(invalidated), node.generation))
    self._emit(*events)
    return node

window

window(node_id: str, start: int, stop: int, columns: Sequence[str] | None = None) -> Frame

Rows start:stop of the node's frame (computes it if needed).

Source code in src/datacook/engine/session.py
def window(
    self,
    node_id: str,
    start: int,
    stop: int,
    columns: Sequence[str] | None = None,
) -> Frame:
    """Rows `start:stop` of the node's frame (computes it if needed)."""
    return frames.window(self.materialize(node_id), start, stop, columns)

:fa-brands-github: Show source on GitHub