Context managers using Python for HDFStore
--
In this post we will be making a context manager using Python. Context managers comes in handy when we are using the with
statement in Python. We will be making a context manager for writing HDFS file in Pandas. This will be a continuation of my previous post, regarding HDFS using Python Pandas.
We will start be creating a class for our context manager:
__enter__
and __exit__
are the magic functions of with
statement. __enter__
is called immediately after the __init__
function and __exit__
is called at the end of the with
statement.
Now lets use our context manager class:
This will be our entire code:
This can be also used with opening databases, files, and a lot more.
Happy coding !!!