tags:

views:

65

answers:

1

I have a data object that looks like this:

    {
        'node-16': {
            'tags': ['cuda'],
            'localNodes': [
                {
                    'name': 'nC',
                    'consumesFrom': ['nA', 'nB'],
                    'classType': 'VectorAdder.VectorAdder'
                },
                {
                    'name': 'nB',
                    'consumesFrom': None,
                    'classType': 'RandomVector'
                }
            ]
        },
        'node-17': {
            'tags': ['boring'],
            'localNodes': [
                {
                    'name': 'nA',
                    'consumesFrom': None,
                    'classType': 'RandomVector'
                }
            ]
        }
    }

Notice that node nA is a producer for nC. What's the fastest way to find out if a given localNode is a producer for another localnode in the data structure (and not within the same list)?

For example, I would like to know that nA (node-17) produces for nC (exists on node-16). But I don't need to know that nB produces for nC, since they exist in the same localNodes list.

+1  A: 
namedict = dict((x['name'], y) for y in data for x in data[y]['localNodes'])
proddict = dict((z['name'], [y for y in z['consumesFrom'] if namedict[y] != x])
  for x in data for z in data[x]['localNodes'] if z['consumesFrom'] is not None)

print 'nA' in proddict['nC']
Ignacio Vazquez-Abrams
I think this is probably not substantially different from what I was thinking of, but I think the nested for loops in dict-comprehension form get a bit dense to read. You could use some more lines, it's okay!
keturn