i have set of nested lists can seperated in 3 groups:
a (subelements disjunction, line colors green), e.g.
lista = { ‘a1’: ['b1', 'a2'], ‘a2’: ['c1', 'c2'] }
b (subelements ordered conjunction, line colors orange), e.g.
listb = { ‘b1’: ['c4', 'c5', 'c7'], ‘b2’:['c3', 'b1'] }
- c (final elements - leaf nodes)
the function combinations
itereates through nested lists , returns possible combinations (which @ end contains of elements of type c, leaf nodes). function write_nodes
helps write nodes colored lines. call write_nodes('task', inputlist)
creating init node:
def write_nodes(node, subnotes): k in subnotes: if node in type_a: text_file.write("{} -> {} [color=\"green\"]\n".format(node, k)) elif (node in type_b) or (node 'task'): text_file.write("{} -> {} [color=\"orange\"]\n".format(node, k)) write_nodes('task', inputlist) def combinations(actions): if len(actions)==1: action= actions[0] if action not in type_c: root = action try: actions= type_a[action] write_nodes(root, actions) except keyerror: try: actions= type_b[action] write_nodes(root, actions) except keyerror: #action of type c, possible combination yield actions else: #action of type b (conjunction), combine actions combination in combinations(actions): yield combination else: #action of type (disjunction), generate combinations each action action in actions: combination in combinations([action]): yield combination else: #generate combinations first action in list #and combine them combinations rest of list action= actions[0] combination in combinations(actions[1:]): combo in combinations([action]): yield combo + combination
example input (ordered conjunction):
['a1', 'b2', 'c6']
example result:
['c4', 'c5', 'c7', 'c3', 'c4', 'c5', 'c7', 'c6'] ['c1', 'c3', 'c4', 'c5', 'c7', 'c6'] ['c2', 'c3', 'c4', 'c5', 'c7', 'c6']
the result got code: corresponding dot file:
task -> a1 [color="orange"] task -> b2 [color="orange"] task -> c6 [color="orange"] b2 -> c3 [color="orange"] b2 -> b1 [color="orange"] b1 -> c4 [color="orange"] b1 -> c5 [color="orange"] b1 -> c7 [color="orange"] a1 -> b1 [color="green"] a1 -> a2 [color="green"] b1 -> c4 [color="orange"] b1 -> c5 [color="orange"] b1 -> c7 [color="orange"] a2 -> c1 [color="green"] a2 -> c2 [color="green"]
the result want (colors not prior one):
questions:
how can handle fact, there there duplicated nodes result mentioned?
thanks help.
duplicate nodes problem
to avoid duplicate nodes problem should name each node unique name , use label
displayed name. example change:
b2 -> b1 [color="orange"] b1 -> c4 [color="orange"] b1 -> c5 [color="orange"] b1 -> c7 [color="orange"] a1 -> b1 [color="green"] b1 -> c4 [color="orange"] b1 -> c5 [color="orange"] b1 -> c7 [color="orange"]
to:
b21 [label="b2"] b11 [label="b1"] b21 -> b11 [color="orange"] c41 [label="c4"] b11 -> c41 [color="orange"] c51 [label="c5"] b11 -> c51 [color="orange"] c71 [label="c7"] b11 -> c71 [color="orange"] a11 [label="a2"] b12 [label="b1"] a11 -> b12 [color="green"] c42 [label="c4"] b12 -> c42 [color="orange"] c52 [label="c5"] b12 -> c52 [color="orange"] c72 [label="c7"] b12 -> c72 [color="orange"]
avoid try/except
flow
it's better use if/else
, not try/except
normal program flow. example instead of:
try: actions= type_a[action] write_nodes(root, actions) except keyerror: #do whatever
use:
actions = type_a.get(action, none) #if key doesn't exists assign none if actions: write_nodes(root, actions) else: #do whatever
graphviz python package
you can use graphviz
python package instead of writing dot file yourself.
Comments
Post a Comment