Anonymous avatar Anonymous committed 7e415ac

add lazy list

Comments (0)

Files changed (1)

+__all__ = ["llist"]
+
+class llist(list):
+    """
+    >>> ll = llist(range(100))
+    >>> isinstance(ll, list)
+    True
+    >>> ll[10]
+    10
+    >>> print ll
+    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, ...]
+    >>> ll[100]
+    Traceback (most recent call last):
+    IndexError: list index out of range
+    >>> ll = llist(range(100))
+    >>> ll
+    [...]
+    >>> len(ll)
+    100
+    >>> ll = llist(range(10))
+    >>> ll.extend(range(10,20))
+    >>> len(ll)
+    20
+    >>> ll
+    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
+    >>> ll = llist(range(10))
+    >>> l = list(range(10))
+    >>> ll.pop(5)
+    5
+    >>> l.pop(5)
+    5
+    >>> ll == l
+    True
+    >>> ll
+    [0, 1, 2, 3, 4, 6, 7, 8, 9]
+    >>> ll.pop()
+    9
+    >>> l.pop()
+    9
+    >>> ll
+    [0, 1, 2, 3, 4, 6, 7, 8]
+    >>> 4 in ll
+    True
+    >>> 5 in ll
+    False
+    >>> ll = llist(range(10))
+    >>> ll.append('A')
+    >>> ll
+    [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A']
+    >>> ll = llist(range(10))
+    >>> ll[4:8]
+    [4, 5, 6, 7]
+    
+    """
+    def __init__(self, iterable):
+        if not hasattr(iterable, "next"):
+            iterable = iter(iterable)
+        self.__iterable__ = iterable
+        self.__exhausted__ = False
+        
+    def __readuntil__(self, index):
+        if self.__exhausted__: return
+        end = super(llist, self).__len__() -1
+        while index < 0 or index < end:
+            try:
+                value = self.__iterable__.next()
+                super(llist, self).append(value)
+                end += 1
+            except StopIteration:
+                self.__exhausted__ = True
+                break
+            
+    def __getitem__(self, index):
+        self.__readuntil__(index)
+        return super(llist, self).__getitem__(index)
+    
+    def __setitem__(self, index, value):
+        self.__readuntil__(index)
+        super(llist, self).__setitem__(index, value)
+
+    def __delitem__(self, index):
+        self.__readuntil__(index)
+        super(llist, self).__delitem__(index)
+
+    def __getslice__(self, start, end):
+        self.__readuntil__(end)
+        return super(llist, self).__getslice__(start, end)
+    
+    def __len__(self):
+        self.__readuntil__(-1)
+        return super(llist, self).__len__()
+
+    def __contains__(self, value):
+        self.__readuntil__(-1)
+        return super(llist, self).__contains__(value)
+
+    def __eq__(self, other):
+        self.__readuntil__(-1)
+        return super(llist, self).__eq__(other)
+
+    def __repr__(self):
+        s = super(llist, self).__repr__()
+        if not self.__exhausted__:
+            if s[-2] == '[':
+                return "[...]"
+            return s[:-1] + ", ...]"
+        else:
+            return s
+
+    def __str__(self):
+        s = super(llist, self).__repr__()
+        if not self.__exhausted__:
+            if s[-2] == '[':
+                return "[...]"
+            return s[:-1] + ", ...]"
+        else:
+            return s
+        
+    def append(self, value):
+        self.__readuntil__(-1)
+        super(llist, self).append(value)
+
+    def remove(self, value):
+        self.__readuntil__(-1)
+        super(llist, self).remove(value)
+        
+    def pop(self, index=-1):
+        self.__readuntil__(index)
+        return super(llist, self).pop(index)
+
+    def reverse(self):
+        self.__readuntil__(-1)
+        super(llist, self).reverse()
+
+    def sort(self, *av):
+        self.__readuntil__(-1)
+        super(llist, self).sort(*av)
+
+    def extend(self, iterable):
+        self.__readuntil__(-1)
+        super(llist, self).extend(iterable)
+        
+if __name__ == '__main__':
+    import doctest
+    doctest.testmod()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.