Skip to content

Instantly share code, notes, and snippets.

@jerilkuriakose
Created September 26, 2017 07:23
Show Gist options
  • Save jerilkuriakose/16dea095fe785eeb7a1fd8009845deec to your computer and use it in GitHub Desktop.
Save jerilkuriakose/16dea095fe785eeb7a1fd8009845deec to your computer and use it in GitHub Desktop.
# import the required library to open HDF file
from pandas import HDFStore
class OpenHDFS():
"""
Context Manager class to open / create a HDFS / h5 file
"""
def __init__(self, filename):
"""
The initialization function, takes the filename
e.g., "data.h5"
"""
self.filename = filename
def __enter__(self):
"""
Enter the runtime context related to OpenHDFS object
Opens / creates a HDFS file and returns it.
"""
self.open_file = HDFStore(self.filename)
return self.open_file
def __exit__(self, *args):
"""
Exit the runtime context related to OpenHDFS object
Makes sure the file gets closed
"""
self.open_file.close()
@MarkWilliamMatthews
Copy link

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment