-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #33 from ridi/feature/public
Feature/public
- Loading branch information
Showing
24 changed files
with
491 additions
and
262 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,28 @@ | ||
import time | ||
from typing import Callable | ||
|
||
DEFAULT_MEMORIZE_TIMEOUT = 60 | ||
|
||
|
||
def memorize(timeout: int = DEFAULT_MEMORIZE_TIMEOUT): | ||
def _wrapper(func: Callable): | ||
_cache = {} | ||
_timeouts = {} | ||
|
||
def _decorator(*args, clear: bool = False, **kwargs): | ||
if clear: | ||
return func(*args, **kwargs) | ||
|
||
key = func.__name__ + str(args) + str(kwargs) | ||
if key not in _cache: | ||
_timeouts[key] = time.time() | ||
_cache[key] = func(*args, **kwargs) | ||
|
||
delta = time.time() - _timeouts[key] | ||
if delta > timeout: | ||
_timeouts[key] = time.time() | ||
_cache[key] = func(*args, **kwargs) | ||
|
||
return _cache[key] | ||
return _decorator | ||
return _wrapper |
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,49 @@ | ||
import traceback | ||
from functools import wraps | ||
from typing import Callable, Tuple, Type | ||
|
||
_DEFAULT_RETRY_COUNT = 5 | ||
|
||
|
||
class RetryFailException(Exception): | ||
pass | ||
|
||
|
||
def retry( | ||
retry_count: int = _DEFAULT_RETRY_COUNT, | ||
retriable_exceptions: Tuple[Type[BaseException]] = (BaseException,), | ||
print_stacktrace: bool = False, | ||
): | ||
def _decorator(func: Callable): | ||
def _wrapper(*args, **kwargs): | ||
stacktraces = [] | ||
|
||
for _ in range(0, retry_count): | ||
try: | ||
value = func(*args, **kwargs) | ||
except Exception as e: | ||
if is_retriable_exception(e, retriable_exceptions): | ||
stacktraces.append(traceback.format_exc()) | ||
continue | ||
|
||
raise e | ||
else: | ||
return value | ||
|
||
if print_stacktrace: | ||
print('[RetryFail][StackTrace] %s', stacktraces) | ||
|
||
raise RetryFailException | ||
|
||
return wraps(func)(_wrapper) | ||
|
||
return _decorator | ||
|
||
|
||
def is_retriable_exception(exception: Type[BaseException], retriable_exceptions: Tuple[Type[BaseException]]) -> bool: | ||
is_class = isinstance(exception, type) | ||
|
||
if is_class: | ||
return issubclass(exception, retriable_exceptions) | ||
|
||
return isinstance(exception, retriable_exceptions) |
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,5 @@ | ||
def bytes_to_int(by): | ||
result = 0 | ||
for b in by: | ||
result = result * 256 + int(b) | ||
return result |
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
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
Oops, something went wrong.