Skip to main content

Questions tagged [context-manager]

For code implementing a Python (or python-like) context manager, that can be scoped using the "with" statement.

Filter by
Sorted by
Tagged with
4 votes
6 answers
702 views

Generate all lines from a file matching a given word list

I have a script - generator that accepts a file name or file object and a list of search words as input. The generator searches through the lines of the file and returns only those lines (the whole ...
zaelcovsky's user avatar
2 votes
1 answer
92 views

Is using a generator of context managers kosher?

I know this is not necessarily a problem question but more of a style question. I understand that it doesn't have to be pretty for the most part. In general, context managers are used for mutexing, ...
georgwalker45's user avatar
2 votes
0 answers
105 views

Python context manager for importing modules outside my local directory

I implemented this context manager for importing modules from arbitrary directories. Good? Bad? - Keen to get thoughts: Implementation ...
MYK's user avatar
  • 141
1 vote
2 answers
844 views

More Pythonic Context Manager Wrapper

I wanted to ask this here because what I wanted to originally do felt like a really Pythonic method. I want to be able to use the syntax: ...
Stephen's user avatar
  • 125
5 votes
2 answers
2k views

Dictionary as context manager

I've created a class that implements customized addition, subtraction, etc. But for this class I'd like to have some additional parameters for these methods (__add__...
MSeifert's user avatar
  • 740
0 votes
1 answer
860 views

Context manager for SMTP connections

My code right now look like this ...
user1685095's user avatar
7 votes
1 answer
173 views

Switch like context manager in Python

This is self-explaining example with usage in doctests (it's not that fast as implementation with dict key-lookups, but it's a lot more readable, and don't require callables and lambdas): ...
canni's user avatar
  • 211