Commit f96fd2f3 authored by Hanno Schlichting's avatar Hanno Schlichting

Lazy PEP8

parent 7f532585
...@@ -16,37 +16,42 @@ class Lazy: ...@@ -16,37 +16,42 @@ class Lazy:
# Allow (reluctantly) access to unprotected attributes # Allow (reluctantly) access to unprotected attributes
__allow_access_to_unprotected_subobjects__=1 __allow_access_to_unprotected_subobjects__=1
def __repr__(self): return `list(self)` def __repr__(self):
return repr(list(self))
def __len__(self): def __len__(self):
# This is a worst-case len, subclasses should try to do better # This is a worst-case len, subclasses should try to do better
try: return self._len try:
except AttributeError: pass return self._len
except AttributeError:
pass
l=len(self._data) l = len(self._data)
while 1: while 1:
try: try:
self[l] self[l]
l=l+1 l = l + 1
except: except:
self._len=l self._len = l
return l return l
def __add__(self, other): def __add__(self, other):
if not isinstance(other, Lazy): if not isinstance(other, Lazy):
raise TypeError( raise TypeError(
"Can not concatenate objects. Both must be lazy sequences.") "Can not concatenate objects. Both must be lazy sequences.")
return LazyCat([self, other]) return LazyCat([self, other])
def __getslice__(self,i1,i2): def __getslice__(self, i1, i2):
r=[] r = []
for i in xrange(i1,i2): for i in xrange(i1, i2):
try: r.append(self[i]) try:
except IndexError: return r r.append(self[i])
except IndexError:
return r
return r return r
slice=__getslice__ slice = __getslice__
class LazyCat(Lazy): class LazyCat(Lazy):
# Lazy concatenation of one or more sequences. Should be handy # Lazy concatenation of one or more sequences. Should be handy
...@@ -66,46 +71,54 @@ class LazyCat(Lazy): ...@@ -66,46 +71,54 @@ class LazyCat(Lazy):
else: else:
flattened_seq.append(s) flattened_seq.append(s)
sequences = flattened_seq sequences = flattened_seq
self._seq=sequences self._seq = sequences
self._data=[] self._data = []
self._sindex=0 self._sindex = 0
self._eindex=-1 self._eindex = -1
if length is not None: self._len=length if length is not None:
self._len = length
def __getitem__(self,index):
def __getitem__(self, index):
data=self._data data = self._data
try: seq=self._seq try:
except AttributeError: return data[index] seq = self._seq
except AttributeError:
i=index return data[index]
if i < 0: i=len(self)+i
if i < 0: raise IndexError, index i = index
if i < 0:
ind=len(data) i = len(self) + i
if i < ind: return data[i] if i < 0:
ind=ind-1 raise IndexError(index)
sindex=self._sindex ind = len(data)
try: s=seq[sindex] if i < ind:
except: raise IndexError, index return data[i]
eindex=self._eindex ind = ind - 1
sindex = self._sindex
try:
s = seq[sindex]
except:
raise IndexError(index)
eindex = self._eindex
while i > ind: while i > ind:
try: try:
eindex=eindex+1 eindex = eindex + 1
v=s[eindex] v = s[eindex]
data.append(v) data.append(v)
ind=ind+1 ind = ind + 1
except IndexError: except IndexError:
self._sindex=sindex=sindex+1 self._sindex = sindex = sindex + 1
try: s=self._seq[sindex] try:
except: s = self._seq[sindex]
except Exception:
del self._seq del self._seq
del self._sindex del self._sindex
del self._eindex del self._eindex
raise IndexError, index raise IndexError(index)
self._eindex=eindex=-1 self._eindex = eindex = -1
self._eindex=eindex self._eindex = eindex
return data[i] return data[i]
def __len__(self): def __len__(self):
...@@ -113,7 +126,7 @@ class LazyCat(Lazy): ...@@ -113,7 +126,7 @@ class LazyCat(Lazy):
# of its underlying sequences # of its underlying sequences
try: try:
return self._len return self._len
except: except Exception:
try: try:
l = 0 l = 0
for s in self._seq: for s in self._seq:
...@@ -123,128 +136,147 @@ class LazyCat(Lazy): ...@@ -123,128 +136,147 @@ class LazyCat(Lazy):
self._len = l self._len = l
return l return l
class LazyMap(Lazy): class LazyMap(Lazy):
# Act like a sequence, but get data from a filtering process. # Act like a sequence, but get data from a filtering process.
# Don't access data until necessary # Don't access data until necessary
def __init__(self, func, seq, length=None): def __init__(self, func, seq, length=None):
self._seq=seq self._seq = seq
self._data=[] self._data = []
self._func=func self._func = func
if length is not None: self._len=length if length is not None:
else: self._len = len(seq) self._len = length
else:
def __getitem__(self,index): self._len = len(seq)
data=self._data def __getitem__(self, index):
try: s=self._seq data = self._data
except AttributeError: return data[index] try:
s = self._seq
except AttributeError:
return data[index]
i=index i = index
if i < 0: i=len(self)+i if i < 0:
if i < 0: raise IndexError, index i = len(self) + i
if i < 0:
raise IndexError(index)
ind=len(data) ind = len(data)
if i < ind: return data[i] if i < ind:
ind=ind-1 return data[i]
ind = ind - 1
func=self._func func = self._func
while i > ind: while i > ind:
try: try:
ind=ind+1 ind = ind + 1
data.append(func(s[ind])) data.append(func(s[ind]))
except IndexError: except IndexError:
del self._func del self._func
del self._seq del self._seq
raise IndexError, index raise IndexError(index)
return data[i] return data[i]
class LazyFilter(Lazy): class LazyFilter(Lazy):
# Act like a sequence, but get data from a filtering process. # Act like a sequence, but get data from a filtering process.
# Don't access data until necessary. Only data for which test(data) # Don't access data until necessary. Only data for which test(data)
# returns true will be considered part of the set. # returns true will be considered part of the set.
def __init__(self, test, seq): def __init__(self, test, seq):
self._seq=seq self._seq = seq
self._data=[] self._data = []
self._eindex=-1 self._eindex = -1
self._test=test self._test = test
def __getitem__(self,index):
data=self._data def __getitem__(self, index):
try: s=self._seq data = self._data
except AttributeError: return data[index] try:
s = self._seq
except AttributeError:
return data[index]
i=index i = index
if i < 0: i=len(self)+i if i < 0:
if i < 0: raise IndexError, index i = len(self) + i
if i < 0:
raise IndexError(index)
ind=len(data) ind = len(data)
if i < ind: return data[i] if i < ind:
ind=ind-1 return data[i]
ind = ind - 1
test=self._test test = self._test
e=self._eindex e = self._eindex
while i > ind: while i > ind:
try: try:
e=e+1 e = e + 1
v=s[e] v = s[e]
if test(v): if test(v):
data.append(v) data.append(v)
ind=ind+1 ind = ind + 1
except IndexError: except IndexError:
del self._test del self._test
del self._seq del self._seq
del self._eindex del self._eindex
raise IndexError, index raise IndexError(index)
self._eindex=e self._eindex = e
return data[i] return data[i]
class LazyMop(Lazy): class LazyMop(Lazy):
# Act like a sequence, but get data from a filtering process. # Act like a sequence, but get data from a filtering process.
# Don't access data until necessary. If the filter raises an exception # Don't access data until necessary. If the filter raises an exception
# for a given item, then that item isn't included in the sequence. # for a given item, then that item isn't included in the sequence.
def __init__(self, test, seq): def __init__(self, test, seq):
self._seq=seq self._seq = seq
self._data=[] self._data = []
self._eindex=-1 self._eindex = -1
self._test=test self._test = test
def __getitem__(self,index):
data=self._data def __getitem__(self, index):
try: s=self._seq data = self._data
except AttributeError: return data[index] try:
s = self._seq
except AttributeError:
return data[index]
i=index i = index
if i < 0: i=len(self)+i if i < 0:
if i < 0: raise IndexError, index i = len(self) + i
if i < 0:
raise IndexError(index)
ind=len(data) ind = len(data)
if i < ind: return data[i] if i < ind:
ind=ind-1 return data[i]
ind = ind - 1
test=self._test test = self._test
e=self._eindex e = self._eindex
while i > ind: while i > ind:
try: try:
e=e+1 e = e + 1
v=s[e] v = s[e]
try: try:
v=test(v) v = test(v)
data.append(v) data.append(v)
ind=ind+1 ind = ind + 1
except: pass except Exception:
pass
except IndexError: except IndexError:
del self._test del self._test
del self._seq del self._seq
del self._eindex del self._eindex
raise IndexError, index raise IndexError(index)
self._eindex=e self._eindex = e
return data[i] return data[i]
class LazyValues(Lazy): class LazyValues(Lazy):
"""Given a sequence of two tuples typically (key, value) act as """Given a sequence of two tuples typically (key, value) act as
though we are just a list of the values lazily""" though we are just a list of the values lazily"""
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment