Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

nesting with namedtuple

I'm having trouble getting my data in the form that I'd like in python.

Basically I have a program that reads in binary data and provides functions for plotting and analysis on said data.

My data has main headings and then subheadings that could be any number of varied datatypes.

I'd like to be able to access my data like for example:

>>> a = myDatafile.readit()
>>> a.elements.hydrogen.distributionfunction
(a big array)
>>> a.elements.hydrogen.mass
1
>>> a.elements.carbon.mass
12

but I don't know the names of the atoms until runtime.

I've tried using namedtuple, for example after I've read in all the atom names:

self.elements = namedtuple('elements',elementlist)

Where elementlist is a list of strings for example ('hydrogen','carbon'). But the problem is I can't nest these using for example:

for i in range(0,self.nelements):
    self.elements[i] = namedtuple('details',['ux','uy','uz','mass','distributionfunction'])

and then be able to access the values through for example

self.elements.electron.distributionfunction.

Maybe I'm doing this completely wrong. I'm fairly inexperienced with python. I know this would be easy to do if I wasn't bothered about naming the variables dynamically.

I hope I've made myself clear with what I'm trying to achieve!

like image 230
Daniel Fletcher Avatar asked Dec 05 '11 16:12

Daniel Fletcher


1 Answers

Without knowing your data, we can only give a generic solution.

Considering the first two lines contains the headings and Sub-Heading reading it somehow you determined the hierarchy. All you have to do is to create an hierarchical dictionary.

For example, extending your example

data.elements.hydrogen.distributionfunction
data.elements.nitrogen.xyzfunction
data.elements.nitrogen.distributionfunction
data.compound.water.distributionfunction
data.compound.hcl.xyzfunction

So we have to create a dictionary as such

{'data':{'elements':{'hydrogen':{'distributionfunction':<something>}
                     'nitrogen':{'xyzfunction':<something>,
                           'distributionfunction':<something>}
                }
       compound:{'water':{'distributionfunction':<something>}
                 'hcl':{'xyzfunction':<something>}
                }
       }
 }

how you will populate the dictionary depends on the data which is difficult to say now. But the keys to the dictionary you should populate from the headers, and somehow you have to map the data to the respective value in the empty slot's of the dictionary.

Once the map is populated, you can access it as

 yourDict['data']['compound']['hcl']['xyzfunction']
like image 128
Abhijit Avatar answered Dec 23 '22 14:12

Abhijit