Repository navigation
Expand file tree
/
Copy pathContextManagers.html
More file actions
312 lines (288 loc) · 22.2 KB
/
Copy pathContextManagers.html
File metadata and controls
312 lines (288 loc) · 22.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Context Managers — PythonCert 4.0 documentation</title>
<link rel="stylesheet" href="../_static/alabaster.css" type="text/css" />
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
<script type="text/javascript">
var DOCUMENTATION_OPTIONS = {
URL_ROOT: '../',
VERSION: '4.0',
COLLAPSE_INDEX: false,
FILE_SUFFIX: '.html',
HAS_SOURCE: true,
SOURCELINK_SUFFIX: '.txt'
};
</script>
<script type="text/javascript" src="../_static/jquery.js"></script>
<script type="text/javascript" src="../_static/underscore.js"></script>
<script type="text/javascript" src="../_static/doctools.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
<link rel="index" title="Index" href="../genindex.html" />
<link rel="search" title="Search" href="../search.html" />
<link rel="next" title="Decorators" href="Decorators.html" />
<link rel="prev" title="Comprehensions" href="Comprehensions.html" />
<link rel="stylesheet" href="../_static/custom.css" type="text/css" />
<meta name="viewport" content="width=device-width, initial-scale=0.9, maximum-scale=0.9" />
</head>
<body>
<div class="document">
<div class="documentwrapper">
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="context-managers">
<h1>Context Managers<a class="headerlink" href="#context-managers" title="Permalink to this headline">¶</a></h1>
<p><strong>Repetition in code stinks (DRY!)</strong></p>
<div class="left build docutils container">
<p>A large source of repetition in code deals with the handling of external
resources.</p>
<p>As an example, how many times do you think you might type the following
code:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="n">file_handle</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'filename.txt'</span><span class="p">,</span> <span class="s1">'r'</span><span class="p">)</span>
<span class="n">file_content</span> <span class="o">=</span> <span class="n">file_handle</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="n">file_handle</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="c1"># do some stuff with the contents</span>
</pre></div>
</div>
<p>What happens if you forget to call <code class="docutils literal"><span class="pre">.close()</span></code>?</p>
<p>What happens if reading the file raises an exception?</p>
</div>
<div class="section" id="resource-handling">
<h2>Resource Handling<a class="headerlink" href="#resource-handling" title="Permalink to this headline">¶</a></h2>
<p>Leaving an open file handle laying around is bad enough. What if the resource
is a network connection, or a database cursor?</p>
<p>You can write more robust code for handling your resources:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">try</span><span class="p">:</span>
<span class="n">file_handle</span> <span class="o">=</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'filename.txt'</span><span class="p">,</span> <span class="s1">'r'</span><span class="p">)</span>
<span class="n">file_content</span> <span class="o">=</span> <span class="n">file_handle</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="k">finally</span><span class="p">:</span>
<span class="n">file_handle</span><span class="o">.</span><span class="n">close</span><span class="p">()</span>
<span class="c1"># do something with file_content here</span>
</pre></div>
</div>
<p>But what exceptions do you want to catch? And do you really want to have to
remember to type all that <strong>every</strong> time you open a resource?</p>
<p>Starting in version 2.5, Python provides a structure for reducing the
repetition needed to handle resources like this.</p>
<p class="centered"><strong>Context Managers</strong></p>
<p>You can encapsulate the setup, error handling and teardown of resources in a
few simple steps.</p>
<p>The key is to use the <code class="docutils literal"><span class="pre">with</span></code> statement.</p>
</div>
<div class="section" id="with-a-little-help">
<h2><code class="docutils literal"><span class="pre">with</span></code> a little help<a class="headerlink" href="#with-a-little-help" title="Permalink to this headline">¶</a></h2>
<p>Since the introduction of the <code class="docutils literal"><span class="pre">with</span></code> statement in <a class="reference external" href="http://legacy.python.org/dev/peps/pep-0343/">pep343</a>, the above six
lines of defensive code have been replaced with this simple form:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">with</span> <span class="nb">open</span><span class="p">(</span><span class="s1">'filename'</span><span class="p">,</span> <span class="s1">'r'</span><span class="p">)</span> <span class="k">as</span> <span class="n">file_handle</span><span class="p">:</span>
<span class="n">file_content</span> <span class="o">=</span> <span class="n">file_handle</span><span class="o">.</span><span class="n">read</span><span class="p">()</span>
<span class="c1"># do something with file_content</span>
</pre></div>
</div>
<p><code class="docutils literal"><span class="pre">open</span></code> builtin is defined as a <em>context manager</em>.</p>
<p>The resource it returns (<code class="docutils literal"><span class="pre">file_handle</span></code>) is automatically and reliably closed
when the code block ends.</p>
<p>At this point in Python history, many functions you might expect to behave this
way do:</p>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">open</span></code> and works as a context manager.</li>
<li>networks connections via <code class="docutils literal"><span class="pre">socket</span></code> do as well.</li>
<li>most implementations of database wrappers can open connections or cursors as
context managers.</li>
<li>…</li>
<li>But what if you are working with a library that doesn’t support this
(<code class="docutils literal"><span class="pre">urllib</span></code>)?</li>
</ul>
</div>
<div class="section" id="close-it-automatically">
<h2>Close It Automatically<a class="headerlink" href="#close-it-automatically" title="Permalink to this headline">¶</a></h2>
<p>There are a couple of ways you can go.</p>
<p>If the resource in questions has a <code class="docutils literal"><span class="pre">.close()</span></code> method, then you can simply use
the <code class="docutils literal"><span class="pre">closing</span></code> context manager from <code class="docutils literal"><span class="pre">contextlib</span></code> to handle the issue:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">urllib</span> <span class="kn">import</span> <span class="n">request</span>
<span class="kn">from</span> <span class="nn">contextlib</span> <span class="kn">import</span> <span class="n">closing</span>
<span class="k">with</span> <span class="n">closing</span><span class="p">(</span><span class="n">request</span><span class="o">.</span><span class="n">urlopen</span><span class="p">(</span><span class="s1">'http://google.com'</span><span class="p">))</span> <span class="k">as</span> <span class="n">web_connection</span><span class="p">:</span>
<span class="c1"># do something with the open resource</span>
<span class="c1"># and here, it will be closed automatically</span>
</pre></div>
</div>
<p>But what if the thing doesn’t have a <code class="docutils literal"><span class="pre">close()</span></code> method, or you’re creating
the thing and it shouldn’t have a close() method?</p>
<p>(full confession: urlib.request was not a context manager in py2 – but it is in py3)</p>
</div>
<div class="section" id="do-it-yourself">
<h2>Do It Yourself<a class="headerlink" href="#do-it-yourself" title="Permalink to this headline">¶</a></h2>
<p>You can also define a context manager of your own.</p>
<p>The interface is simple. It must be a class that implements two
more of the nifty python <em>special methods</em></p>
<p><strong>__enter__(self)</strong> Called when the <code class="docutils literal"><span class="pre">with</span></code> statement is run, it should
return something to work with in the created context.</p>
<p><strong>__exit__(self, e_type, e_val, e_traceback)</strong> Clean-up that needs to
happen is implemented here.</p>
<p>The arguments will be the exception raised in the context.</p>
<p>If the exception will be handled here, return True. If not, return False.</p>
<p>Let’s see this in action to get a sense of what happens.</p>
</div>
<div class="section" id="an-example">
<h2>An Example<a class="headerlink" href="#an-example" title="Permalink to this headline">¶</a></h2>
<p>Consider this code:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Context</span><span class="p">(</span><span class="nb">object</span><span class="p">):</span>
<span class="sd">"""from Doug Hellmann, PyMOTW</span>
<span class="sd"> https://pymotw.com/3/contextlib/#module-contextlib</span>
<span class="sd"> """</span>
<span class="k">def</span> <span class="fm">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">handle_error</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="s1">'__init__({})'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">handle_error</span><span class="p">))</span>
<span class="bp">self</span><span class="o">.</span><span class="n">handle_error</span> <span class="o">=</span> <span class="n">handle_error</span>
<span class="k">def</span> <span class="fm">__enter__</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="s1">'__enter__()'</span><span class="p">)</span>
<span class="k">return</span> <span class="bp">self</span>
<span class="k">def</span> <span class="fm">__exit__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">exc_type</span><span class="p">,</span> <span class="n">exc_val</span><span class="p">,</span> <span class="n">exc_tb</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="s1">'__exit__({}, {}, {})'</span><span class="o">.</span><span class="n">format</span><span class="p">(</span><span class="n">exc_type</span><span class="p">,</span> <span class="n">exc_val</span><span class="p">,</span> <span class="n">exc_tb</span><span class="p">))</span>
<span class="k">return</span> <span class="bp">self</span><span class="o">.</span><span class="n">handle_error</span>
</pre></div>
</div>
<p><code class="docutils literal"><span class="pre">Examples/Session10/context_managers.py</span></code></p>
<p>This class doesn’t do much of anything, but playing with it can help
clarify the order in which things happen:</p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [46]: </span><span class="k">with</span> <span class="n">Context</span><span class="p">(</span><span class="bp">True</span><span class="p">)</span> <span class="k">as</span> <span class="n">foo</span><span class="p">:</span>
<span class="gp"> ....: </span> <span class="k">print</span><span class="p">(</span><span class="s1">'This is in the context'</span><span class="p">)</span>
<span class="gp"> ....: </span> <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">'this is the error message'</span><span class="p">)</span>
<span class="go">__init__(True)</span>
<span class="go">__enter__()</span>
<span class="go">This is in the context</span>
<span class="go">__exit__(<type 'exceptions.RuntimeError'>, this is the error message, <traceback object at 0x1049cca28>)</span>
</pre></div>
</div>
<div class="build docutils container">
Because the exit method returns True, the raised error is ‘handled’.</div>
<p>What if we try with <code class="docutils literal"><span class="pre">False</span></code>?</p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [47]: </span><span class="k">with</span> <span class="n">Context</span><span class="p">(</span><span class="bp">False</span><span class="p">)</span> <span class="k">as</span> <span class="n">foo</span><span class="p">:</span>
<span class="gp"> ....: </span> <span class="k">print</span><span class="p">(</span><span class="s1">'This is in the context'</span><span class="p">)</span>
<span class="gp"> ....: </span> <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">'this is the error message'</span><span class="p">)</span>
<span class="go">__init__(False)</span>
<span class="go">__enter__()</span>
<span class="go">This is in the context</span>
<span class="go">__exit__(<type 'exceptions.RuntimeError'>, this is the error message, <traceback object at 0x1049ccb90>)</span>
<span class="gt">---------------------------------------------------------------------------</span>
<span class="ne">RuntimeError</span><span class="g g-Whitespace"> </span>Traceback (most recent call last)
<span class="nn"><ipython-input-47-de2c0c873dfc></span> in <span class="ni"><module></span><span class="nt">()</span>
<span class="g g-Whitespace"> </span><span class="mi">1</span> <span class="k">with</span> <span class="n">Context</span><span class="p">(</span><span class="bp">False</span><span class="p">)</span> <span class="k">as</span> <span class="n">foo</span><span class="p">:</span>
<span class="g g-Whitespace"> </span><span class="mi">2</span> <span class="k">print</span> <span class="s1">'This is in the context'</span>
<span class="ne">----> </span><span class="mi">3</span> <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s1">'this is the error message'</span><span class="p">)</span>
<span class="g g-Whitespace"> </span><span class="mi">4</span>
<span class="ne">RuntimeError</span>: this is the error message
</pre></div>
</div>
</div>
<div class="section" id="the-contextmanager-decorator">
<h2>The <code class="docutils literal"><span class="pre">contextmanager</span></code> decorator<a class="headerlink" href="#the-contextmanager-decorator" title="Permalink to this headline">¶</a></h2>
<p><code class="docutils literal"><span class="pre">contextlib.contextmanager</span></code> turns generator functions into context managers.</p>
<p>Consider this code:</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">contextlib</span> <span class="kn">import</span> <span class="n">contextmanager</span>
<span class="nd">@contextmanager</span>
<span class="k">def</span> <span class="nf">context</span><span class="p">(</span><span class="n">boolean</span><span class="p">):</span>
<span class="k">print</span><span class="p">(</span><span class="s2">"__init__ code here"</span><span class="p">)</span>
<span class="k">try</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="s2">"__enter__ code goes here"</span><span class="p">)</span>
<span class="k">yield</span> <span class="nb">object</span><span class="p">()</span>
<span class="k">except</span> <span class="ne">Exception</span> <span class="k">as</span> <span class="n">e</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="s2">"errors handled here"</span><span class="p">)</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">boolean</span><span class="p">:</span>
<span class="k">raise</span> <span class="n">e</span>
<span class="k">finally</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="s2">"__exit__ cleanup goes here"</span><span class="p">)</span>
</pre></div>
</div>
<p>The code is similar to the class defined previously.</p>
<p>And using it has similar results. We can handle errors:</p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [96]: </span><span class="k">with</span> <span class="n">context</span><span class="p">(</span><span class="bp">True</span><span class="p">):</span>
<span class="gp"> ....: </span> <span class="k">print</span><span class="p">(</span><span class="s2">"in the context"</span><span class="p">)</span>
<span class="gp"> ....: </span> <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">"error raised"</span><span class="p">)</span>
<span class="gp"> ....:</span>
<span class="go">__init__ code here</span>
<span class="go">__enter__ code goes here</span>
<span class="go">in the context</span>
<span class="go">errors handled here</span>
<span class="go">__exit__ cleanup goes here</span>
</pre></div>
</div>
<p>Or, we can allow them to propagate:</p>
<div class="highlight-ipython"><div class="highlight"><pre><span></span><span class="gp">In [51]: </span><span class="k">with</span> <span class="n">context</span><span class="p">(</span><span class="bp">False</span><span class="p">):</span>
<span class="gp"> ....: </span><span class="k">print</span><span class="p">(</span><span class="s2">"in the context"</span><span class="p">)</span>
<span class="gp"> ....: </span><span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">"error raised"</span><span class="p">)</span>
<span class="go">__init__ code here</span>
<span class="go">__enter__ code goes here</span>
<span class="go">in the context</span>
<span class="go">errors handled here</span>
<span class="go">__exit__ cleanup goes here</span>
<span class="gt">---------------------------------------------------------------------------</span>
<span class="ne">RuntimeError</span><span class="g g-Whitespace"> </span>Traceback (most recent call last)
<span class="nn"><ipython-input-51-641528ffa695></span> in <span class="ni"><module></span><span class="nt">()</span>
<span class="g g-Whitespace"> </span><span class="mi">1</span> <span class="k">with</span> <span class="n">context</span><span class="p">(</span><span class="bp">False</span><span class="p">):</span>
<span class="g g-Whitespace"> </span><span class="mi">2</span> <span class="k">print</span> <span class="s2">"in the context"</span>
<span class="ne">----> </span><span class="mi">3</span> <span class="k">raise</span> <span class="ne">RuntimeError</span><span class="p">(</span><span class="s2">"error raised"</span><span class="p">)</span>
<span class="g g-Whitespace"> </span><span class="mi">4</span>
<span class="ne">RuntimeError</span>: error raised
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>
<div class="sphinxsidebar" role="navigation" aria-label="main navigation">
<div class="sphinxsidebarwrapper">
<h3><a href="../index.html">Table Of Contents</a></h3>
<ul>
<li><a class="reference internal" href="#">Context Managers</a><ul>
<li><a class="reference internal" href="#resource-handling">Resource Handling</a></li>
<li><a class="reference internal" href="#with-a-little-help"><code class="docutils literal"><span class="pre">with</span></code> a little help</a></li>
<li><a class="reference internal" href="#close-it-automatically">Close It Automatically</a></li>
<li><a class="reference internal" href="#do-it-yourself">Do It Yourself</a></li>
<li><a class="reference internal" href="#an-example">An Example</a></li>
<li><a class="reference internal" href="#the-contextmanager-decorator">The <code class="docutils literal"><span class="pre">contextmanager</span></code> decorator</a></li>
</ul>
</li>
</ul>
<div class="relations">
<h3>Related Topics</h3>
<ul>
<li><a href="../index.html">Documentation overview</a><ul>
<li>Previous: <a href="Comprehensions.html" title="previous chapter">Comprehensions</a></li>
<li>Next: <a href="Decorators.html" title="next chapter">Decorators</a></li>
</ul></li>
</ul>
</div>
<div role="note" aria-label="source link">
<h3>This Page</h3>
<ul class="this-page-menu">
<li><a href="../_sources/modules/ContextManagers.rst.txt"
rel="nofollow">Show Source</a></li>
</ul>
</div>
<div id="searchbox" style="display: none" role="search">
<h3>Quick search</h3>
<form class="search" action="../search.html" method="get">
<div><input type="text" name="q" /></div>
<div><input type="submit" value="Go" /></div>
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
</div>
</div>
<div class="clearer"></div>
</div>
<div class="footer">
©2017, Christopher Barker, Cris Ewing, Christy Heaton, Maria McKinley, Rick Riehle, Joseph Schilz.
|
Powered by <a href="http://sphinx-doc.org/">Sphinx 1.6.2</a>
& <a href="https://github.com/bitprophet/alabaster">Alabaster 0.7.10</a>
|
<a href="../_sources/modules/ContextManagers.rst.txt"
rel="nofollow">Page source</a>
</div>
</body>
</html>