Hi Jan,

Sorry for the delay on answering this. We are pretty busy working in the
2.0 beta2 release.

El dj 22 de 03 del 2007 a les 03:53 +0000, en/na Jan Strube va
escriure:
> Before I am going to embark on using pytables2, I would like to know
> how free I am in choosing the dtype of a column.
> I would like to store a momentum in my table.
> Currently in pytables1 I am using something like 
> momentum    = Float32Col(shape=3)
> 
> But now that I know more about numpy, I am wondering if there is a way
> to specify the dtype of momentum, like so
> momentum    = Float32Col(dtype={'names': ('x','y', 'z'), 'formats':
> ( N.float32, N.float32, N.float32)})
> or something like that.

Uh, no. The dtype that you can pass is only limited to homogeneous
types, not heterogenous, like in your example. Maybe in a future
(PyTables 3.0?) we will introduce fully nested Column definitions
through the introduction of a more flexible Atom class. But this is only
something that Ivan and me have been talking about quite informally.

> The other way would be to have momentum a class with three components
> x, y, z.
> That would be OK, too, but then will I be able to assign the values to
> the row all at once? 
> Something like
> row = table.row
> row['momentum'] = (1, 2, 3)

Yes. That works in 2.0 right out-of-the-box (but not in 1.x series).
This is mainly due to the fact that the underlying NumPy recarray buffer
does support these kind of assignments. See the attachment for a
complete example on this capability and also the different ways to
retrieve the momentum (a nested field).

> Right now I am assigning a new dtype after reading the column, but if
> feels like a hack and I would like to be able to read the right dtype
> from disk. 

As I said, in the attached example there are also different ways to read
nested fields. Have a look at it and tell us if they fulfill your
requirements. If you think that the mechanism can be improved in some
way or another, it would be nice if you tell us about your thoughts.

Cheers,

-- 
Francesc Altet    |  Be careful about using the following code --
Carabos Coop. V.  |  I've only proven that it works, 
www.carabos.com   |  I haven't tested it. -- Donald Knuth
import tables as T

class Particle(T.IsDescription):
    class momentum(T.IsDescription):
        x = T.Float32Col()
        y = T.Float32Col()
        z = T.Float32Col()

# Create a table with just one single particle on it
f = T.openFile('/tmp/particles.h5', 'w')
table = f.createTable('/', 'table', Particle)
row = table.row
row['momentum'] = (1,2,3)
row.append()
f.close()

# Reopen the file in read-only mode
f = T.openFile('/tmp/particles.h5')
table = f.root.table

# Get the first (and single one) row
row = table[0]
momentum = row['momentum']  # This returns a 'void' NumPy object
print "momentum for the first row-->", momentum
print "components x, y & z -->", momentum['x'], momentum['y'], momentum['z']

# Using iterators
print "another way of doing the same:",
for row in table:
    print "momentum for the first row-->", row['momentum']
    print "components x, y & z -->", row['momentum/x'], \
          row['momentum/y'], row['momentum/z']

# Another way of using the row accessor from iterators
print "yet another way:"
for row in table:
    momentum = row['momentum']   # This returns a tuple
    print "components x, y & z -->", momentum[0], momentum[1], momentum[2]

f.close()
-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys-and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Pytables-users mailing list
Pytables-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/pytables-users

Reply via email to