Skip to content

Commit 9bdd5ca

Browse files
committed
stuff
1 parent 5fec8a5 commit 9bdd5ca

2 files changed

Lines changed: 15 additions & 10 deletions

File tree

asyncutils/func.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ async def wrapper(*a, **k):
9292
except CRITICAL: raise Critical
9393
except expected as _:
9494
e = timer()-s
95-
if should_log: log.warning(f'function {f.__qualname__} encountered {type(_).__qualname__}: {_}')
95+
if should_log: log.warning(f'function {f.__qualname__} encountered {type(_).__qualname__} after {e:.{precision}f} {'nano'*ns}seconds: {_}')
9696
return wrap_exc(_), e
9797
return wraps(f)(wrapper)
9898
def retry(tries=None, delay=None, max_delay=None, backoff=None, jitter=None, exc=Exception, on_retry=lambda *_: None, on_success=lambda *_: None, random=_randinst.random):

asyncutils/func.pyi

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,33 @@ def every[T](intvl: float, /, *, stop_when: Future[T]|None=..., count_f: bool=..
1919
When using the supplied_args and supplied_kwargs parameters, maintain a reference to them so that you can edit the args and kwargs fed to the function on the fly.
2020
Finally, the function returns `default` or None if it was not passed, unless `stop_on_exc` is True or `default` is `RAISE`.'''
2121
def everymethod[T, R](intvl: float, /, *, stop_when_getter: Callable[[T], Future[R]]|None=..., count_f: bool=..., verbose: bool=..., stop_on_exc: bool=..., wait_first: bool=..., max_iterations: int=..., timer: Timer=..., supplied_args: Iterable[Any]=..., supplied_kwargs: Mapping[str, Any]=..., default: R=...) -> Callable[[_everymethodft[T, R]], _everymethodrvrv[T, R]]: '''The method version of `every`. `stop_when_getter`, if passed, should take `self` and returns a suitable future `stop_when`. Other parameters are as in `every`.'''
22-
def timer[T, **P](f: Callable[P, Awaitable[T]], /, *, precision: int=..., expected: Exceptable=..., log: bool=..., timer: Timer=..., ns: bool=...) -> Callable[P, Coroutine[Any, Any, tuple[T|_ExceptionWrapper, float]]]:
22+
def timer[T, **P](f: Callable[P, Awaitable[T]], /, *, precision: int=..., expected: Exceptable=..., should_log: bool=..., timer: Timer=..., ns: bool=...) -> Callable[P, Coroutine[Any, Any, tuple[T|_ExceptionWrapper, float]]]:
2323
'''Convert the function that returns an awaitable object into an async function that returns a tuple `(res_or_exc, elapsed)`.
2424
`timer` is used to count `elapsed`, the time required to execute the function.
25-
`res_or_exc` is the awaited result of the wrapped function or the exception thrown wrapped by `exceptions.wrap_exc`.'''
25+
`res_or_exc` is the awaited result of the wrapped function or the exception thrown wrapped by `exceptions.wrap_exc`.
26+
`precision` is the number of digits to keep in the time in logging, and `ns` whether the return value of the timer indicates time in nanoseconds.
27+
Any exception the wrapped function emits whose type is not in `expected` is propagated directly.'''
2628
def retry(tries: int=..., delay: float=..., max_delay: float=..., backoff: float=..., jitter: float=..., exc: Exceptable=..., on_retry: Callable[[int, BaseException], Any]=..., on_success: Callable[[int, float], Any]=..., random: Callable[[], float]=...) -> _frv: ...
2729
def throttle(lim: float, timer: Timer=...) -> _frv: ...
2830
def debounce(wait: float) -> _frv: ...
2931
def rate_limit(calls: int, period: float, timer: Timer=...) -> _frv: ...
30-
async def measure[T](f: Callable[[], Awaitable[T]], timer: Timer=...) -> tuple[T, float]: ...
32+
async def measure[T](f: Callable[[], Awaitable[T]], timer: Timer=...) -> tuple[T, float]: '''A simple version of `timer` for functions taking no arguments and returning awaitables.'''
3133
class benchmark(tuple[float, float, float, float, int]):
3234
'''Actually a function at runtime.'''
33-
def __new__(cls, f: Callable[[], Awaitable], /, times: int=..., warmup: int=...) -> Self: ...
35+
def __new__(cls, f: Callable[[], Awaitable], /, times: int=..., warmup: int=...) -> Self:
36+
'''`f` is the function to benchmark, which should take no arguments and return an awaitable.
37+
`times` is how many times the function should be run, which defaults to 1.
38+
`warmup` is the number of warmup rounds to call the function for; not included in the benchmark results. Default 0.'''
3439
@property
35-
def min(self) -> float: ...
40+
def min(self) -> float: '''The minimum execution time among all non-warmup calls.'''
3641
@property
37-
def max(self) -> float: ...
42+
def max(self) -> float: '''The maximum execution time among all non-warmup calls.'''
3843
@property
39-
def total(self) -> float: ...
44+
def total(self) -> float: '''The total execution time.'''
4045
@property
41-
def avg(self) -> float: ...
46+
def avg(self) -> float: '''`total`/`iterations`.'''
4247
@property
43-
def iterations(self) -> int: ...
48+
def iterations(self) -> int: '''The `times` constructor parameter.'''
4449
@type_check_only
4550
class _everymethodrvrv[T, R](Protocol):
4651
async def __call__(_, self: T, /, *a: Any, **k: Any) -> R|None: ...

0 commit comments

Comments
 (0)