pypekit
pypekit.core
Core building blocks for pypekit.
- Task - the abstract base every processing step inherits from.
- Pipeline - a linear chain of tasks.
- Repository - a tree builder that discovers all possible pipelines from a set of tasks.
- CachedExecutor - a convenience wrapper to run many pipelines while caching intermediate results.
CachedExecutor
Run many pipelines while memoising intermediate results.
Source code in pypekit/core.py
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 | |
__init__(pipelines, cache=None, verbose=False)
Create an executor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pipelines
|
list[Pipeline]
|
Pipelines to execute. |
required |
cache
|
dict or None
|
Pre-populated cache mapping task signatures to cached
outputs and runtimes. If |
None
|
verbose
|
bool
|
Emit human-readable progress to stdout. |
``False``
|
Source code in pypekit/core.py
run(input_=None, run_config=None)
Execute every pipeline and return a nested results dict.
Caching is keyed by a signature consisting of a stable hash of
input_ and run_config followed by the UUIDs of every task
executed up to that point. If the signature already exists, the
cached value is used instead of re-running the task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_
|
Any
|
Input object forwarded to the first task of each pipeline. |
None
|
run_config
|
dict or None
|
Extra configuration propagated to every task via its
|
None
|
Returns:
| Type | Description |
|---|---|
dict[str, dict[str, Any]]
|
Mapping pipeline ID → {output, runtime, tasks} where
runtime is a |
Source code in pypekit/core.py
Node
Tree node holding a :class:Task instance.
Source code in pypekit/core.py
__init__(task, parent=None)
add_child(child)
Attach child to the current node.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
child
|
Node
|
The node to attach. |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the task types are incompatible - i.e. none of the
current node's |
Source code in pypekit/core.py
Pipeline
Bases: Task
A linear sequence of :class:Task objects.
The class itself derives from :class:Task so that pipelines can be
nested if desired. Its public interface mirrors a single task:
instantiate it, then call :py:meth:run.
Source code in pypekit/core.py
__init__(tasks=None)
Create a pipeline.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tasks
|
list[Task] or None
|
Tasks that make up the pipeline in order. |
None
|
Source code in pypekit/core.py
run(input_=None)
Execute all tasks sequentially.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_
|
Any
|
The argument passed to the first task in the chain. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
Output of the last task. |
Source code in pypekit/core.py
Repository
Utility class that builds every valid pipeline from a task pool.
Source code in pypekit/core.py
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 | |
__init__(tasks=None)
Create a repository.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tasks
|
list[Task | Type[Task] | tuple[Type[Task], dict[str, Any]]] or None
|
Tasks to include in the repository. Each task can be either
an instance of a :class: |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If the provided tasks are not valid (i.e. not instances of
:class: |
TypeError
|
If the provided tasks are not of the expected type (i.e. not
instances of :class: |
Source code in pypekit/core.py
build_pipelines()
Generate a list of all valid pipelines from the task tree.
Each pipeline is a linear sequence of tasks starting from the root node and ending at a leaf node (sink).
Returns:
| Type | Description |
|---|---|
list[Pipeline]
|
A list of pipelines, each represented as a :class: |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the tree has not been built yet (i.e. no root node exists). |
Source code in pypekit/core.py
build_tree(max_depth=sys.getrecursionlimit())
Generate a tree where paths map to potential pipelines.
Starting from an implicit :class:Root node, the method attaches
every task whose input_types match the current node's
output_types. Branches that do not end in a task with
output_types == ["sink"] are pruned afterwards.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_depth
|
int
|
Maximum depth of the tree. |
``sys.getrecursionlimit()``
|
Returns:
| Type | Description |
|---|---|
Node
|
The tree's root node. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no valid leaf (sink) can be found. |
Source code in pypekit/core.py
build_tree_string()
Generate a human-readable string representation of the task tree.
Returns:
| Type | Description |
|---|---|
str
|
A string representation of the task tree, where each line represents a task and its children, indented according to their depth in the tree. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the tree has not been built yet (i.e. no root node exists). |
Source code in pypekit/core.py
Root
Bases: Task
A dummy task representing the root of a task tree.
This task does not perform any operation and simply returns the
input unchanged. It is used to start the task tree in a
:class:Repository.
Source code in pypekit/core.py
Task
Bases: ABC
Abstract base class for every processing step.
Subclasses must implement :py:meth:run and override the
class attributes input_types and output_types to describe
which data types they consume and emit. These strings are free-form
but should be consistent across your project so that
:class:Repository can wire tasks together automatically.
Attributes:
| Name | Type | Description |
|---|---|---|
run_config |
dict or None, class variable
|
Configuration passed down from a :class: |
input_types |
list[str], class variable
|
Declared input type identifiers. |
output_types |
list[str], class variable
|
Declared output type identifiers. |
id |
str
|
Randomly generated UUID assigned when the task is inserted into a pipeline or repository. |
kwargs |
dict
|
Keyword arguments used at instantiation time. |
Source code in pypekit/core.py
run(input_=None)
abstractmethod
Execute the task.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_
|
Any
|
Input object consumed by the task. |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The task's output. |