0

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

  1. Input a valid node name from the user
  2. Output the children of this node and
  3. 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

  1. funcName1 / funcName2 / funcName3
  2. funcName1
  3. funcName2
  4. 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.

Ezio
  • 175
  • 6
  • Is it possible that function names (keys) will appear in more than one way? That is, can `funcname1` be a key, and also `funcname1 / funcname2` be a key at the same time? – aghast Feb 24 '17 at 05:36
  • Regular expressions aren't a magical fuzzy matching tool. If the dictionary had keys like `'???'`, it would crash the script. Just do `def validateInstruction(inputName): for key in insGraph: if inputName in key: print insGraph[key]`. – TigerhawkT3 Feb 24 '17 at 05:39

0 Answers0