diff options
Diffstat (limited to 'gen/dp.py')
-rw-r--r-- | gen/dp.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/gen/dp.py b/gen/dp.py new file mode 100644 index 0000000..0ac8cce --- /dev/null +++ b/gen/dp.py | |||
@@ -0,0 +1,33 @@ | |||
1 | from __future__ import division | ||
2 | |||
3 | class DesignPointGenerator(object): | ||
4 | '''Iterates over all combinations of values specified in options. | ||
5 | Shamelessly stolen (and simplified) from bcw.''' | ||
6 | def __init__(self, options): | ||
7 | self.point_idx = 0 # Current point | ||
8 | self.options = options | ||
9 | self.total = 1 | ||
10 | for x in options.itervalues(): | ||
11 | self.total *= len(x) | ||
12 | |||
13 | def __iter__(self): | ||
14 | return self | ||
15 | |||
16 | def next(self): | ||
17 | while True: | ||
18 | if self.point_idx == self.total: | ||
19 | raise StopIteration | ||
20 | else: | ||
21 | point = {} | ||
22 | |||
23 | divisor = 1 | ||
24 | for key in sorted(self.options.keys()): | ||
25 | size = len(self.options[key]) | ||
26 | |||
27 | option_idx = int(self.point_idx / divisor) % size | ||
28 | point[key] = self.options[key][option_idx] | ||
29 | |||
30 | divisor *= size | ||
31 | self.point_idx += 1 | ||
32 | |||
33 | return point | ||