operations.wrappers
Module for operations wrappers, operations that control behavior of the wrapped operation.
Classes
SkipNone - Skips the operation if the input is None.
Suppress - Suppresses exceptions raised by the operation.
- class OperationWrapper(operation: BaseOperation)[source]
Bases:
OperationSearcherMixinA wrapper class for operations that extends the base operation functionality. Acts as a higher order operation, which controls behavior of the wrapped operation.
- __init__(operation: BaseOperation) None[source]
Initialize Operation Wrapper.
Parameters
- operationBaseOperation
The operation to be wrapped.
Raises
- NotOperationException
If provided object is not an instance of BaseOperation.
- class SkipNone(operation: BaseOperation)[source]
Bases:
OperationWrapperA wrapper that skips the operation if the input is None. Used to prevent exceptions where it’s safe and expected to skip the operation.
Example
>>> from soupsavvy.operations import Text ... from soupsavvy.models import SkipNone ... operation = SkipNone(Text()) ... operation.execute(None) None
When element was not found, which can be expected, skips operation and returns None.
- class Suppress(operation: ~soupsavvy.base.BaseOperation, category: ~typing.Type[Exception] | tuple[~typing.Type[Exception], ...] = <class 'Exception'>)[source]
Bases:
OperationWrapperA wrapper that executes the operation and suppresses exceptions raised, returning None instead. Used to catch exceptions where it’s expected this might happen.
Example
>>> from soupsavvy.operations import Operation ... from soupsavvy.models import Suppress ... operation = Suppress(Operation(int)) ... operation.execute("") None
This can be used with Default operation to provide a default value when the operation fails.
Example
>>> from soupsavvy.operations import Operation ... from soupsavvy.models import Suppress ... operation = Default(Suppress(Operation(int)), 0) ... operation.execute("") None
Operations in example can be used to try to convert string to integer from text of element, that can potentially be empty. In such case, if it’s not required, default can be set to None or known value.
Suppress also accepts category of exceptions to suppress, by default it suppresses all exceptions that inherit from Exception.
Example
>>> from soupsavvy.operations import Operation ... from soupsavvy.models import Suppress ... operation = Suppress(Operation(int), category=ValueError) ... operation.execute("not an integer") None
Category can be a tuple of exceptions as well, issubclass is used to check if the cause of exception is subclass of provided category.
Example
>>> from soupsavvy.operations import Operation ... from soupsavvy.models import Suppress ... operation = Suppress(Operation(int), category=(AttributeError, ValueError)) ... operation.execute("not an integer") FailedOperationExecution
- __init__(operation: ~soupsavvy.base.BaseOperation, category: ~typing.Type[Exception] | tuple[~typing.Type[Exception], ...] = <class 'Exception'>) None[source]
Initialize Suppress operation instance.
Parameters
- operationBaseOperation
The operation to be wrapped.
- categoryType[Exception] | tuple[Type[Exception], …], optional
The exception type(s) to suppress. By default, suppresses all exceptions that inherit from Exception.
Raises
- NotOperationException
If provided object is not an instance of BaseOperation.