From: Norbert N. <Nor...@gm...> - 2005-01-16 13:13:02
|
Hi there, just considered using Tables instead of Arrays for my purpose. Now I really had to fight the fact that column descriptors have to be given as classes. Problem is: defining a class the regular way means, that at the time of writing a python script, you have to know the names and types of all columns. If you get the names of the columns as strings from some other source, this becomes considerably harder. The hack I've come up with for the moment looks like: ----------------------------------------------------------- #!/usr/bin/env python from tables import * def descfromdict(__d__): class res(IsDescription): for __k__ in __d__: locals()[__k__] = __d__[__k__] return res file = openFile('tryout.h5','w') mydict={} mydict['somecolumn'] = FloatCol() mydict['transmission'] = FloatCol() mydict['statistics'] = UInt16Col() file.root.mytable = Table(descfromdict(mydict)) file.close() --------------------------------------------- similarly, it would be possible to us an object instead of a class, which would even allow natural naming: ------------------------------------------------------ #!/usr/bin/env python from tables import * def descfromobj(__d__): class res(IsDescription): for __k__ in __d__.__dict__: locals()[__k__] = getattr(__d__,__k__) return res class MyDescription: def __setitem__(self,key,value): assert type(key) is str assert isinstance(value,Col) setattr(self,key,value) file = openFile('tryout.h5','w') myobj = MyDescription() myobj.somecolumn = FloatCol() myobj['anothercolumn'] = FloatCol() file.root.mytable = Table(descfromobj(myobj)) file.close() -------------------------------------------- Both work, but it is really ugly if you have to find such a solution yourself. In the least, I would suggest to offer these options as an alternative way to pass descriptions. But I would think, that it would simplify things a lot, if one of these methods (or something even better) would become the preferred and documented way of passing a descriptor and the old method of defining and passing a class be obsoleted. I think, even the internals of pytables might be simplified by it... Ciao, Norbert -- _________________________________________Norbert Nemec Bernhardstr. 2 ... D-93053 Regensburg Tel: 0941 - 2009638 ... Mobil: 0179 - 7475199 eMail: <No...@Ne...> |