pfun.effect.Effect
dataclass
Wrapper for functions of type Callable[[R], Awaitable[pfun.Either[E, A]]]
that are allowed to perform side-effects
__call__(self, r, max_processes=None, max_threads=None)
special
Run the function wrapped by this Effect
asynchronously, including potential side-effects. If the function fails the resulting error will be raised as an exception.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
r |
R |
The dependency with which to run this |
required |
max_processes |
int |
The max number of processes used to run cpu bound parts of this effect |
None |
max_threads |
int |
The max number of threads used to run io bound parts of this effect |
None |
Returns:
Type | Description |
---|---|
A |
The succesful result of the wrapped function if it succeeds |
Exceptions:
Type | Description |
---|---|
E |
If the Effect fails and |
RuntimeError |
if the effect fails and |
and_then(self, f)
Create new Effect
that applies f
to the result of running this effect successfully. If this Effect
fails, f
is not applied.
Examples:
1 2 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f |
Callable[[A], Union[Awaitable[Effect[Any, E2, B]], Effect[Any, E2, B]]] |
Function to pass the result of this |
required |
Returns:
Type | Description |
---|---|
Effect[Any, Union[E, E2], B] |
New |
discard_and_then(self, effect)
Create a new effect that discards the result of this effect, and produces instead effect
. Like and_then
but does not require
you to handle the result. Convenient for effects that produce None
, like writing to files.
Examples:
1 2 3 4 5 6 7 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
effect |
Effect[Any, E2, B] |
|
required |
Returns:
Type | Description |
---|---|
Effect[Any, Union[E, E2], B] |
New effect that succeeds with |
either(self)
Push the potential error into the success channel as an either, allowing error handling.
Examples:
1 2 3 4 5 |
|
Returns:
Type | Description |
---|---|
Effect[R, NoReturn, Either[E, A]] |
New |
ensure(self, effect)
Create an Effect
that will always run effect
, regardless
of whether this Effect
succeeds or fails. The result of
effect
is ignored, and the resulting effect instead succeeds or fails
with the succes or error value of this effect. Useful for closing
resources.
Examples:
1 2 3 4 5 6 7 8 9 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
effect |
Effect[Any, NoReturn, Any] |
|
required |
Returns:
Type | Description |
---|---|
Effect[Any, E, A] |
|
map(self, f)
Map f
over the result produced by this Effect
once it is run
Examples:
1 2 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f |
Callable[[A], Union[Awaitable[B], B]] |
function to map over this |
required |
Returns:
Type | Description |
---|---|
Effect[R, E, B] |
new |
memoize(self)
Create an Effect
that caches its result. When the effect is evaluated
for the second time, its side-effects are not performed, it simply
succeeds with the cached result. This means you should be careful with
memoizing complicated effects. Useful for effects that have expensive
results, such as calling a slow HTTP api or reading a large file.
Examples:
1 2 3 4 5 6 7 8 9 10 11 12 |
|
Returns:
Type | Description |
---|---|
Effect[R, E, A] |
memoized |
recover(self, f)
Create new Effect
that applies f
to the error result of running this effect if it fails. If this Effect
succeeds, f
is not applied.
Examples:
1 2 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f |
Callable[[E], Effect[Any, E2, B]] |
Function to pass the error result of this |
required |
Returns:
Type | Description |
---|---|
Effect[Any, E2, Union[A, B]] |
New : |
run(self, r, asyncio_run=<function run at 0x7fb19a737830>, max_processes=None, max_threads=None)
Run the function wrapped by this Effect
, including potential side-effects. If the function fails the resulting error will be raised as an exception.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
r |
R |
The dependency with which to run this |
required |
max_processes |
int |
The max number of processes used to run cpu bound parts of this effect |
None |
max_threads |
int |
The max number of threads used to run io bound parts of this effect |
None |
Returns:
Type | Description |
---|---|
A |
The succesful result of the wrapped function if it succeeds |
Exceptions:
Type | Description |
---|---|
E |
If the Effect fails and |
RuntimeError |
if the effect fails and |
pfun.effect.Success
Type-alias for Effect[object, NoReturn, TypeVar('A')]
.
pfun.effect.Try
Type-alias for Effect[object, TypeVar('E'), TypeVar('A')]
.
pfun.effect.Depends
Type-alias for Effect[TypeVar('R'), NoReturn, TypeVar('A')]
.
pfun.effect.success(value)
Wrap a function in Effect
that does nothing but return value
Examples:
1 2 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
value |
A1 |
The value to return when the |
required |
Returns:
Type | Description |
---|---|
Effect[object, NoReturn, A1] |
|
pfun.effect.error(reason)
Create an Effect
that does nothing but fail with reason
Examples:
1 2 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
reason |
E1 |
Value to fail with |
required |
Returns:
Type | Description |
---|---|
Effect[object, E1, NoReturn] |
|
pfun.effect.depend(r_type=None)
Get an Effect
that produces the dependency passed to run
when executed
Examples:
1 2 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
r_type |
Optional[Type[R1]] |
The expected dependency type of the resulting effect. Used ONLY for type-checking and doesn't impact runtime behaviour in any way |
None |
Returns:
Type | Description |
---|---|
Depends[R1, R1] |
|
pfun.effect.sequence_async(iterable)
Evaluate each Effect
in iterable
asynchronously
and collect the results
Examples:
1 2 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
iterable |
Iterable[Effect[R1, E1, A1]] |
The iterable to collect results from |
required |
Returns:
Type | Description |
---|---|
Effect[R1, E1, Iterable[A1]] |
|
pfun.effect.filter_(f, iterable)
Map each element in iterable
by applying f
,
filter the results by the value returned by f
and combine from left to right.
Examples:
1 2 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f |
Callable[[A], Effect[R1, E1, bool]] |
Function to map |
required |
iterable |
Iterable[A] |
Iterable to map by |
required |
Returns:
Type | Description |
---|---|
Effect[R1, E1, Iterable[A]] |
|
pfun.effect.for_each(f, iterable)
Map each in element in iterable
to
an Effect
by applying f
,
combine the elements by and_then
from left to right and collect the results
Examples:
1 2 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f |
Callable[[A1], Effect[R1, E1, B]] |
Function to map over |
required |
iterable |
Iterable[A1] |
Iterable to map |
required |
Returns:
Type | Description |
---|---|
Effect[R1, E1, Iterable[B]] |
|
pfun.effect.absolve(effect)
Move the error type from an Effect
producing an Either
into the error channel of the Effect
Examples:
1 2 3 4 5 6 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
effect |
Effect[R1, NoReturn, Either[E1, A1]] |
an |
required |
Returns:
Type | Description |
---|---|
Effect[R1, E1, A1] |
an |
pfun.effect.combine(*effects)
Create an effect that produces the result of calling the passed function with the results of effects in effects
Examples:
1 2 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
effects |
Effect[R1, E1, A2] |
Effects the results of which to pass to the combiner function |
() |
Returns:
Type | Description |
---|---|
Callable[[Callable[..., Union[Awaitable[A1], A1]]], Effect[Any, Any, A1]] |
function that takes a combiner function and returns an |
pfun.effect.lift
Decorator that enables decorated functions to operate on Effect
instances.
Examples:
1 2 3 4 |
|
__call__(self, *effects)
special
Parameters:
Name | Type | Description | Default |
---|---|---|---|
args |
|
|
required |
Returns:
Type | Description |
---|---|
Effect |
|
__init__(self, f)
special
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f |
L |
The function to decorate |
required |
pfun.effect.catch
dataclass
Decorator that catches errors as an Effect
. If the decorated
function performs additional side-effects, they are not carried out
until the effect is run.
Examples:
1 2 3 4 5 |
|
__call__(self, f)
special
Decorate f
to catch exceptions as an Effect
__init__(self, error, *errors)
special
Parameters:
Name | Type | Description | Default |
---|---|---|---|
error |
Type[EX] |
The first exception type to catch |
required |
errors |
Type[EX] |
The remaining exception types to catch |
() |
pfun.effect.from_awaitable(awaitable)
Create an Effect
that produces the result of awaiting awaitable
Examples:
1 2 3 4 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
awaitable |
Awaitable[A1] |
Awaitable to await in the resulting |
required |
Returns:
Type | Description |
---|---|
Effect[object, NoReturn, A1] |
|
pfun.effect.from_callable(f)
Create an Effect
from a function that takes a dependency and returns an Either
Examples:
1 2 3 4 5 6 7 8 9 10 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f |
Callable[[R1], Union[Awaitable[Either[E1, A1]], Either[E1, A1]]] |
the function to turn into an |
required |
Returns:
Type | Description |
---|---|
Effect[R1, E1, A1] |
|
pfun.effect.cpu_bound(f)
Decorator for functions that should be executed in separate a separate process during effect evaluation
Examples:
1 2 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f |
F1 |
The function to decorate |
required |
Returns:
Type | Description |
---|---|
F1 |
|
pfun.effect.io_bound(f)
Decorator for functions that should be executed in separate a separate thread during effect evaluation
Examples:
1 2 |
|
Parameters:
Name | Type | Description | Default |
---|---|---|---|
f |
F1 |
The function to decorate |
required |
Returns:
Type | Description |
---|---|
F1 |
|