1
2
3
4
5
6
7
10 """
11 computeFunc should take a single argument, the integer bit id
12 to compute
13
14 """
15 if sigSize<=0:
16 raise ValueError('zero size')
17 self.computeFunc=computeFunc
18 self.size=sigSize
19 self._cache={}
20
22 """
23
24 >>> obj = LazySig(lambda x:1,10)
25 >>> len(obj)
26 10
27
28 """
29 return self.size
30
32 """
33
34 >>> obj = LazySig(lambda x:x,10)
35 >>> obj[1]
36 1
37 >>> obj[-1]
38 9
39 >>> try:
40 ... obj[10]
41 ... except IndexError:
42 ... 1
43 ... else:
44 ... 0
45 1
46 >>> try:
47 ... obj[-10]
48 ... except IndexError:
49 ... 1
50 ... else:
51 ... 0
52 1
53
54 """
55 if which<0:
56
57 which = self.size+which
58
59 if which<=0 or which>=self.size:
60 raise IndexError('bad index')
61
62 if self._cache.has_key(which):
63 v= self._cache[which]
64 else:
65 v = self.computeFunc(which)
66 self._cache[which]=v
67 return v
68
69
70
71
72
74 import doctest,sys
75 return doctest.testmod(sys.modules["__main__"])
76
77
78 if __name__ == '__main__':
79 import sys
80 failed,tried = _test()
81 sys.exit(failed)
82