From: Francesc A. <fa...@op...> - 2003-07-11 08:13:04
|
A Dijous 10 Juliol 2003 19:45, vareu escriure: > I'm working on converting python data structures to pytables commands. > > So if you have: > > data={'fred':[1,2,3,4,5,6,7,8,9,10], > 'barney':[11,12,13,14,15], > 'wilma':[99,99,3,22,66] > } > > > Just do something like: > save(data,'file1.hdf') > > and it will automatically save the dictionary in pytables format for > you. > I think it will make pytables more accessible. That sounds nice > > > Anyways, if I define a class like: > > class Row(tables.IsDescription): pass > > I cannot use setattr to do this: > > setattr(Row, 'data0', tables.Col('Int32')) > > The definition of data0 has to be present at class defintion time. I > think this is because of the metaclass that helps create > tables.IsDescription. (the metaclass stuff they've added to python is > cool!) > > I've tried some tactics like defining my own metaclass for Row, but > ended up > resorting to using compile/exec on strings to build the class. It seems > awkward, can you think of a better way to at runtime define the data > that forms a table? I don't know if you are aware that pytables can also build a Table from a dictionary like: RecordDescriptionDict = { 'var1': Col("CharType", 4), # 4-character String 'var2': Col("Int32", 1), # integer 'var3': Col("Int16", 1), # short integer 'var4': Col("Float64", 2), # double (double-precision) 'var5': Col("Float32", 4), # float (single-precision) 'var6': Col("Int16", 1), # short integer 'var7': Col("CharType", 1), # 1-character String } and then call the Table constructor as always: h5file.createTable(group, 'table', RecordDescritionDict, title = self.title) So, you can pass both a metaclass descentant or a dictionary to Table and both will be understood (for more information see http://pytables.sourceforge.net/html-doc/usersguide-html4.html#subsection4.4.2 and http://pytables.sourceforge.net/html-doc/usersguide-html3.html#secondExample) I think that this should be the way to go for you. However, it is indeed possible to build-up a metaclass dynamically and you can see examples of that use in the Table._newRecArray() and Table._open() methods in the sources. But this is a more lower level approach and not recommended unless you absolutely need it. Good luck!, -- Francesc Alted |