Problem with this unittest:
>>> d1 = {1:[1,1,1,'one'], 2:[2,4,6,8], 4:4, 8:['eight']} #PKHG the firs 1 was 'ones' ==>error not allowed in 3.2
>>> for x in dictCombinations(d1): print(sorted(x.items())) #PKHG this tests dictCombinations
[(2, 2), (4, 4), (8, 'eight'), ('ones', 1)]
[(2, 4), (4, 4), (8, 'eight'), ('ones', 1)]
[(2, 6), (4, 4), (8, 'eight'), ('ones', 1)]
[(2, 8), (4, 4), (8, 'eight'), ('ones', 1)]
[(2, 2), (4, 4), (8, 'eight'), ('ones', 'one')]
[(2, 4), (4, 4), (8, 'eight'), ('ones', 'one')]
[(2, 6), (4, 4), (8, 'eight'), ('ones', 'one')]
[(2, 8), (4, 4), (8, 'eight'), ('ones', 'one')]
This works
thus ‘ones’ is not allowed => replaced by 1
And a problem, the keys ‘must be’ strings! in utilities.py of pybrain
at least at this moment
Change recursive code is this:
def dictCombinations(listdict):
""" Iterates over dictionaries that go through every possible combination
of key-value pairs as specified in the lists of values for each key in listdict."""
listdict = listdict.copy()
if len(listdict) == 0:
return [{}]
k, vs = listdict.popitem()
print("PKHG L681 in utilities ",k,vs)
res = dictCombinations(listdict)
print("L683 res k vs new ",res,k,vs)
if isinstance(vs, list) or isinstance(vs, tuple):
res = [dict(d, **{str(k):v}) for d in res for v in sorted(set(vs))] #PKHG str added!
else:
res = [dict(d, **{str(k):vs}) for d in res] #PKHG str added!
return res
original: **{k:v} gives error:
File “C:\Python32\lib\site-packages\pybrain-0.3-py3.2.egg\pybrain\utilities.py”, line 686, in <listcomp>
res = [dict(d, **{k:v}) for d in res for v in sorted(set(vs))]
TypeError: keyword arguments must be strings
d1 = {1:[1,2], 2:[2,4,6,8], 4:4, 8:[‘eight’,‘4’]}
for x in res: print(sorted(x.items()))
giving:
[(‘1’, 1), (‘2’, 2), (‘4’, 4), (‘8’, ‘4’)]
[(‘1’, 1), (‘2’, 2), (‘4’, 4), (‘8’, ‘eight’)]
[(‘1’, 2), (‘2’, 2), (‘4’, 4), (‘8’, ‘4’)]
[(‘1’, 2), (‘2’, 2), (‘4’, 4), (‘8’, ‘eight’)]
[(‘1’, 1), (‘2’, 4), (‘4’, 4), (‘8’, ‘4’)]
[(‘1’, 1), (‘2’, 4), (‘4’, 4), (‘8’, ‘eight’)]
[(‘1’, 2), (‘2’, 4), (‘4’, 4), (‘8’, ‘4’)]
[(‘1’, 2), (‘2’, 4), (‘4’, 4), (‘8’, ‘eight’)]
[(‘1’, 1), (‘2’, 6), (‘4’, 4), (‘8’, ‘4’)]
[(‘1’, 1), (‘2’, 6), (‘4’, 4), (‘8’, ‘eight’)]
[(‘1’, 2), (‘2’, 6), (‘4’, 4), (‘8’, ‘4’)]
[(‘1’, 2), (‘2’, 6), (‘4’, 4), (‘8’, ‘eight’)]
[(‘1’, 1), (‘2’, 8), (‘4’, 4), (‘8’, ‘4’)]
[(‘1’, 1), (‘2’, 8), (‘4’, 4), (‘8’, ‘eight’)]
[(‘1’, 2), (‘2’, 8), (‘4’, 4), (‘8’, ‘4’)]
[(‘1’, 2), (‘2’, 8), (‘4’, 4), (‘8’, ‘eight’)]
SO Alain, do you ever need this???
just a minute ago 
