Skip to content

Instantly share code, notes, and snippets.

@jerilkuriakose
Last active September 26, 2017 07:37
Show Gist options
  • Save jerilkuriakose/82afaa6d22045dd5398da915a9f365d6 to your computer and use it in GitHub Desktop.
Save jerilkuriakose/82afaa6d22045dd5398da915a9f365d6 to your computer and use it in GitHub Desktop.
from pandas import DataFrame
# import the required library to open HDF file
from pandas import HDFStore
# File that stores the dataframe
filename = 'data.h5'
# Creating a dataframe
df = DataFrame(np.random.randn(5,3), columns=['A', 'B', 'C',])
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()
# Using our context manager
with OpenHDFS(filename) as store:
store.put('d1', df)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment