-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add decorators and add error handling
- Loading branch information
Showing
19 changed files
with
672 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from pystreamapi.__stream import Stream | ||
from pystreamapi._streams.error.__levels import ErrorLevel | ||
|
||
__version__ = "0.2" | ||
__all__ = ["Stream"] | ||
__version__ = "0.3" | ||
__all__ = ["Stream", "ErrorLevel"] |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# pylint: disable=protected-access | ||
from pystreamapi._streams.error.__error import ErrorHandler, _sentinel | ||
|
||
|
||
def dropwhile(predicate, iterable, handler: ErrorHandler=None): | ||
""" | ||
Drop items from the iterable while predicate(item) is true. | ||
Afterward, return every element until the iterable is exhausted. | ||
""" | ||
it = iter(iterable) | ||
for x in it: | ||
if handler is not None: | ||
res = handler._one(mapper=predicate, item=x) | ||
else: | ||
res = predicate(x) | ||
if not res and res is not _sentinel: | ||
yield x | ||
break | ||
yield from it | ||
|
||
|
||
_initial_missing = object() | ||
|
||
def reduce(function, sequence, initial=_initial_missing, handler: ErrorHandler=None): | ||
""" | ||
reduce(function, iterable[, initial]) -> value | ||
Apply a function of two arguments cumulatively to the items of a sequence | ||
or iterable, from left to right, to reduce the iterable to a single | ||
value. For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates | ||
((((1+2)+3)+4)+5). If initial is present, it is placed before the items | ||
of the iterable in the calculation, and serves as a default when the | ||
iterable is empty. | ||
""" | ||
|
||
it = iter(sequence) | ||
|
||
if initial is _initial_missing: | ||
try: | ||
value = next(it) | ||
except StopIteration: | ||
raise TypeError( | ||
"reduce() of empty iterable with no initial value") from None | ||
else: | ||
value = initial | ||
|
||
for element in it: | ||
if handler is not None: | ||
new_value = handler._one(mapper=lambda x: function(value, x), item=element) | ||
if new_value is not _sentinel: | ||
value = new_value | ||
else: | ||
value = function(value, element) | ||
|
||
return value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from joblib import Parallel as _JoblibParallel, delayed # pylint: disable=unused-import | ||
|
||
from pystreamapi._streams.error.__error import ErrorHandler | ||
from pystreamapi._streams.error.__levels import ErrorLevel | ||
|
||
class Parallel: | ||
"""Wrapper for joblib.Parallel supporting error handling""" | ||
|
||
def __init__(self, n_jobs=-1, prefer="processes", handler: ErrorHandler=None): | ||
self.n_jobs = n_jobs | ||
self.prefer = prefer | ||
self.handler = handler | ||
|
||
def __call__(self, iterable): | ||
"""Call joblib.Parallel with error handling""" | ||
res = _JoblibParallel(n_jobs=self.n_jobs, prefer=self.prefer)(iterable) | ||
if self.handler and self.handler._get_error_level() != ErrorLevel.RAISE: | ||
return ErrorHandler._remove_sentinel(res) | ||
return res |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.