Pain Reliever— Context manager and Encoding detector in Python

piyush kumar
Forsk Coding School
4 min readMar 23, 2019

Resource management is the most important key to being a good programmer. Its practical example is that one of the main reason for Python popularity is resource management. While coding this is the most common problem faced by all programmers. In Python, Context managers play an important role to solve out this problem.

Context managers allow us to allocate and release resources precisely when we want. Seems no big deal after reading the above line. But after getting into this I found that this is a big deal. Somehow we are using the context managers in our python programs for writing better and clean code but we are not aware of context managers so much. Searching for it I came through a very common practical implementation of this

Let’s have a look at that example-

A simple code for file handling in Python

fileobj = open(“file.txt”,”w”)

fileobj.write(“hello”)

fileobj.close()

Every Python geek must be aware of it. It was fun to handle file so easily. But the real pain starts when we accidentally forgot to close the file and our changes were not reflected on the file. To overcome this pain we used the with keyword like this -

with open(“file.txt” ,”w”) as fileobj:

fileobj.write(“hello”)

We all used this but most of us are not aware that this is a technique of context management. with keyword is the most used context management technique in python programming.

To implement context management Python provide a library known as contextlib. This module enhances the functionality of with keyword as a context manager. There are numerous of functionality that is provided by contextlib module like asynchronous functionality, exception handling, creating own context manager, handling standard output message etc.

To use contextlib module we have to import it in our Python file-

import contextlib

I used the exception handling feature of contextlib module a lot and really I enjoyed using it. It has a method suppress that silently handles the exception passed in it as a parameter i.e means no exception message is displayed on the screen.

with contextlib.suppress((TypeError)):

…. sum = ‘11’+10

The above code is similar to -

try:

…. sum = ‘11’+10

except TypeError as e:

…. pass

I found suppress really useful as I don’t have to worry about the except block which was to be ignored while program execution as a pass was mentioned in its block.

There is also a powerful functionality of creating our own context manager in contextlib. Basically, the inner workflow of context manager consist of __enter__ and __exit__ methods but we can implement this by using contextmanger method of the module, generators and decorators.

Syntax -

from contextlib import contextmanager

@contextmanager

def func_name():

…. code

with func_name():

…. code

I tried to make a practical implementation of contextmanager by creating a simple program. To view it visit this link-

https://github.com/piyush546/Machine-Learning-Bootcamp/blob/master/Intermediate%20Python%20programs/Contextlib/Timecounter.py

There are lots of methods as mentioned above that are used for resource management in Python programming. For more details about contextlib module, the best resource is its official documentation which can be viewed here -

https://docs.python.org/3/library/contextlib.html

The discussion is not over yet. Let’s proceed towards another pain killer.

Absolutely the encoding detector is the biggest pain reliever for the geeks performing the data analysis through programming languages. Firstly they have to fetch and load the data for performing the analysis. But it’s not sure that every time our compiler or interpreter understand the encoding format of the data in the data file on its own. We are required to tell the compiler/ Interpreter encoding format of the data in the data file. And practically it is not possible to remember all the encoding formats.

Being a Machine Learning geek I faced this problem a lot while loading the dataset using Pandas for performing data analysis on them. One day I got stuck badly while loading a dataset in my one of the code challenges. How hard I tried but every time it displayed an error message that encoding format was not detected. After a lot of effort, I came across a Python module named chardet that helped me to solve my problem.

pip install chardet

chardet is a universal encoding detector module of Python that detects the encoding format of the characters in the given file. To get the encoding format of the characters in the dataset I added 4 lines of code in my program and after executing it I got the encoding format. After viewing the code snippet mentioned below the goal of describing these two topics under one article would also become more clear.

Firstly to use chardet module we have to import it

import chardet

After that following operation is performed to fetch the encoding format

with open(“abc.csv”, “rb”) as fileobj:

…. result = chardet.detect(fileobj.read())

print(result[‘encoding’])

The above snippet shows the use of context manager as well. Thus in this way the context manager and encoding detector act as a pain reliever in the Python programming world.

Thanks to my mentors and Forsk Technologies team who helped me a lot while preparing this article.

Any changes are cordially welcomed at my mail.

By Piyush Kumar(3rd Year, CSE)

Intern at Forsk Technologies, Jaipur

JECRC University, Jaipur

Mail-id: pamaa.py@gmail.com

--

--