4

I am building an HDF5 file using PyTables python package. The file would be updated everyday with latest tick data. I want to create two groups - Quotes and Trades and tables for different futures expiries. I want to check if the group Quotes exists or not and if not then create it. What is the best way to do it in PyTables?

Here is a code snippet of where I am right now:

hdf_repos_filters = tables.Filters(complevel=1, complib='zlib')
for instrument in instruments:
    if options.verbose:
    hdf_file = os.path.join(dest_path, "{}.h5".format(instrument))
    store = tables.open_file(hdf_file, mode='a', filters=hdf_repos_filters)
    # This is where I want to check whether the group "Quotes" and "Trades" exist and if not create it
Kapil Sharma
  • 1,412
  • 1
  • 15
  • 19

2 Answers2

4

Kapil is on the right track in that you want to use the __contains__ method, although because it is a double underscore method it is not intended to be called directly rather through an alternate interface. In this case that interface is in. So to check a file hdf_file contains a group "Quotes" you can run:

with tables.open_file(hdf_file) as store:
    if "/Quotes" in store:
       print(f"Quotes already exists in the file {hdf_file}")
1

I think I have figured it out.

I am using the File.__contains__(path) method in the File class in PyTables.

As per the documentation:

File.__contains__(path)

Is there a node with that path?

Returns True if the file has a node with the given path (a string), False otherwise.

PyTables File class

Kapil Sharma
  • 1,412
  • 1
  • 15
  • 19