#29 Broken iteration protocol on tables?

closed
None
5
2005-06-20
2005-05-20
No

The iteration code of tables seems not to be compliant
with the standard Python iterator protocol. For
instance, the following code::

import tables as t

class myDescr(t.IsDescription):
c1 = t.IntCol(pos=0)
c2 = t.FloatCol(pos=1)

h5f = t.openFile('t.h5', 'w')
tbl = h5f.createTable('/', 'test', myDescr)

data = ((1, 1.0), (2, 2.0), (3, 3.0))
tbl.append(data)
tbl.flush()

print "Data iteration:", tuple(iter(data))
print "Table iteration:", tuple(tbl)
print "Table for:",
for r in tbl:
print r,
print

h5f.close()

shows this output::

Data iteration: ((1, 1.0), (2, 2.0), (3, 3.0))
Table iteration: ((3, 3.0), (3, 3.0), (3, 3.0))
Table for: (1, 1.0) (2, 2.0) (3, 3.0)

which proves that, although the stored data is OK, the
data retrieved by ``tuple()`` is not. Please note that
this should work as is shown with ``tuple(iter(data))``.

Discussion

  • Francesc Altet

    Francesc Altet - 2005-06-20
    • assigned_to: nobody --> falted
    • status: open --> closed
     
  • Francesc Altet

    Francesc Altet - 2005-06-20

    Logged In: YES
    user_id=249033

    This is due to the fact that the Table iterator returns
    a pointer to the Row instance, which is a data
    *accessor*, instead of a pointer to the actual data.

    A possible workaround for the previous example would
    be:

    tuple([(row['c1'], row['c2']) for row in tbl])

    However, a general solution is not easy.

     

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks