|
9 | 9 | __all__ = ['spark_chars', 'UNSET', 'walk', 'globtastic', 'maybe_open', 'mkdir', 'image_size', 'bunzip', 'loads', 'loads_multi', |
10 | 10 | 'dumps', 'untar_dir', 'repo_details', 'run', 'open_file', 'save_pickle', 'load_pickle', 'parse_env', |
11 | 11 | 'expand_wildcards', 'dict2obj', 'obj2dict', 'repr_dict', 'is_listy', 'mapped', 'IterLen', |
12 | | - 'ReindexCollection', 'exec_eval', 'get_source_link', 'truncstr', 'sparkline', 'modify_exception', |
13 | | - 'round_multiple', 'set_num_threads', 'join_path_file', 'autostart', 'EventTimer', 'stringfmt_names', |
14 | | - 'PartialFormatter', 'partial_format', 'utc2local', 'local2utc', 'trace', 'modified_env', 'ContextManagers', |
15 | | - 'shufflish', 'console_help', 'hl_md', 'type2str', 'dataclass_src', 'Unset', 'nullable_dc', 'make_nullable', |
16 | | - 'flexiclass', 'asdict', 'is_typeddict', 'is_namedtuple', 'CachedIter', 'CachedAwaitable', 'reawaitable', |
17 | | - 'flexicache', 'time_policy', 'mtime_policy', 'timed_cache'] |
| 12 | + 'ReindexCollection', 'trim_wraps', 'save_iter', 'exec_eval', 'get_source_link', 'truncstr', 'sparkline', |
| 13 | + 'modify_exception', 'round_multiple', 'set_num_threads', 'join_path_file', 'autostart', 'EventTimer', |
| 14 | + 'stringfmt_names', 'PartialFormatter', 'partial_format', 'utc2local', 'local2utc', 'trace', 'modified_env', |
| 15 | + 'ContextManagers', 'shufflish', 'console_help', 'hl_md', 'type2str', 'dataclass_src', 'Unset', 'nullable_dc', |
| 16 | + 'make_nullable', 'flexiclass', 'asdict', 'is_typeddict', 'is_namedtuple', 'CachedIter', 'CachedAwaitable', |
| 17 | + 'reawaitable', 'flexicache', 'time_policy', 'mtime_policy', 'timed_cache'] |
18 | 18 |
|
19 | 19 | # %% ../nbs/03_xtras.ipynb |
20 | 20 | from .imports import * |
@@ -409,6 +409,28 @@ def __setstate__(self, s): self.coll,self.idxs,self.cache,self.tfm = s['coll'],s |
409 | 409 | shuffle="Randomly shuffle indices", |
410 | 410 | cache_clear="Clear LRU cache") |
411 | 411 |
|
| 412 | +# %% ../nbs/03_xtras.ipynb |
| 413 | +def trim_wraps(f, n=1): |
| 414 | + "Like wraps, but removes the first n parameters from the signature" |
| 415 | + import inspect |
| 416 | + def _(g): |
| 417 | + g = wraps(f)(g) |
| 418 | + sig = inspect.signature(f) |
| 419 | + params = list(sig.parameters.values())[n:] |
| 420 | + g.__signature__ = sig.replace(parameters=params) |
| 421 | + return g |
| 422 | + return _ |
| 423 | + |
| 424 | +# %% ../nbs/03_xtras.ipynb |
| 425 | +class _save_iter: |
| 426 | + def __init__(self, g, *args, **kw): self.g,self.args,self.kw = g,args,kw |
| 427 | + def __iter__(self): yield from self.g(self, *self.args, **self.kw) |
| 428 | + |
| 429 | +def save_iter(g): |
| 430 | + @trim_wraps(g) |
| 431 | + def _(*args, **kwargs): return _save_iter(g, *args, **kwargs) |
| 432 | + return _ |
| 433 | + |
412 | 434 | # %% ../nbs/03_xtras.ipynb |
413 | 435 | def exec_eval(code, # Code to exec/eval |
414 | 436 | g=None, # Globals namespace dict |
|
0 commit comments