towhee.functional.mixins.safe.SafeMixin¶
- class towhee.functional.mixins.safe.SafeMixin[source]¶
Bases:
object
Mixin for exception safety.
Methods
Unbox Option values and drop Empty.
Making the data collection exception-safe by warp elements with Option.
Unbox Option values and fill Empty with default values.
Shortcut for exception_safe.
- drop_empty(callback: Callable = None) DataCollection [source]¶
Unbox Option values and drop Empty.
- Parameters:
callback (Callable) – handler for empty values;
- Returns:
A DataCollection that drops empty values;
- Return type:
Examples:
>>> from towhee import DataCollection >>> dc = DataCollection.range(5) >>> dc.safe().map(lambda x: x / (0 if x == 3 else 2)).drop_empty().to_list() [0.0, 0.5, 1.0, 2.0]
Get inputs that case exceptions:
>>> exception_inputs = [] >>> result = dc.safe().map(lambda x: x / (0 if x == 3 else 2)).drop_empty(lambda x: exception_inputs.append(x.get().value)) >>> exception_inputs [3]
- exception_safe()[source]¶
Making the data collection exception-safe by warp elements with Option.
Examples:
Exception breaks pipeline execution:
>>> from towhee import DataCollection >>> dc = DataCollection.range(5) >>> dc.map(lambda x: x / (0 if x == 3 else 2)).to_list() Traceback (most recent call last): ZeroDivisionError: division by zero
Exception-safe execution
>>> dc.exception_safe().map(lambda x: x / (0 if x == 3 else 2)).to_list() [Some(0.0), Some(0.5), Some(1.0), Empty(), Some(2.0)]
>>> dc.exception_safe().map(lambda x: x / (0 if x == 3 else 2)).filter(lambda x: x < 1.5).to_list() [Some(0.0), Some(0.5), Some(1.0), Empty()]
>>> dc.exception_safe().map(lambda x: x / (0 if x == 3 else 2)).filter(lambda x: x < 1.5, drop_empty=True).to_list() [Some(0.0), Some(0.5), Some(1.0)]
- fill_empty(default: Any = None) DataCollection [source]¶
Unbox Option values and fill Empty with default values.
- Parameters:
default (Any) – default value to replace empty values;
- Returns:
data collection with empty values filled with default;
- Return type:
Examples:
>>> from towhee import DataCollection >>> dc = DataCollection.range(5) >>> dc.safe().map(lambda x: x / (0 if x == 3 else 2)).fill_empty(-1.0).to_list() [0.0, 0.5, 1.0, -1.0, 2.0]