Created
September 26, 2017 07:23
-
-
Save jerilkuriakose/16dea095fe785eeb7a1fd8009845deec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks!