base
Class info¶
Classes¶
| Name | Children | Inherits |
|---|---|---|
| CallOp datacook.engine.ops.base An op that is one pandas call. |
||
| Match datacook.engine.ops.base A recognised statement: the op's params, input variables and output variable. |
||
| NoParams datacook.engine.ops.base |
||
| Op datacook.engine.ops.base Base class for operations. Subclasses set the metadata class variables. |
||
| OpError datacook.engine.ops.base An op failed to apply (wraps the backend's exception). |
||
| OpRecord datacook.engine.ops.base What a node stores: the op id plus its (immutable) params. |
||
| Params datacook.engine.ops.base Base for op parameters: immutable, unknown keys rejected. |
|
🛈 DocStrings¶
Op base classes and op records.
An Op subclass is the behaviour (apply, code generation, code recognition) plus metadata;
an OpRecord (op id + frozen Params instance) is what a tree node stores and what project
files contain. Ops are stateless: all methods are classmethods taking the params.
Most ops are a single pandas call; CallOp derives apply, to_code and from_call from
one declaration (method / function, positional params), so execution and generated code
cannot drift. Ops working on selected columns derive from ops.series.ColumnOp.
CallOp
¶
Bases: Op
An op that is one pandas call.
Either method (called on the first input, remaining inputs as leading positional
arguments: out = a.merge(b, how="left")) or function (a dotted name in the generated
code, all inputs as leading positional arguments: out = pd.read_csv("x.csv")).
Params named in positional are passed positionally; the other params are passed as
keywords when they differ from their default.
Source code in src/datacook/engine/ops/base.py
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 | |
always
class-attribute
¶
Params passed even when they equal their default (defaults that differ from pandas).
fixed
class-attribute
¶
Keyword arguments that are not params, always passed first (e.g. axis="columns").
then
class-attribute
¶
Code appended to the call (".reset_index()"); finish() does the same in apply.
finish
classmethod
¶
literal_args
classmethod
¶
Literal positional (after skip) and keyword arguments of a call, else None.
Source code in src/datacook/engine/ops/base.py
params_from_call
classmethod
¶
params_from_call(args: list[Any], kwargs: dict[str, Any]) -> Params | None
The params of a call; None if not this op.
Source code in src/datacook/engine/ops/base.py
Match
dataclass
¶
A recognised statement: the op's params, input variables and output variable.
Source code in src/datacook/engine/ops/base.py
Op
¶
Base class for operations. Subclasses set the metadata class variables.
Source code in src/datacook/engine/ops/base.py
imports
class-attribute
¶
Import lines the generated code needs.
n_inputs
class-attribute
¶
Number of parent frames: 0 = source (reader), 2 = merge, ...
requires
class-attribute
¶
Importable modules needed at runtime (optional dependencies).
from_block
classmethod
¶
from_block(stmts: Sequence[stmt], bound: Mapping[str, Any]) -> Match | None
Recognise a multi-statement block produced by to_code (default: none).
from_code
classmethod
¶
from_code(stmt: stmt, bound: Mapping[str, Any]) -> Match | None
Recognise one statement produced by to_code; bound = known variable names.
Return None if the statement is not this op. Raise ValueError for a recognised
statement with invalid params.
Source code in src/datacook/engine/ops/base.py
imports_for
classmethod
¶
Import lines the generated code needs for params (default: cls.imports).
inputs_text
classmethod
¶
takes
classmethod
¶
to_code
classmethod
¶
Python statement(s) computing output from the input variables.
to_code_block
classmethod
¶
Like to_code, with the script's already-used variable names (for unique locals).
OpError
¶
OpRecord
dataclass
¶
What a node stores: the op id plus its (immutable) params.
Source code in src/datacook/engine/ops/base.py
Params
¶
assign_item
¶
(name, value) of an assign argument name=value or **{"name": value}.
Source code in src/datacook/engine/ops/base.py
assign_kwarg
¶
name=expr for DataFrame.assign, or **{"name": expr} if not a plain identifier.
Source code in src/datacook/engine/ops/base.py
assign_target
¶
(frame, keyword) of frame.assign(<one keyword>) on a bound frame variable.
Source code in src/datacook/engine/ops/base.py
assignment
¶
(target, value) of name = value, else None.
Source code in src/datacook/engine/ops/base.py
call_arguments
¶
call_arguments(
params: Params,
positional: Sequence[str] = (),
always: Sequence[str] = (),
skip: Sequence[str] = (),
) -> tuple[list[Any], dict[str, Any]]
Call arguments of params: positional ones in order, the others as keywords.
A keyword is passed when it is required, listed in always or differs from its
default; skip: params that are not call arguments (e.g. the selected columns).
Source code in src/datacook/engine/ops/base.py
column_refs
¶
Column names the params refer to, per input index ({0: ["a", "b"], 1: [...]}).
Read from the fields' UI hints (json_schema_extra={"widget": "column" | "columns" |
"column_map", "input": n}), also in nested models (e.g. Aggregation.column).
column_map refers to its keys. Order of first appearance, no duplicates.
Source code in src/datacook/engine/ops/base.py
format_call
¶
func(a, b, key=value), wrapped one argument per line when longer than 88 chars.
Source code in src/datacook/engine/ops/base.py
literal
¶
Python source of a literal value (str/int/float/bool/None, lists, tuples, dicts).
Strings use double quotes where possible (ruff / black style).
Source code in src/datacook/engine/ops/base.py
literal_value
¶
Evaluate a literal expression (plus float("nan") / float("inf")).
Raises ValueError for anything else.
Source code in src/datacook/engine/ops/base.py
string_values
¶
Every string anywhere in the params (keys included): names a step may create.
Source code in src/datacook/engine/ops/base.py
strip_tail
¶
value without the trailing code tail (".reset_index()"); None if it has none.