def setUp(self): """ This method sets the following instance attributes: * ``h5fname``: the name of the temporary HDF5 file. * ``h5file``: the writable, temporary HDF5 file with a ``/test`` node. * ``fnode``: the readable file node in ``/test``, with text in it. """ super(ReadlineTestCase, self).setUp() linesep = self.lineSeparator # Fill the node file with some text. fnode = filenode.newNode(self.h5file, where = '/', name = 'test') fnode.lineSeparator = linesep fnode.write(linesep) fnode.write('short line%sshort line%s%s' % ((linesep,) * 3)) fnode.write('long line ' * 20 + linesep) fnode.write('unterminated') fnode.close() # Re-open it for reading. self.fnode = filenode.openNode(self.h5file.getNode('/test')) self.fnode.lineSeparator = linesep
def test00_OpenFileRead(self): "Opening an existing file node for reading." node = self.h5file.getNode('/test') fnode = filenode.openNode(node) self.assertEqual( fnode.node, node, "filenode.openNode() opened the wrong node.") self.assertEqual( fnode.mode, 'r', "File was opened with an invalid mode %s." % repr(fnode.mode)) self.assertEqual( fnode.tell(), 0L, "Pointer is not positioned at the beginning of the file.") fnode.close()
def readFile(self, fileNodeToRead, outputfileName): """ Used to export a binary file within the HDFFIle to a output file. """ logging.debug(fileNodeToRead) fileNodeToRead = os.path.join( os.path.dirname(fileNodeToRead), self._makesafekey(os.path.basename(fileNodeToRead)) ) logging.debug(fileNodeToRead) fileNodeOb = self.hdfFile.getNode(fileNodeToRead) fnode = filenode.openNode(fileNodeOb, "r") with open(outputfileName, "w") as fdisk: fdisk.write(fnode.read()) fdisk.close() fnode.close()
def retrieve_srcdata(datastore, data_filename, output_filename): store = pd.HDFStore(datastore, "r") # pandas API change between 0.10.1 and 0.11 try: _h = store.handle except: _h = store._handle fnode = filenode.openNode(_h.getNode(where="/{0}".format(path_src), name=data_filename)) DATA = fnode.read() fnode.close() with open(output_filename, "wb") as fd: fd.write(DATA) del DATA store.close()
def setUp(self): """ This method sets the following instance attributes: * ``h5fname``: the name of the temporary HDF5 file. * ``h5file``: the writable, temporary HDF5 file with a ``/test`` node. * ``fnode``: the readable file node in ``/test``. """ self.h5fname = tempfile.mktemp(suffix = '.h5') self.oldh5fname = self._testFilename(self.oldh5fname) oldh5f = tables.openFile(self.oldh5fname) oldh5f.copyFile(self.h5fname) oldh5f.close() self.h5file = tables.openFile( self.h5fname, 'r+', title = "Test for file node old version compatibility") self.fnode = filenode.openNode(self.h5file.root.test, 'a+')
def setUp(self): """setUp() -> None This method sets the following instance attributes: * 'datafile', the opened data file * 'h5fname', the name of the temporary HDF5 file * 'h5file', the writable, temporary HDF5 file with a '/test' node * 'fnode', the readable file node in '/test', with data in it """ self.datafname = self._testFilename(self.datafname) self.datafile = file(self.datafname) super(ReadFileTestCase, self).setUp() fnode = filenode.newNode(self.h5file, where = '/', name = 'test') copyFileToFile(self.datafile, fnode) fnode.close() self.datafile.seek(0) self.fnode = filenode.openNode(self.h5file.getNode('/test'))
"""How to use the filenode module.""" from tables.nodes import filenode import tables h5file = tables.openFile('fnode.h5', 'w') fnode = filenode.newNode(h5file, where='/', name='fnode_test') print >> fnode, "This is a test text line." print >> fnode, "And this is another one." print >> fnode fnode.write("Of course, file methods can also be used.") fnode.close() node = h5file.root.fnode_test fnode = filenode.openNode(node, 'a+') print >> fnode, "This is a new line." fnode.attrs.content_type = 'text/plain; charset=us-ascii' fnode.attrs.author = "Ivan Vilata i Balaguer" fnode.attrs.creation_date = '2004-10-20T13:25:25+0200' fnode.attrs.keywords_en = ["FileNode", "test", "metadata"] fnode.attrs.keywords_ca = ["FileNode", "prova", "metadades"] fnode.attrs.owner = 'ivan' fnode.attrs.acl = {'ivan': 'rw', '@users': 'r'} fnode.close() h5file.close()
print >> fnode, "And this is another one." print >> fnode fnode.write("Of course, file methods can also be used.") fnode.seek(0) # Go back to the beginning of file. for line in fnode: print repr(line) fnode.close() print fnode.closed node = h5file.root.fnode_test fnode = filenode.openNode(node, 'a+') print repr(fnode.readline()) print fnode.tell() print >> fnode, "This is a new line." print repr(fnode.readline()) fnode.seek(0) for line in fnode: print repr(line) fnode.attrs.content_type = 'text/plain; charset=us-ascii' fnode.attrs.author = "Ivan Vilata i Balaguer"