Context Managers: a Data Scientist’s View | by Tim Meehan | Nov, 2020

[ad_1]


https://unsplash.com/photos/KVihRByJR5g?utm_source=unsplash&utm_medium=referral&utm_content=creditShareLink

How python context managers can clean up your code

This post is part of a series where I will be sharing things I’m learning on the topic of clean python code. I am a data scientist seeking to level up my python skills by writing more pythonic code and finding better ways to structure my larger code bases. I’m reading through Clean Code in Python and enriching the material with other sources. My goal is to reinforce the topics I’m learning about by summarizing them here and hopefully help others understand these topics as well!

The best way to describe context managers is to show an example almost every Python programmer has encountered at some point, unknowing they were using one! The bellow code snippet opens a .txt file and stores the lines in a python list. Let’s see what this looks like:

with open('example.txt') as f:
lines = f.readlines()
print(lines)
f = open('example.txt')
lines = f.readlines()
f.close()
print(lines)

To illustrate how to write your own, we’ll use a practical example. I recently ran into an issue while webscraping using selenium where I was unable to open a new url in an existing browser. The solution was to open a new browser to visit the url and close it after I’ve scraped the data. There’s two ways to do this. We’ll start with defining a class and use the __enter__ and __exit__ dunder methods.

# imports
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
# define context manager class
class OpenBrowser():

def __enter__(self):
self.driver = webdriver.Chrome(chrome_driver_path)
return self.driver

def __exit__(self, exception_type, exception_value,
exception_traceback):
self.driver.close()

# use context manager
with OpenBrowser() as driver:
driver.get("http://www.python.org")
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
html = driver.page_source
print(html)
# imports
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import contextlib
@contextlib.contextmanager
def open_browser():
driver = webdriver.Chrome(chrome_driver_path)
yield driver
driver.close()
with open_browser() as driver:
driver.get("http://www.python.org")
elem = driver.find_element_by_name("q")
elem.clear()
elem.send_keys("pycon")
elem.send_keys(Keys.RETURN)
html = driver.page_source

I hope I’ve not only explained context managers well enough, but convinced you that they can be extremely valuable to have in your tool box. Even as a data scientist, I have found many cases where these came in handy. Feel free to leave any questions or comments, happy coding!



[ad_2]




Source link

Write a comment