Here it is yet another example on how to inherit from the Table object so as to build an easy-to-use Table object. Thanks to Brent Pedersen for this one (taken from http://pypi.python.org/pypi/simpletable/).

   1 """
   2 
   3 SimpleTable: simple wrapper around pytables hdf5
   4 ------------------------------------------------------------------------------
   5 
   6 Example Usage::
   7 
   8   >>> from simpletable import SimpleTable
   9   >>> import tables
  10 
  11   # define the table as a subclass of simple table.
  12   >>> class ATable(SimpleTable):
  13   ...     x = tables.Float32Col()
  14   ...     y = tables.Float32Col()
  15   ...     name = tables.StringCol(16)
  16 
  17   # instantiate with: args: filename, tablename
  18   >>> tbl = ATable('test_docs.h5', 'atable1')
  19 
  20   # insert as with pytables:
  21   >>> row = tbl.row
  22   >>> for i in range(50):
  23   ...    row['x'], row['y'] = i, i * 10
  24   ...    row['name'] = "name_%i" % i
  25   ...    row.append()
  26   >>> tbl.flush()
  27 
  28   # there is also insert_many() method() with takes an iterable
  29   # of dicts with keys matching the colunns (x, y, name) in this
  30   # case.
  31 
  32   # query the data (query() alias of tables' readWhere()
  33   >>> tbl.query('(x > 4) & (y < 70)') #doctest: +NORMALIZE_WHITESPACE
  34   array([('name_5', 5.0, 50.0), ('name_6', 6.0, 60.0)],
  35         dtype=[('name', '|S16'), ('x', '<f4'), ('y', '<f4')])
  36 
  37 """
  38 
  39 import tables
  40 _filter = tables.Filters(complib="lzo", complevel=1, shuffle=True)
  41 
  42 class SimpleTable(tables.Table):
  43     def __init__(self, file_name, table_name, description=None,
  44                  group_name='default', mode='a', title="", filters=_filter,
  45                  expectedrows=512000):
  46 
  47         f = tables.openFile(file_name, mode)
  48         self.uservars = None
  49 
  50         if group_name is None: group_name = 'default'
  51         parentNode = f._getOrCreatePath('/' + group_name, True)
  52 
  53         if table_name in parentNode: # existing table
  54             description = None
  55         elif description is None: # pull the description from the attrs
  56             description = dict(self._get_description())
  57 
  58         tables.Table.__init__(self, parentNode, table_name,
  59                        description=description, title=title,
  60                        filters=filters,
  61                        expectedrows=expectedrows,
  62                        _log=False)
  63         self._c_classId = self.__class__.__name__
  64 
  65     def _get_description(self):
  66         # pull the description from the attrs
  67         for attr_name in dir(self):
  68             if attr_name[0] == '_': continue
  69             try:
  70                 attr = getattr(self, attr_name)
  71             except:
  72                 continue
  73             if isinstance(attr, tables.Atom):
  74                 yield attr_name, attr
  75 
  76     def insert_many(self, data_generator, attr=False):
  77         row = self.row
  78         cols = self.colnames
  79         if not attr:
  80             for d in data_generator:
  81                 for c in cols:
  82                     row[c] = d[c]
  83                 row.append()
  84         else:
  85             for d in data_generator:
  86                 for c in cols:
  87                     row[c] = getattr(d, c)
  88                 row.append()
  89         self.flush()
  90 
  91     query = tables.Table.readWhere
  92 
  93 # convience sublcass that i use a lot.
  94 class BlastTable(SimpleTable):
  95       query      = tables.StringCol(5)
  96       subject    = tables.StringCol(5)
  97 
  98       pctid      = tables.Float32Col()
  99       hitlen     = tables.UInt16Col()
 100       nmismatch  = tables.UInt16Col()
 101       ngaps      = tables.UInt16Col()
 102 
 103       qstart     = tables.UInt32Col()
 104       qstop      = tables.UInt32Col()
 105       sstart     = tables.UInt32Col()
 106       sstop      = tables.UInt32Col()
 107 
 108       evalue     = tables.Float64Col()
 109       score      = tables.Float32Col()
 110 
 111 
 112 if __name__ == '__main__':
 113     import doctest
 114     doctest.testmod()
 115     import os
 116     os.unlink('test_docs.h5')

UserDocuments/SimpleTable (last edited 2010-04-20 16:44:41 by FrancescAlted)