Skip to content

Commit

Permalink
functiontools: revamp of internal naming
Browse files Browse the repository at this point in the history
  • Loading branch information
ebonnal committed Oct 10, 2024
1 parent ae5e164 commit a6e07fa
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 21 deletions.
8 changes: 5 additions & 3 deletions streamable/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
)
from streamable.util.constants import NO_REPLACEMENT
from streamable.util.exceptions import NoopStopIteration
from streamable.util.functiontools import reraise_as
from streamable.util.functiontools import catch_and_raise_as
from streamable.util.validationtools import (
validate_concurrency,
validate_group_interval,
Expand Down Expand Up @@ -95,7 +95,7 @@ def group(
else:
interval_seconds = interval.total_seconds()
if by is not None:
by = reraise_as(by, StopIteration, NoopStopIteration)
by = catch_and_raise_as(by, StopIteration, NoopStopIteration)
return GroupingByIterator(iterator, size, interval_seconds, by)
return GroupingIterator(iterator, size, interval_seconds)

Expand All @@ -109,7 +109,9 @@ def map(
) -> Iterator[U]:
validate_iterator(iterator)
validate_concurrency(concurrency)
transformation = reraise_as(transformation, StopIteration, NoopStopIteration)
transformation = catch_and_raise_as(
transformation, StopIteration, NoopStopIteration
)
if concurrency == 1:
return builtins.map(transformation, iterator)
else:
Expand Down
6 changes: 3 additions & 3 deletions streamable/iters.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
cast,
)

from streamable.util.functiontools import reraise_as
from streamable.util.functiontools import catch_and_raise_as

T = TypeVar("T")
U = TypeVar("U")
Expand Down Expand Up @@ -90,7 +90,7 @@ def __next__(self) -> U:
return next(self._current_iterator_elem)
except StopIteration:
iterable_elem = next(self.iterator)
self._current_iterator_elem = reraise_as(
self._current_iterator_elem = catch_and_raise_as(
iter, StopIteration, NoopStopIteration
)(iterable_elem)

Expand Down Expand Up @@ -569,7 +569,7 @@ def __iter__(self) -> Iterator[Union[T, RaisingIterator.ExceptionContainer]]:
except StopIteration:
break
try:
iterator = reraise_as(
iterator = catch_and_raise_as(
iter, StopIteration, NoopStopIteration
)(iterable)
except Exception as e:
Expand Down
26 changes: 13 additions & 13 deletions streamable/util/functiontools.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,33 @@
R = TypeVar("R")


class ErrorMappedFunc(Generic[T, R]):
class CatchAndRaiseAs(Generic[T, R]):
def __init__(
self,
func: Callable[[T], R],
source_error_type: Type[Exception],
target_error_type: Type[Exception],
catched_error: Type[Exception],
raised_error: Type[Exception],
) -> None:
self.func = func
self.source_error_type = source_error_type
self.target_error_type = target_error_type
self.catched_error = catched_error
self.raised_error = raised_error

def __call__(self, arg: T) -> R:
try:
return self.func(arg)
except self.source_error_type as source:
raise self.target_error_type() from source
except self.catched_error as e:
raise self.raised_error(str(e)) from e


def reraise_as(
def catch_and_raise_as(
func: Callable[[T], R],
source_error_type: Type[Exception],
target_error_type: Type[Exception],
catched_error: Type[Exception],
raised_error: Type[Exception],
) -> Callable[[T], R]:
return ErrorMappedFunc(func, source_error_type, target_error_type)
return CatchAndRaiseAs(func, catched_error, raised_error)


class SidifiedFunc(Generic[T]):
class Sidify(Generic[T]):
def __init__(self, func: Callable[[T], Any]) -> None:
self.func = func

Expand All @@ -40,7 +40,7 @@ def __call__(self, arg: T) -> T:


def sidify(func: Callable[[T], Any]) -> Callable[[T], T]:
return SidifiedFunc(func)
return Sidify(func)


def async_sidify(
Expand Down
6 changes: 4 additions & 2 deletions streamable/visitors/iterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
ThrottleStream,
TruncateStream,
)
from streamable.util.functiontools import async_sidify, reraise_as, sidify
from streamable.util.functiontools import async_sidify, catch_and_raise_as, sidify
from streamable.visitors import Visitor

T = TypeVar("T")
Expand All @@ -34,7 +34,9 @@ def visit_catch_stream(self, stream: CatchStream[T]) -> Iterator[T]:

def visit_filter_stream(self, stream: FilterStream[T]) -> Iterator[T]:
return filter(
reraise_as(stream._keep, StopIteration, functions.NoopStopIteration),
catch_and_raise_as(
stream._keep, StopIteration, functions.NoopStopIteration
),
cast(Iterable[T], stream.upstream.accept(self)),
)

Expand Down

0 comments on commit a6e07fa

Please sign in to comment.