Most viewed recipes tagged "with_statement"http://code.activestate.com/recipes/tags/with_statement/views/2013-12-16T21:43:27-08:00ActiveState Code Recipes'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> Use 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> Wrap any iterable context manager so it closes when consumed (Python) 2012-11-19T20:10:35-08:00Andrew Barnerthttp://code.activestate.com/recipes/users/4184316/http://code.activestate.com/recipes/578342-wrap-any-iterable-context-manager-so-it-closes-whe/ <p style="color: grey"> Python recipe 578342 by <a href="/recipes/users/4184316/">Andrew Barnert</a> (<a href="/recipes/tags/contextmanager/">contextmanager</a>, <a href="/recipes/tags/context_manager/">context_manager</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/iterable/">iterable</a>, <a href="/recipes/tags/iterator/">iterator</a>, <a href="/recipes/tags/with_statement/">with_statement</a>). </p> <p>There are a few types in Python—most notably, files—that are both iterators and context managers. For trivial cases, these features are easy to use together, but as soon as you need to use the iterator lazily or asynchronously, a with statement won't help. That's where this recipe comes in handy:</p> <pre class="prettyprint"><code>send_async(with_iter(open(path, 'r'))) </code></pre> <p>This also allows you to "forward" closing for a wrapped iterator, so closing the outer iterator also closes the inner one:</p> <pre class="prettyprint"><code>sync_async(line.upper() for line in with_iter(open(path, 'r'))) </code></pre> 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>