1 def generate(mask, cond_mask, upper_bounds):
3 start_iterator_ch =
'i'
7 for i
in xrange(0, len(upper_bounds)):
9 cond_is_true = (cond_mask & (1 << cond_idx)) != 0
10 slot_lookup_result = 99
if cond_is_true
else -1
11 if_cond = indent_level *
' ' +
'if %d >= 0:' % slot_lookup_result
13 iterators.append(slot_lookup_result)
14 loops += if_cond +
'\n'
16 iterator_ch = chr(ord(start_iterator_ch) + i)
17 for_loop = (indent_level *
' ' +
'for %s in xrange(0, %d):' %
18 (iterator_ch, upper_bounds[i]))
19 iterators.append(iterator_ch)
20 loops += for_loop +
'\n'
22 loops += (indent_level *
' ' +
"print('" +
', '.
join([
'%s' for iterator
in iterators])
23 +
"' % (" +
', '.
join([str(iterator)
for iterator
in iterators]) +
'))')
26 if __name__ ==
'__main__':
27 upper_bounds = [5, 3, 9]
28 for mask
in xrange(0, 1 << len(upper_bounds)):
29 mask_bitcount = bin(mask).count(
"1")
30 for cond_mask
in xrange(0, 1 << mask_bitcount):
31 loops =
generate(mask, cond_mask, upper_bounds)