Just-In-Time List

I would like to know if there is a class available either in the standard library or in pypi, which matches this description.

The constructor will take an iterator.

It will implement the container protocol (i.e. __getitem__, __len__, etc.), so that slices, length, etc. will be executed. At the same time, it will iterate over and save only enough values ​​from the constructor argument to provide any information requested.

So, if jitlist [6] is requested, it will call self.source.next () 7 times, save these elements in its list and return the last one.

This will allow you to use the code down to use it as a list, but to avoid unnecessarily creating an instance of the list for cases where the list function is not needed, and to avoid allocating memory for the entire list if only a few members are requested.

It seems pretty easy to write, but also seems useful enough that someone would probably already make this available in the module.

+3
source share
1 answer
import itertools
class Indexable(object):
    def __init__(self,it):
        self.it=it
        self.already_computed=[]
    def __iter__(self):
        for elt in self.it:
            self.already_computed.append(elt)
            yield elt
    def __getitem__(self,index):
        try:
            max_idx=index.stop
        except AttributeError:
            max_idx=index
        n=max_idx-len(self.already_computed)+1
        if n>0:
            self.already_computed.extend(itertools.islice(self.it,n))
        return self.already_computed[index]      
+2
source

Source: https://habr.com/ru/post/1747014/


All Articles