Skip to content

Commit 2db6ff0

Browse files
committed
fixes #684
1 parent 0fad28b commit 2db6ff0

File tree

3 files changed

+180
-27
lines changed

3 files changed

+180
-27
lines changed

fastcore/_modidx.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,7 +534,13 @@
534534
'fastcore.xml.showtags': ('xml.html#showtags', 'fastcore/xml.py'),
535535
'fastcore.xml.to_xml': ('xml.html#to_xml', 'fastcore/xml.py'),
536536
'fastcore.xml.valmap': ('xml.html#valmap', 'fastcore/xml.py')},
537-
'fastcore.xtras': { 'fastcore.xtras.ContextManagers': ('xtras.html#contextmanagers', 'fastcore/xtras.py'),
537+
'fastcore.xtras': { 'fastcore.xtras.CachedAwaitable': ('xtras.html#cachedawaitable', 'fastcore/xtras.py'),
538+
'fastcore.xtras.CachedAwaitable.__await__': ('xtras.html#cachedawaitable.__await__', 'fastcore/xtras.py'),
539+
'fastcore.xtras.CachedAwaitable.__init__': ('xtras.html#cachedawaitable.__init__', 'fastcore/xtras.py'),
540+
'fastcore.xtras.CachedIter': ('xtras.html#cachediter', 'fastcore/xtras.py'),
541+
'fastcore.xtras.CachedIter.__init__': ('xtras.html#cachediter.__init__', 'fastcore/xtras.py'),
542+
'fastcore.xtras.CachedIter.__iter__': ('xtras.html#cachediter.__iter__', 'fastcore/xtras.py'),
543+
'fastcore.xtras.ContextManagers': ('xtras.html#contextmanagers', 'fastcore/xtras.py'),
538544
'fastcore.xtras.ContextManagers.__enter__': ('xtras.html#contextmanagers.__enter__', 'fastcore/xtras.py'),
539545
'fastcore.xtras.ContextManagers.__exit__': ('xtras.html#contextmanagers.__exit__', 'fastcore/xtras.py'),
540546
'fastcore.xtras.ContextManagers.__init__': ('xtras.html#contextmanagers.__init__', 'fastcore/xtras.py'),
@@ -621,6 +627,7 @@
621627
'fastcore.xtras.open_file': ('xtras.html#open_file', 'fastcore/xtras.py'),
622628
'fastcore.xtras.parse_env': ('xtras.html#parse_env', 'fastcore/xtras.py'),
623629
'fastcore.xtras.partial_format': ('xtras.html#partial_format', 'fastcore/xtras.py'),
630+
'fastcore.xtras.reawaitable': ('xtras.html#reawaitable', 'fastcore/xtras.py'),
624631
'fastcore.xtras.repo_details': ('xtras.html#repo_details', 'fastcore/xtras.py'),
625632
'fastcore.xtras.repr_dict': ('xtras.html#repr_dict', 'fastcore/xtras.py'),
626633
'fastcore.xtras.round_multiple': ('xtras.html#round_multiple', 'fastcore/xtras.py'),

fastcore/xtras.py

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
'round_multiple', 'set_num_threads', 'join_path_file', 'autostart', 'EventTimer', 'stringfmt_names',
1414
'PartialFormatter', 'partial_format', 'utc2local', 'local2utc', 'trace', 'modified_env', 'ContextManagers',
1515
'shufflish', 'console_help', 'hl_md', 'type2str', 'dataclass_src', 'Unset', 'nullable_dc', 'make_nullable',
16-
'flexiclass', 'asdict', 'is_typeddict', 'is_namedtuple', 'flexicache', 'time_policy', 'mtime_policy',
17-
'timed_cache']
16+
'flexiclass', 'asdict', 'is_typeddict', 'is_namedtuple', 'CachedIter', 'CachedAwaitable', 'reawaitable',
17+
'flexicache', 'time_policy', 'mtime_policy', 'timed_cache']
1818

1919
# %% ../nbs/03_xtras.ipynb
2020
from .imports import *
@@ -744,6 +744,29 @@ def is_namedtuple(cls):
744744
"`True` if `cls` is a namedtuple type"
745745
return issubclass(cls, tuple) and hasattr(cls, '_fields')
746746

747+
# %% ../nbs/03_xtras.ipynb
748+
class CachedIter:
749+
"Cache the result returned by an iterator"
750+
def __init__(self, o): self.o,self.value = o,UNSET
751+
def __iter__(self):
752+
if self.value is UNSET: self.value = yield from self.o
753+
return self.value
754+
755+
# %% ../nbs/03_xtras.ipynb
756+
class CachedAwaitable:
757+
"Cache the result from an awaitable"
758+
def __init__(self, o): self.o,self.value = o,UNSET
759+
def __await__(self):
760+
if self.value is UNSET: self.value = yield from self.o.__await__()
761+
return self.value
762+
763+
# %% ../nbs/03_xtras.ipynb
764+
def reawaitable(func:callable):
765+
"Wraps the result of an asynchronous function into an object which can be awaited more than once"
766+
@wraps(func)
767+
def _f(*args, **kwargs): return CachedAwaitable(func(*args, **kwargs))
768+
return _f
769+
747770
# %% ../nbs/03_xtras.ipynb
748771
def flexicache(*funcs, maxsize=128):
749772
"Like `lru_cache`, but customisable with policy `funcs`"

0 commit comments

Comments
 (0)