def test_tohdf5(): f = NamedTemporaryFile() # set up a new hdf5 table to work with h5file = tables.open_file(f.name, mode="w", title="Test file") h5file.create_group('/', 'testgroup', 'Test Group') h5file.create_table('/testgroup', 'testtable', FooBar, 'Test Table') h5file.flush() h5file.close() # load some data via tohdf5 table1 = (('foo', 'bar'), (1, b'asdfgh'), (2, b'qwerty'), (3, b'zxcvbn')) tohdf5(table1, f.name, '/testgroup', 'testtable') ieq(table1, fromhdf5(f.name, '/testgroup', 'testtable')) tohdf5(table1, f.name, '/testgroup/testtable') ieq(table1, fromhdf5(f.name, '/testgroup/testtable')) h5file = tables.open_file(f.name, mode="a") tohdf5(table1, h5file, '/testgroup/testtable') ieq(table1, fromhdf5(h5file, '/testgroup/testtable')) h5table = h5file.get_node('/testgroup/testtable') tohdf5(table1, h5table) ieq(table1, fromhdf5(h5table)) # clean up h5file.close()
def test_tohdf5_create(): table1 = (('foo', 'bar'), (1, b'asdfgh'), (2, b'qwerty'), (3, b'zxcvbn')) f = NamedTemporaryFile() # test creation with defined datatype tohdf5(table1, f.name, '/testgroup', 'testtable', create=True, drop=True, description=FooBar, createparents=True) ieq(table1, fromhdf5(f.name, '/testgroup', 'testtable')) # test dynamically determined datatype tohdf5(table1, f.name, '/testgroup', 'testtable2', create=True, drop=True, createparents=True) ieq(table1, fromhdf5(f.name, '/testgroup', 'testtable2'))
def test_fromhdf5(): f = NamedTemporaryFile() # set up a new hdf5 table to work with h5file = tables.open_file(f.name, mode='w', title='Test file') h5file.create_group('/', 'testgroup', 'Test Group') h5table = h5file.create_table('/testgroup', 'testtable', FooBar, 'Test Table') # load some data into the table table1 = (('foo', 'bar'), (1, b'asdfgh'), (2, b'qwerty'), (3, b'zxcvbn')) for row in table1[1:]: for i, fld in enumerate(table1[0]): h5table.row[fld] = row[i] h5table.row.append() h5file.flush() h5file.close() # verify we can get the data back out table2a = fromhdf5(f.name, '/testgroup', 'testtable') ieq(table1, table2a) ieq(table1, table2a) # verify we can get the data back out table2b = fromhdf5(f.name, '/testgroup/testtable') ieq(table1, table2b) ieq(table1, table2b) # verify using an existing tables.File object h5file = tables.open_file(f.name) table3 = fromhdf5(h5file, '/testgroup/testtable') ieq(table1, table3) # verify using an existing tables.Table object h5tbl = h5file.get_node('/testgroup/testtable') table4 = fromhdf5(h5tbl) ieq(table1, table4) # verify using a condition to filter data table5 = fromhdf5(h5tbl, condition="(foo < 3)") ieq(table1[:3], table5) # clean up h5file.close()
def test_appendhdf5(): f = NamedTemporaryFile() # set up a new hdf5 table to work with h5file = tables.open_file(f.name, mode="w", title="Test file") h5file.create_group('/', 'testgroup', 'Test Group') h5file.create_table('/testgroup', 'testtable', FooBar, 'Test Table') h5file.flush() h5file.close() # load some initial data via tohdf5() table1 = (('foo', 'bar'), (1, b'asdfgh'), (2, b'qwerty'), (3, b'zxcvbn')) tohdf5(table1, f.name, '/testgroup', 'testtable') ieq(table1, fromhdf5(f.name, '/testgroup', 'testtable')) # append some more data appendhdf5(table1, f.name, '/testgroup', 'testtable') ieq(chain(table1, table1[1:]), fromhdf5(f.name, '/testgroup', 'testtable'))