Code Challenge 09 - The With Statement and Context Managers - Review

PyBites, Sat 11 March 2017, Challenges

code review, codechallenges, contextmanagers, learning, Paramiko, pytest, with

It's end of the week again so we review the code challenge of this week. It's never late to join, just fork our challenges repo and start coding.

Learning

Julian

Admittedly, when Bob and I discussed this challenge, I was a little intimidated. I'd never actually dealt with any of these concepts before! That said, I'm so glad we put this one out there!

If it wasn't for the challenge, I wouldn't have learned nearly as much as I did.

Given my current job role, I decided that it'd be fun to try and wrap a context manager around SSH functionality. This was a perfect choice given you need to specifically call .close() to close off the SSH session. If not, it just stays open indefinitely (or until timeout).

All up, I had to solidify my understanding of generators, context managers and ssh within Python. In a venv, I installed the paramiko SSH package and went from there.

After figuring out how to get the SSH connection going, I then spent the time bundling it all up within the context manager. Dan Bader's post; the pep-0343 doc and a few Googled questions on Stack Overflow helped sort me out.

See the full code here. Not only does it work, but I'm also now using at home with my NAS!

Bob

This was a nice challenge. I got inspired by the DB rollback example of PEP 343.

I wanted to see if I could use some existing code so I re-used the test code of my Simple Flask API article. Just as an exercise, in real life I actually like the setUp/tearDown sandwich unittest already provides.

What I really liked was that I got to think about different ways to implement this: class vs contextmanager. All this experimentation led to some good learning.

And I got to use pytest which I really want to explore further :)

(venv) [[email protected] 09 (solutions)]$ pytest with_tes*
=== test session starts ===
...

with_testdb.py ..
with_testdb_class.py ..

=== 4 passed in 0.01 seconds ===

Feedback

What was your solution? Feel free to share in the comments below.

We hope you enjoy these challenges. Please provide us feedback if we can improve anything ...

If you have an interesting challenge you want us to feature, don't hesitate to reach out to us.

See you next week ...

PyBites Python Tips

Do you want to get 250+ concise and applicable Python tips in an ebook that will cost you less than 10 bucks (future updates included), check it out here.

Get our Python Tips Book

"The discussions are succinct yet thorough enough to give you a solid grasp of the particular problem. I just wish I would have had this book when I started learning Python." - Daniel H

"Bob and Julian are the masters at aggregating these small snippets of code that can really make certain aspects of coding easier." - Jesse B

"This is now my favourite first Python go-to reference." - Anthony L

"Do you ever go on one of those cooking websites for a recipe and have to scroll for what feels like an eternity to get to the ingredients and the 4 steps the recipe actually takes? This is the opposite of that." - Sergio S

Get the book