El dt 25 de 08 del 2009 a les 11:16 +0200, en/na Gabriel J.L. Beckers va
escriure:
> Hi List,
>
> I ran into unexpected behavior when iterating over rows in a table
> (PyTables version '2.2a2').
>
> The attached script shows the problem (minimal example), but, briefly:
> if one iterates over rows in a table, and checks in the loop if the
> row has a certain key, the iterator will jump to the last row and skip
> all other rows.
>
> I admit checking for a key is a stupid thing to do (one should look at
> the table colnames before the loop), and I don't even know if the row
> type is intended to support this. But this aside, is the iterator
> behavior shown here a bug?
Well, the Row instances are not designed to be used like this, but, as
long it is not explicitely forbidden, it can be called a bug yes. I
think the next patch would fix the problem:
Index: tables/tableExtension.pyx
===================================================================
--- tables/tableExtension.pyx (revision 4232)
+++ tables/tableExtension.pyx (working copy)
@@ -1143,6 +1143,12 @@
table._markColumnsAsDirty(self.modified_fields)
+ def __contains__(self, item):
+ raise (NotImplementedError,
+ "You cannot use a Row instance this way. "
+ "Use `Table.colnames` or `Table.colpathnames` better.")
+
+
# This method is twice as faster than __getattr__ because there is
# not a lookup in the local dictionary
def __getitem__(self, key):
and your script would look like the attached one.
I'll file a ticket about this. Thanks!
Francesc
import numpy as np
import tables as tb
dtype = np.dtype([('number', np.int)])
structar = np.arange(10).view(dtype)
h5f = tb.openFile('testfile.h5','w')
t = h5f.createTable(h5f.root, 'testtable', structar)
h5f.flush()
print "this works as expected"
for row in t.iterrows():
print row
print "this doesn't"
for row in t.iterrows():
#if 'number' in row:
if 'number' in t.colnames:
pass
print row
h5f.close()
------------------------------------------------------------------------------
Let Crystal Reports handle the reporting - Free Crystal Reports 2008 30-Day
trial. Simplify your report design, integration and deployment - and focus on
what you do best, core application coding. Discover what's new with
Crystal Reports now. http://p.sf.net/sfu/bobj-july
_______________________________________________
Pytables-users mailing list
Pytables-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/pytables-users