Commit 099091c6 authored by Denis Bilenko's avatar Denis Bilenko

cythonpp.py: copy implementations of itertools.combinations() and...

cythonpp.py: copy implementations of itertools.combinations() and itertools.product(). This fixes cythonpp.py on python 2.5.
parent 14110ee5
......@@ -5,7 +5,6 @@ import os
import re
import traceback
import datetime
import itertools
import pipes
import difflib
from hashlib import md5
......@@ -39,13 +38,6 @@ def match_condition(line):
newline_token = ' <cythonpp.py: REPLACE WITH NEWLINE!> '
def combinations(p, r):
# python 2.6.1 on snow leopard fails with a ValueError on this case
if len(p) < r:
return []
return itertools.combinations(p, r)
def process_filename(filename, output_filename=None):
"""Process the .ppyx file with preprocessor and compile it with cython.
......@@ -654,7 +646,7 @@ def flat_tuple(x):
def get_selections(items):
return set([flat_tuple(sorted(set(x))) for x in itertools.product(items, repeat=len(items))])
return set([flat_tuple(sorted(set(x))) for x in product(items, repeat=len(items))])
def is_impossible(configuration):
......@@ -722,6 +714,41 @@ def dbg(*args):
return log(*args)
# itertools is only available in python 2.6
# itertools.combinations has a problem on 2.6.1
def combinations(iterable, r):
# combinations('ABCD', 2) --> AB AC AD BC BD CD
# combinations(range(4), 3) --> 012 013 023 123
pool = tuple(iterable)
n = len(pool)
if r > n:
return
indices = range(r)
yield tuple(pool[i] for i in indices)
while True:
for i in reversed(range(r)):
if indices[i] != i + n - r:
break
else:
return
indices[i] += 1
for j in range(i+1, r):
indices[j] = indices[j-1] + 1
yield tuple(pool[i] for i in indices)
def product(*args, **kwds):
# product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
# product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
pools = map(tuple, args) * kwds.get('repeat', 1)
result = [[]]
for pool in pools:
result = [x+[y] for x in result for y in pool]
for prod in result:
yield tuple(prod)
if __name__ == '__main__':
import optparse
parser = optparse.OptionParser()
......
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