Context Managers in Python

Senior Data Engineer • Contractor / Freelancer • GCP & AWS Certified
Search for a command to run...

Senior Data Engineer • Contractor / Freelancer • GCP & AWS Certified
No comments yet. Be the first to comment.
Concise guides to Python features and patterns most useful for data engineering — from built-ins to standard library modules.
Crafting readable, maintainable, and organized code is a North Star of Software Engineering. Let's look at a quick tip that nudges us towards this ideal. Have you encountered Enums in #Python yet? Enums, short for "enumerations", represent a distinct...
Here's a useful Dataform concept: pre_operations and post_operations. As the name implies, these represent a set of actions that run before and after the main operation (table, view, or SQL operations

BigQuery has always been a SQL engine for tabular data. Object tables add an interesting twist to that. Instead of rows containing values, an object table gives you one row per file — pointing at da

Query your data lake with warehouse-grade security and performance — without moving a single file.

Ever run a heavy BigQuery SQL query, processed gigabytes of data — and then accidentally closed the tab or forgot to save the results? 😬 Don't re-run it. Your results are still there. BigQuery automa

You can use query parameters in BigQuery hashtag#SQL (now in the console as well!) — but how are they different from variables, and when should you use each? Both parameters and variables act as place

Ever encountered the with statement? I've seen them around lots of times, but once, when asked about Context Managers during an interview, I didn't know what they were! Here's what to know about them, so you won't repeat my mistake.
You might've used it without realizing it, especially while working with files. For instance:
with open('test.txt', 'w') as f:
f.write('Test')
Why do we need a Context Manager in Python anyway?
Managing resources like files, databases, or network connections is very common in programming. Ensuring these resources are appropriately released/closed after usage is vital to prevent issues like hanging connections or file access problems.
Context Managers help by:
1️⃣ Managing resources, ensuring safe usage and disposal.
2️⃣ Executing clean-up operations, like closing files or connections, even when errors pop up.
3️⃣ Enhancing code readability and ease of refactoring.
For Data Engineers, why should this matter?
While constructing a data pipeline that involves acquiring and releasing resources (like files or database connections), a context manager ensures proper closure, even after errors.
Wondering how to define a Context Manager?
A class just needs to implement two magical dunder methods: __enter__() (which produces the resource) and __exit__() (which handles cleanup). See below a simple example illustrating how context managers work behind the scenes.

Found it useful? Subscribe to my Analytics newsletter at notjustsql.com.
Enjoyed this? Here are some related articles you might find useful: