tags:

views:

94

answers:

1

I have a list of tuples the looks similar to this (simplified here, there are over 14,000 of these tuples with more complicated paths than Obj.part)

[ (Obj1.part1, {<SPEC>}), (Obj1.partN, {<SPEC>}), (ObjK.partN, {<SPEC>}) ]

Where Obj goes from 1 - 1000, part from 0 - 2000. These "keys" all have a dictionary of specs associated with them which act as a lookup reference for inspecting another binary file. The specs dict contains information such as the bit offset, bit size, and C type of the data pointed to by the path ObjK.partN.

For example: Obj4.part500 might have this spec, {'size':32, 'offset':128, 'type':'int'} which would let me know that to access Obj4.part500 in the binary file I must unpack 32 bits from offset 128.

So, now I want to take my list of strings and create a nested dictionary which in the simplified case will look like this

data = { 'Obj1' : {'part1':{spec}, 'partN':{spec} }, 
         'ObjK' : {'part1':{spec}, 'partN':{spec} }
       }

To do this I am currently doing two things, 1. I am using a dotdict class to be able to use dot notation for dictionary get / set. That class looks like this:

class dotdict(dict):
    def __getattr__(self, attr):
        return self.get(attr, None)
    __setattr__ = dict.__setitem__
    __delattr__ = dict.__delitem__

The method for creating the nested "dotdict"s looks like this:

def addPath(self, spec, parts, base):
    if len(parts) > 1:
        item = base.setdefault(parts[0], dotdict())
        self.addPath(spec, parts[1:], item)
    else:
        item = base.setdefault(parts[0], spec)
    return base

Then I just do something like:

for path, spec in paths:
    self.lookup = dotdict()
    self.addPath(spec, path.split("."), self.lookup)

So, in the end
self.lookup.Obj4.part500 points to the spec.

Is there a better (more pythonic) way to do this?

+1  A: 

Unless you prefer to access the specs with dot notation, try putting them into the dictionary directly. In the below code, the name d tracks the innermost dictionary visited on the path:

specs = {}
for path, spec in paths:
    parts = path.split('.')
    d = specs
    for p in parts[:-1]:
        d = d.setdefault(p, {})
    d[parts[-1]] = spec

If you have only two parts per path (ObjN and partN say), you could just do this:

specs = {}
for path, spec in paths:
    [obj, part] = path.split('.')
    specs.setdefault(obj, {})[part] = spec
tcarobruce