Popular recipes tagged "with_statement" but not "iterable"http://code.activestate.com/recipes/tags/with_statement-iterable/2013-12-16T21:43:27-08:00ActiveState Code RecipesUse PDFWriter with context manager support (Python)
2013-12-16T21:43:27-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/578790-use-pdfwriter-with-context-manager-support/
<p style="color: grey">
Python
recipe 578790
by <a href="/recipes/users/4173351/">Vasudev Ram</a>
(<a href="/recipes/tags/contextmanager/">contextmanager</a>, <a href="/recipes/tags/context_manager/">context_manager</a>, <a href="/recipes/tags/pdf/">pdf</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/reportlab/">reportlab</a>, <a href="/recipes/tags/with_statement/">with_statement</a>, <a href="/recipes/tags/xtopdf/">xtopdf</a>).
</p>
<p>PDFWriter - a core class of the xtopdf toolkit - can now be used with a Python context manager, a.k.a. the Python <strong>with</strong> statement.</p>
<p>Code example below. More details here:</p>
<p><a href="http://jugad2.blogspot.in/2013/12/xtopdf-pdfwriter-now-has-context.html" rel="nofollow">http://jugad2.blogspot.in/2013/12/xtopdf-pdfwriter-now-has-context.html</a></p>
Decorator and context manager from a single API (Python)
2010-06-27T15:15:01-07:00Michael Foordhttp://code.activestate.com/recipes/users/2183852/http://code.activestate.com/recipes/577273-decorator-and-context-manager-from-a-single-api/
<p style="color: grey">
Python
recipe 577273
by <a href="/recipes/users/2183852/">Michael Foord</a>
(<a href="/recipes/tags/contextmanager/">contextmanager</a>, <a href="/recipes/tags/context_manager/">context_manager</a>, <a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/decorators/">decorators</a>, <a href="/recipes/tags/with/">with</a>, <a href="/recipes/tags/with_statement/">with_statement</a>).
Revision 12.
</p>
<p>Create objects that act as both context managers <em>and</em> as decorators, and behave the same in both cases.</p>
<p>Works with Python 2.4 - 2.7 and Python 3. The tests require unittest2 or Python 3.2 to run. (And because the tests use the with statement they won't work with Python 2.4.)</p>
<p>Example:</p>
<pre class="prettyprint"><code>from contextdecorator import ContextDecorator
class mycontext(ContextDecorator):
def __init__(self, *args):
"""Normal initialiser"""
def before(self):
"""
Called on entering the with block or starting the decorated function.
If used in a with statement whatever this method returns will be the
context manager.
"""
def after(self, *exc):
"""
Called on exit. Arguments and return value of this method have
the same meaning as the __exit__ method of a normal context
manager.
"""
</code></pre>
<p>Both before and after methods are optional (but providing neither is somewhat pointless). See the tests for more usage examples.</p>
Temporary substitution of object in modules via with statement (Python)
2010-01-24T04:19:12-08:00Jacob Oscarsonhttp://code.activestate.com/recipes/users/1355144/http://code.activestate.com/recipes/577020-temporary-substitution-of-object-in-modules-via-wi/
<p style="color: grey">
Python
recipe 577020
by <a href="/recipes/users/1355144/">Jacob Oscarson</a>
(<a href="/recipes/tags/modules/">modules</a>, <a href="/recipes/tags/testing/">testing</a>, <a href="/recipes/tags/with_statement/">with_statement</a>).
</p>
<p>See docstring in the code</p>
'with' statement for StringIO (Python)
2009-02-24T00:01:16-08:00sridhttp://code.activestate.com/recipes/users/4053089/http://code.activestate.com/recipes/576650-with-statement-for-stringio/
<p style="color: grey">
Python
recipe 576650
by <a href="/recipes/users/4053089/">srid</a>
(<a href="/recipes/tags/stringio/">stringio</a>, <a href="/recipes/tags/with_statement/">with_statement</a>).
Revision 3.
</p>
<p>NOTE: Consider this recipe obsolete. Instead use <code>contextlib.closing</code> (see comment below).</p>
<p>This contextmanager adds 'with' statement support for StringIO. Peruse the following simple example:</p>
<pre class="prettyprint"><code>with StringIO() as sio:
function_accepting_file_handle(sio)
print sio.getvalue()
</code></pre>