core: add helper for computing stats; use it in modules
This commit is contained in:
parent
a94b64c273
commit
1cc4eb5d8d
10 changed files with 79 additions and 3 deletions
|
@ -1,5 +1,8 @@
|
|||
# this file only keeps the most common & critical types/utility functions
|
||||
from .common import PathIsh, Paths, Json
|
||||
from .common import get_files, LazyLogger
|
||||
from .common import get_files
|
||||
from .common import LazyLogger
|
||||
from .common import warn_if_empty
|
||||
from .common import stat
|
||||
|
||||
from .cfg import make_config
|
||||
|
|
|
@ -133,6 +133,10 @@ def modules_check(args):
|
|||
stats = getattr(mod, 'stats', None)
|
||||
if stats is None:
|
||||
continue
|
||||
from . import common
|
||||
common.QUICK_STATS = True
|
||||
# todo make it a cmdline option..
|
||||
|
||||
try:
|
||||
res = stats()
|
||||
except Exception as ee:
|
||||
|
|
|
@ -338,3 +338,30 @@ def warn_if_empty(f):
|
|||
res = f(*args, **kwargs)
|
||||
return _warn_iterable(res, f=f)
|
||||
return wrapped # type: ignore
|
||||
|
||||
|
||||
# hacky hook to speed up for 'hpi doctor'
|
||||
# todo think about something better
|
||||
QUICK_STATS = False
|
||||
|
||||
|
||||
C = TypeVar('C')
|
||||
# todo not sure about return type...
|
||||
def stat(func: Callable[[], Iterable[C]]) -> Dict[str, Any]:
|
||||
from more_itertools import ilen, take, first
|
||||
|
||||
it = iter(func())
|
||||
res: Any
|
||||
if QUICK_STATS:
|
||||
initial = take(100, it)
|
||||
res = len(initial)
|
||||
if first(it, None) is not None: # todo can actually be none...
|
||||
# haven't exhausted
|
||||
res = f'{res}+'
|
||||
else:
|
||||
res = ilen(it)
|
||||
|
||||
|
||||
return {
|
||||
func.__name__: res,
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue