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 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}")
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With