operations.general

Module for general purpose operations.

Classes

  • Operation - Executes custom operation with any function.

  • OperationPipeline - Chains multiple operations together.

class OperationPipeline(operation1: BaseOperation, operation2: BaseOperation, /, *operations: BaseOperation)[source]

Bases: OperationSearcherMixin

Pipeline for chaining multiple operations together. Applies each operation in sequence, passing the result to the next.

Example

>>> from soupsavvy.operations import Operation
... pipeline = Operation(int) | Operation(lambda x: x + 1)
... pipeline.execute("1")
2

Most common way of creating a pipeline is using the | operator on two operations.

OperationPipeline is operation-searcher mixin, which means it can be used to find information in IElement object directly with find methods. This way, it can be used as field in model or execute method can be replaced with find method, which would produce the same result.

__init__(operation1: BaseOperation, operation2: BaseOperation, /, *operations: BaseOperation) None[source]

Initializes OperationPipeline with multiple operations.

Parameters

operationsBaseOperation

BaseOperation instances to be chained together.

Raises

NotOperationException

If any of the input operations is not an instance of BaseOperation.

class Operation(func: Callable, *args, **kwargs)[source]

Bases: OperationSearcherMixin

Custom operation that wraps any function to be used with other soupsavvy components.

Example

>>> from soupsavvy.operations import Operation
... operation = Operation(str.lower)
... operation.execute("TEXT")
"text"

Operation is operation-searcher mixin, which means it can be used to find information in IElement directly with find methods. This way, it can be used as field in model or execute method can be replaced with find method, which would produce the same result.

__init__(func: Callable, *args, **kwargs) None[source]

Initializes Operation with provided function and optional arguments.

Parameters

funcCallable

Any callable object that can be called with one positional argument.

*argsAny

Additional positional arguments passed to the operation function.

**kwargsAny

Additional keyword arguments passed to the operation function.