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