I have a dictionary of lists like this
insGraph =
{'funcA': ['funcZ', 'funcB', 'funcName1 / funcName2 / funcName3'],
'funcB': ['funcD', 'funcE'],
'funcName1 / funcName2 / funcName3': ['funcG', 'funcF]}
If the above is drawn as a graph,
funcA
|- funcZ
|- funcB
| |- funcD
| |- funcE
|- funcName1 / funcName2 / funcName3
|- funcG
|- funcF
As you can see an element can have a name in the format <name1> / <name2> / <name3>
My goal is to
- Input a valid node name from the user
- Output the children of this node and
- Output if this node is a child of any other node
The problem I'm facing here is if a node has multiple names and the user inputs any one of the valid names I should print the correct output.
eg: For the above graph, the following are some of the valid inputs
- funcName1 / funcName2 / funcName3
- funcName1
- funcName2
- funcName3
Case 1 is straight forward, I search for the key in the graph and print the output.
But if the user inputs any one of the values from case 2 to 4, I'm unable to get the key name from that input.
I tried the following,
def validateInstruction(inputName):
# Check if the instruction is present in the instruction graph
if not insGraph.has_key(inputName):
print 'Checking for any matching instruction...'
insStr = str(insGraph).split()
for key, children in insGraph.items():
# Search the children list
ins = re.findall(inputName, str(children))
if len(ins) != 0:
print ins
else:
# Search the pattern in key
ins = re.findall(inputName, str(key))
if len(ins) != 0:
print ins
This matches the input name with the key/children list. But the variable ins
does not contain the entire string in the format 'name1 / name2 / name3'. If funcName1
is the input I should extract the string funcName1 / funcName2 / funcName3
from the graph. What is the simplest way to do this ?
NOTE: This is a part of a bigger script and I cannot change the graph data structure.