Beispiel #1
0
def test_tohdf5():
    
    # set up a new hdf5 table to work with
    h5file = openFile("test2.h5", mode="w", title="Test file")
    h5file.createGroup('/', 'testgroup', 'Test Group')
    class FooBar(IsDescription):
        foo = Int32Col(pos=0)
        bar = StringCol(6, pos=2)
    h5file.createTable('/testgroup', 'testtable', FooBar, 'Test Table')
    h5file.flush()
    h5file.close()
    
    # load some data via tohdf5
    table1 = (('foo', 'bar'),
              (1, 'asdfgh'),
              (2, 'qwerty'),
              (3, 'zxcvbn'))
    
    tohdf5(table1, 'test2.h5', '/testgroup', 'testtable')
    ieq(table1, fromhdf5('test2.h5', '/testgroup', 'testtable'))

    tohdf5(table1, 'test2.h5', '/testgroup/testtable')
    ieq(table1, fromhdf5('test2.h5', '/testgroup/testtable'))

    h5file = openFile("test2.h5", mode="a")
    tohdf5(table1, h5file, '/testgroup/testtable')
    ieq(table1, fromhdf5(h5file, '/testgroup/testtable'))
    
    h5table = h5file.getNode('/testgroup/testtable')
    tohdf5(table1, h5table)
    ieq(table1, fromhdf5(h5table))
    
    # clean up
    h5file.close()
Beispiel #2
0
def test_tohdf5_create():
    
    
    class FooBar(IsDescription):
        foo = Int32Col(pos=0)
        bar = StringCol(6, pos=2)
    
    table1 = (('foo', 'bar'),
              (1, 'asdfgh'),
              (2, 'qwerty'),
              (3, 'zxcvbn'))

    # test creation with defined datatype
    tohdf5(table1, 'test3.h5', '/testgroup', 'testtable', create=True,
           description=FooBar, createparents=True)
    ieq(table1, fromhdf5('test3.h5', '/testgroup', 'testtable'))
    
    # test dynamically determined datatype
    tohdf5(table1, 'test3.h5', '/testgroup', 'testtable2', create=True,
           createparents=True)
    ieq(table1, fromhdf5('test3.h5', '/testgroup', 'testtable2'))
Beispiel #3
0
def test_appendhdf5():
    
    # set up a new hdf5 table to work with
    h5file = openFile("test4.h5", mode="w", title="Test file")
    h5file.createGroup('/', 'testgroup', 'Test Group')
    class FooBar(IsDescription):
        foo = Int32Col(pos=0)
        bar = StringCol(6, pos=2)
    h5file.createTable('/testgroup', 'testtable', FooBar, 'Test Table')
    h5file.flush()
    h5file.close()
    
    # load some initial data via tohdf5()
    table1 = (('foo', 'bar'),
              (1, 'asdfgh'),
              (2, 'qwerty'),
              (3, 'zxcvbn'))
    tohdf5(table1, 'test4.h5', '/testgroup', 'testtable')
    ieq(table1, fromhdf5('test4.h5', '/testgroup', 'testtable'))

    # append some more data
    appendhdf5(table1, 'test4.h5', '/testgroup', 'testtable')
    ieq(chain(table1, table1[1:]), fromhdf5('test4.h5', '/testgroup', 'testtable'))
Beispiel #4
0
for row in table1[1:]:
    for i, f in enumerate(table1[0]):
        h5table.row[f] = row[i]
    h5table.row.append()

h5table.cols.foo.createCSIndex() # CS index is required
h5file.flush()
h5file.close()

# access the data, sorted by the indexed column
from petl import look
from petlx.hdf5 import fromhdf5sorted
table2 = fromhdf5sorted('test1.h5', '/testgroup', 'testtable', sortby='foo')
look(table2)


# tohdf5
table1 = (('foo', 'bar'),
          (1, 'asdfgh'),
          (2, 'qwerty'),
          (3, 'zxcvbn'))

from petl import look
look(table1)
from petlx.hdf5 import tohdf5, fromhdf5
tohdf5(table1, 'test1.h5', '/testgroup', 'testtable', create=True, createparents=True)
look(fromhdf5('test1.h5', '/testgroup', 'testtable'))


Beispiel #5
0
table1 = (('foo', 'bar'), (3, 'asdfgh'), (2, 'qwerty'), (1, 'zxcvbn'))

for row in table1[1:]:
    for i, f in enumerate(table1[0]):
        h5table.row[f] = row[i]
    h5table.row.append()

h5table.cols.foo.createCSIndex()  # CS index is required
h5file.flush()
h5file.close()

# access the data, sorted by the indexed column
from petl import look
from petlx.hdf5 import fromhdf5sorted
table2 = fromhdf5sorted('test1.h5', '/testgroup', 'testtable', sortby='foo')
look(table2)

# tohdf5
table1 = (('foo', 'bar'), (1, 'asdfgh'), (2, 'qwerty'), (3, 'zxcvbn'))

from petl import look
look(table1)
from petlx.hdf5 import tohdf5, fromhdf5
tohdf5(table1,
       'test1.h5',
       '/testgroup',
       'testtable',
       create=True,
       createparents=True)
look(fromhdf5('test1.h5', '/testgroup', 'testtable'))