Quite often we have a lot of keys, that we get from a remote data source, and don't want to have to ask for them again and again, having them be fetched, sent over the network, etc.
So we need caching.
But this caching is not the typical read caching, since it's __iter__ we want to cache, and that's a generator.
So we'll implement a store class decorator specialized for this.
The basic idea is this:
class MyStore(dict):
_iter_cache = None
def __iter__(self):
if self._iter_cache is None:
print('First time: Writing to cache')
self._iter_cache = list(super().__iter__())
yield from self._iter_cache
>>> class MyStore(dict):
... _iter_cache = None
... def __iter__(self):
... if self._iter_cache is None:
... print('First time: Writing to cache')
... self._iter_cache = list(super().__iter__())
... yield from self._iter_cache
...
>>>
>>> s = MyStore({'a': 'pi', 'b': 3.1415})
>>> t = list(s)
First time: Writing
>>> tt = list(s) # but this won't print anything
Quite often we have a lot of keys, that we get from a remote data source, and don't want to have to ask for them again and again, having them be fetched, sent over the network, etc.
So we need caching.
But this caching is not the typical read caching, since it's
__iter__
we want to cache, and that's a generator.So we'll implement a store class decorator specialized for this.
The basic idea is this: