-
Notifications
You must be signed in to change notification settings - Fork 0
/
2017-01-31-on-mocks-and-stubs-in-python.html
359 lines (311 loc) · 28.7 KB
/
2017-01-31-on-mocks-and-stubs-in-python.html
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link rel="alternate"
type="application/rss+xml"
href="https://magnus.therning.org/feed.xml"
title="RSS feed for https://magnus.therning.org/">
<title>On mocks and stubs in Python (free monad or interpreter pattern)</title>
<meta name="author" content="Magnus Therning"><meta name="referrer" content="no-referrer"><link href= "static/style.css" rel="stylesheet" type="text/css" /><link href= "static/htmlize.css" rel="stylesheet" type="text/css" /><link href= "static/extra_style.css" rel="stylesheet" type="text/css" /></head>
<body>
<div id="preamble" class="status"><div class="nav-bar"><a class="nav-link" href="./index.html">Top</a><a class="nav-link" href="./archive.html">Archive</a><a class="nav-link align-right" href="./feed.xml"><img src="static/rss-feed-icon.png" style="height: 24px;" /></a></div></div>
<div id="content">
<div class="post-date">31 Jan 2017</div><h1 class="post-title"><a href="https://magnus.therning.org/2017-01-31-on-mocks-and-stubs-in-python.html">On mocks and stubs in Python (free monad or interpreter pattern)</a></h1>
<p>
A few weeks ago I watched a video where <a href="https://youtu.be/EaxDl5NPuCA">Ken Scambler talks about mocks and
stubs</a>. In particular he talks about how to get rid of them.
</p>
<p>
What he's talking about is free monads, but I feel he's glossing over a lot of
details. Based on some of the questions asked during the talk I think I share
that feeling with some people in the audience. Specifically I feel he skipped
over the following:
</p>
<ul class="org-ul">
<li>How does one <i>actually</i> write such code in a mainstream OO/imperative language?</li>
<li>What's required of the language in order to allow using the techniques he's
talking about?</li>
<li>Errors tend to break abstractions, so how does one deal with error (i.e.
exceptions)?</li>
</ul>
<p>
Every time I've used mocks and stubs for unit testing I've had a feeling that
"this can't be how it's supposed to be done!" So to me, Ken's talk offered some
hope, and I <i>really</i> want to know how applicable the ideas are in mainstream
OO/imperative languages.
</p>
<div id="outline-container-org6d7fb38" class="outline-2">
<h2 id="org6d7fb38">The example</h2>
<div class="outline-text-2" id="text-org6d7fb38">
<p>
To play around with this I picked the following function (in Python):
</p>
<div class="org-src-container">
<pre class="src src-python"><span class="org-keyword">def</span> <span class="org-function-name">count_chars_of_file</span><span class="org-rainbow-delimiters-depth-1">(</span>fn<span class="org-rainbow-delimiters-depth-1">)</span>:
<span class="org-variable-name">fd</span> <span class="org-operator">=</span> os.<span class="org-builtin">open</span><span class="org-rainbow-delimiters-depth-1">(</span>fn, os.O_RDONLY<span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-variable-name">text</span> <span class="org-operator">=</span> os.read<span class="org-rainbow-delimiters-depth-1">(</span>fd, 10000<span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-variable-name">n</span> <span class="org-operator">=</span> <span class="org-builtin">len</span><span class="org-rainbow-delimiters-depth-1">(</span>text<span class="org-rainbow-delimiters-depth-1">)</span>
os.close<span class="org-rainbow-delimiters-depth-1">(</span>fd<span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-keyword">return</span> n
</pre>
</div>
<p>
It's small and simple, but I think it suffices to highlight a few important
points. So the goal is to rewrite this function such that calls to IO operations
(actions) (e.g. <code>os.read</code>) are replaced by data (an instance of some data type)
conveying the intent of the operation. This data can later be passed to an
<i>interpreter of actions</i>.
</p>
</div>
</div>
<div id="outline-container-orgfc1a5e7" class="outline-2">
<h2 id="orgfc1a5e7">Thoughts on the execution of actions and the interpreter pattern</h2>
<div class="outline-text-2" id="text-orgfc1a5e7">
<p>
When reading the examples in the <a href="http://www.oodesign.com/interpreter-pattern.html">description of the interpreter pattern</a> what
stands out to me is that they are either
</p>
<ol class="org-ol">
<li>a list of expressions, or</li>
<li>a tree of expressions</li>
</ol>
<p>
that is passed to an interpreter. Will this do for us when trying to rewrite <code>count_chars_of_file</code>?
</p>
<p>
No, it won't! Here's why:
</p>
<ul class="org-ul">
<li>A tree of actions doesn't really make sense. Our actions are small and simple,
they encode the intent of a single IO operation.</li>
<li>A list of actions can't deal with interspersed non-actions, in this case it's
the line <code>n = len(text)</code> that causes a problem.</li>
</ul>
<p>
The interpreter pattern misses something that is crucial in this case: the
running of the interpreter must be intermingled with running non-interpreted
code. The way I think of it is that not only the action needs to be present and
dealt with, but also <i>the rest of the program</i>, that latter thing is commonly
called a <i>continuation</i>.
</p>
<p>
So, can we introduce actions and rewrite <code>count_chars_of_file</code> such that we
pause the program when interpretation of an action is required, interpret it,
and then resume where we left off?
</p>
<p>
Sure, but it's not really idiomatic Python code!
</p>
</div>
</div>
<div id="outline-container-orgab04d41" class="outline-2">
<h2 id="orgab04d41">Actions and continuations</h2>
<div class="outline-text-2" id="text-orgab04d41">
<p>
The IO operations (actions) are represented as a named tuple:
</p>
<div class="org-src-container">
<pre class="src src-python"><span class="org-variable-name">Op</span> <span class="org-operator">=</span> collections.namedtuple<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-string">'Op'</span>, <span class="org-rainbow-delimiters-depth-2">[</span><span class="org-string">'op'</span>, <span class="org-string">'args'</span>, <span class="org-string">'k'</span><span class="org-rainbow-delimiters-depth-2">]</span><span class="org-rainbow-delimiters-depth-1">)</span>
</pre>
</div>
<p>
and the functions returning actions can then be written as
</p>
<div class="org-src-container">
<pre class="src src-python"><span class="org-keyword">def</span> <span class="org-function-name">cps_open</span><span class="org-rainbow-delimiters-depth-1">(</span>fn, k<span class="org-rainbow-delimiters-depth-1">)</span>:
<span class="org-keyword">return</span> Op<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-string">'open'</span>, <span class="org-rainbow-delimiters-depth-2">[</span>fn<span class="org-rainbow-delimiters-depth-2">]</span>, k<span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-keyword">def</span> <span class="org-function-name">cps_read</span><span class="org-rainbow-delimiters-depth-1">(</span>fd, k<span class="org-rainbow-delimiters-depth-1">)</span>:
<span class="org-keyword">return</span> Op<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-string">'read'</span>, <span class="org-rainbow-delimiters-depth-2">[</span>fd<span class="org-rainbow-delimiters-depth-2">]</span>, k<span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-keyword">def</span> <span class="org-function-name">cps_close</span><span class="org-rainbow-delimiters-depth-1">(</span>fd, k<span class="org-rainbow-delimiters-depth-1">)</span>:
<span class="org-keyword">return</span> Op<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-string">'close'</span>, <span class="org-rainbow-delimiters-depth-2">[</span>fd<span class="org-rainbow-delimiters-depth-2">]</span>, k<span class="org-rainbow-delimiters-depth-1">)</span>
</pre>
</div>
<p>
The interpreter is then an <i>if</i> statement checking the value of <code>op.op</code> with
each branch executing the IO operation and passing the result to the rest of the
program. I decided to wrap it directly in the program runner:
</p>
<div class="org-src-container">
<pre class="src src-python"><span class="org-keyword">def</span> <span class="org-function-name">runProgram</span><span class="org-rainbow-delimiters-depth-1">(</span>prog<span class="org-rainbow-delimiters-depth-1">)</span>:
<span class="org-keyword">def</span> <span class="org-function-name">runOp</span><span class="org-rainbow-delimiters-depth-1">(</span>op<span class="org-rainbow-delimiters-depth-1">)</span>:
<span class="org-indent-bars-1"> </span> <span class="org-keyword">if</span> op.op <span class="org-operator">==</span> <span class="org-string">'open'</span>:
<span class="org-indent-bars-1"> </span> <span class="org-indent-bars-2"> </span> <span class="org-variable-name">fd</span> <span class="org-operator">=</span> os.<span class="org-builtin">open</span><span class="org-rainbow-delimiters-depth-1">(</span><span class="org-operator">*</span>op.args, os.O_RDONLY<span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-indent-bars-1"> </span> <span class="org-indent-bars-2"> </span> <span class="org-keyword">return</span> op.k<span class="org-rainbow-delimiters-depth-1">(</span>fd<span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-indent-bars-1"> </span> <span class="org-keyword">elif</span> op.op <span class="org-operator">==</span> <span class="org-string">'read'</span>:
<span class="org-indent-bars-1"> </span> <span class="org-indent-bars-2"> </span> <span class="org-variable-name">text</span> <span class="org-operator">=</span> os.read<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-operator">*</span>op.args, 10000<span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-indent-bars-1"> </span> <span class="org-indent-bars-2"> </span> <span class="org-keyword">return</span> op.k<span class="org-rainbow-delimiters-depth-1">(</span>text<span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-indent-bars-1"> </span> <span class="org-keyword">elif</span> op.op <span class="org-operator">==</span> <span class="org-string">'close'</span>:
<span class="org-indent-bars-1"> </span> <span class="org-indent-bars-2"> </span> os.close<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-operator">*</span>op.args<span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-indent-bars-1"> </span> <span class="org-indent-bars-2"> </span> <span class="org-keyword">return</span> op.k<span class="org-rainbow-delimiters-depth-1">()</span>
<span class="org-keyword">while</span> <span class="org-builtin">isinstance</span><span class="org-rainbow-delimiters-depth-1">(</span>prog, Op<span class="org-rainbow-delimiters-depth-1">)</span>:
<span class="org-indent-bars-1"> </span> <span class="org-variable-name">prog</span> <span class="org-operator">=</span> runOp<span class="org-rainbow-delimiters-depth-1">(</span>prog<span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-keyword">return</span> prog
</pre>
</div>
<p>
So far so good, but what will <code>count_char_of_file</code> all of this do to
<code>count_chars_of_file</code>?
</p>
<p>
Well, it's not quite as easy to read any more (basically it's rewritten in <a href="https://en.wikipedia.org/wiki/Continuation-passing_style">CPS</a>):
</p>
<div class="org-src-container">
<pre class="src src-python"><span class="org-keyword">def</span> <span class="org-function-name">count_chars_of_file</span><span class="org-rainbow-delimiters-depth-1">(</span>fn<span class="org-rainbow-delimiters-depth-1">)</span>:
<span class="org-keyword">def</span> <span class="org-function-name">cont_1</span><span class="org-rainbow-delimiters-depth-1">(</span>text, fd<span class="org-rainbow-delimiters-depth-1">)</span>:
<span class="org-indent-bars-1"> </span> <span class="org-variable-name">n</span> <span class="org-operator">=</span> <span class="org-builtin">len</span><span class="org-rainbow-delimiters-depth-1">(</span>text<span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-indent-bars-1"> </span> <span class="org-keyword">return</span> cps_close<span class="org-rainbow-delimiters-depth-1">(</span>fd, <span class="org-keyword">lambda</span> n<span class="org-operator">=</span>n: n<span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-keyword">def</span> <span class="org-function-name">cont_0</span><span class="org-rainbow-delimiters-depth-1">(</span>fd<span class="org-rainbow-delimiters-depth-1">)</span>:
<span class="org-indent-bars-1"> </span> <span class="org-keyword">return</span> cps_read<span class="org-rainbow-delimiters-depth-1">(</span>fd, <span class="org-keyword">lambda</span> text, fd<span class="org-operator">=</span>fd: cont_1<span class="org-rainbow-delimiters-depth-2">(</span>text, fd<span class="org-rainbow-delimiters-depth-2">)</span><span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-keyword">return</span> cps_open<span class="org-rainbow-delimiters-depth-1">(</span>fn, cont_0<span class="org-rainbow-delimiters-depth-1">)</span>
</pre>
</div>
</div>
</div>
<div id="outline-container-orgcaaf8cc" class="outline-2">
<h2 id="orgcaaf8cc">Generators to the rescue</h2>
<div class="outline-text-2" id="text-orgcaaf8cc">
<p>
Python does have a notion of continuations in the form of generators.<sup><a id="fnr.1" class="footref" href="#fn.1" role="doc-backlink">1</a></sup> By
making <code>count_char_of_file</code> into a generator it's possible to remove the
explicit continuations and the program actually resembles the original one
again.
</p>
<p>
The type for the actions loses one member, and the functions creating them lose
an argument:
</p>
<div class="org-src-container">
<pre class="src src-python"><span class="org-variable-name">Op</span> <span class="org-operator">=</span> collections.namedtuple<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-string">'Op'</span>, <span class="org-rainbow-delimiters-depth-2">[</span><span class="org-string">'op'</span>, <span class="org-string">'args'</span><span class="org-rainbow-delimiters-depth-2">]</span><span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-keyword">def</span> <span class="org-function-name">gen_open</span><span class="org-rainbow-delimiters-depth-1">(</span>fn<span class="org-rainbow-delimiters-depth-1">)</span>:
<span class="org-keyword">return</span> Op<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-string">'open'</span>, <span class="org-rainbow-delimiters-depth-2">[</span>fn<span class="org-rainbow-delimiters-depth-2">]</span><span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-keyword">def</span> <span class="org-function-name">gen_read</span><span class="org-rainbow-delimiters-depth-1">(</span>fd<span class="org-rainbow-delimiters-depth-1">)</span>:
<span class="org-keyword">return</span> Op<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-string">'read'</span>, <span class="org-rainbow-delimiters-depth-2">[</span>fd<span class="org-rainbow-delimiters-depth-2">]</span><span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-keyword">def</span> <span class="org-function-name">gen_close</span><span class="org-rainbow-delimiters-depth-1">(</span>fd<span class="org-rainbow-delimiters-depth-1">)</span>:
<span class="org-keyword">return</span> Op<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-string">'close'</span>, <span class="org-rainbow-delimiters-depth-2">[</span>fd<span class="org-rainbow-delimiters-depth-2">]</span><span class="org-rainbow-delimiters-depth-1">)</span>
</pre>
</div>
<p>
The interpreter and program runner must be modified to step the generator until
its end:
</p>
<div class="org-src-container">
<pre class="src src-python"><span class="org-keyword">def</span> <span class="org-function-name">runProgram</span><span class="org-rainbow-delimiters-depth-1">(</span>prog<span class="org-rainbow-delimiters-depth-1">)</span>:
<span class="org-keyword">def</span> <span class="org-function-name">runOp</span><span class="org-rainbow-delimiters-depth-1">(</span>op<span class="org-rainbow-delimiters-depth-1">)</span>:
<span class="org-indent-bars-1"> </span> <span class="org-keyword">if</span> op.op <span class="org-operator">==</span> <span class="org-string">'open'</span>:
<span class="org-indent-bars-1"> </span> <span class="org-indent-bars-2"> </span> <span class="org-variable-name">fd</span> <span class="org-operator">=</span> os.<span class="org-builtin">open</span><span class="org-rainbow-delimiters-depth-1">(</span>op.args<span class="org-rainbow-delimiters-depth-2">[</span>0<span class="org-rainbow-delimiters-depth-2">]</span>, os.O_RDONLY<span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-indent-bars-1"> </span> <span class="org-indent-bars-2"> </span> <span class="org-keyword">return</span> fd
<span class="org-indent-bars-1"> </span> <span class="org-keyword">elif</span> op.op <span class="org-operator">==</span> <span class="org-string">'read'</span>:
<span class="org-indent-bars-1"> </span> <span class="org-indent-bars-2"> </span> <span class="org-variable-name">text</span> <span class="org-operator">=</span> os.read<span class="org-rainbow-delimiters-depth-1">(</span>op.args<span class="org-rainbow-delimiters-depth-2">[</span>0<span class="org-rainbow-delimiters-depth-2">]</span>, 10000<span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-indent-bars-1"> </span> <span class="org-indent-bars-2"> </span> <span class="org-keyword">return</span> text
<span class="org-indent-bars-1"> </span> <span class="org-keyword">elif</span> op.op <span class="org-operator">==</span> <span class="org-string">'close'</span>:
<span class="org-indent-bars-1"> </span> <span class="org-indent-bars-2"> </span> os.close<span class="org-rainbow-delimiters-depth-1">(</span>op.args<span class="org-rainbow-delimiters-depth-2">[</span>0<span class="org-rainbow-delimiters-depth-2">]</span><span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-indent-bars-1"> </span> <span class="org-indent-bars-2"> </span> <span class="org-keyword">return</span> <span class="org-constant">None</span>
<span class="org-keyword">try</span>:
<span class="org-indent-bars-1"> </span> <span class="org-variable-name">op</span> <span class="org-operator">=</span> prog.send<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-constant">None</span><span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-indent-bars-1"> </span> <span class="org-keyword">while</span> <span class="org-constant">True</span>:
<span class="org-indent-bars-1"> </span> <span class="org-indent-bars-2"> </span> <span class="org-variable-name">r</span> <span class="org-operator">=</span> runOp<span class="org-rainbow-delimiters-depth-1">(</span>op<span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-indent-bars-1"> </span> <span class="org-indent-bars-2"> </span> <span class="org-variable-name">op</span> <span class="org-operator">=</span> prog.send<span class="org-rainbow-delimiters-depth-1">(</span>r<span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-keyword">except</span> <span class="org-type">StopIteration</span> <span class="org-keyword">as</span> e:
<span class="org-indent-bars-1"> </span> <span class="org-keyword">return</span> e.value
</pre>
</div>
<p>
Finally, the generator-version of <code>count_chars_of_file</code> goes back to being a bit
more readable:
</p>
<div class="org-src-container">
<pre class="src src-python"><span class="org-keyword">def</span> <span class="org-function-name">count_chars_of_file</span><span class="org-rainbow-delimiters-depth-1">(</span>fn<span class="org-rainbow-delimiters-depth-1">)</span>:
<span class="org-variable-name">fd</span> <span class="org-operator">=</span> <span class="org-keyword">yield</span> gen_open<span class="org-rainbow-delimiters-depth-1">(</span>fn<span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-variable-name">text</span> <span class="org-operator">=</span> <span class="org-keyword">yield</span> gen_read<span class="org-rainbow-delimiters-depth-1">(</span>fd<span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-variable-name">n</span> <span class="org-operator">=</span> <span class="org-builtin">len</span><span class="org-rainbow-delimiters-depth-1">(</span>text<span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-keyword">yield</span> gen_close<span class="org-rainbow-delimiters-depth-1">(</span>fd<span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-keyword">return</span> n
</pre>
</div>
</div>
</div>
<div id="outline-container-org413d6be" class="outline-2">
<h2 id="org413d6be">Generators all the way</h2>
<div class="outline-text-2" id="text-org413d6be">
<p>
Limitations of Python generators mean that we have either have to push the
interpreter (<code>runProgram</code>) down to where <code>count_char_of_file</code> is used, or make
all intermediate layers into generators and rewrite the interpreter to deal with
this. It could look something like this then:
</p>
<div class="org-src-container">
<pre class="src src-python"><span class="org-keyword">def</span> <span class="org-function-name">runProgram</span><span class="org-rainbow-delimiters-depth-1">(</span>prog<span class="org-rainbow-delimiters-depth-1">)</span>:
<span class="org-keyword">def</span> <span class="org-function-name">runOp</span><span class="org-rainbow-delimiters-depth-1">(</span>op<span class="org-rainbow-delimiters-depth-1">)</span>:
<span class="org-indent-bars-1"> </span> <span class="org-keyword">if</span> op.op <span class="org-operator">==</span> <span class="org-string">'open'</span>:
<span class="org-indent-bars-1"> </span> <span class="org-indent-bars-2"> </span> <span class="org-variable-name">fd</span> <span class="org-operator">=</span> os.<span class="org-builtin">open</span><span class="org-rainbow-delimiters-depth-1">(</span>op.args<span class="org-rainbow-delimiters-depth-2">[</span>0<span class="org-rainbow-delimiters-depth-2">]</span>, os.O_RDONLY<span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-indent-bars-1"> </span> <span class="org-indent-bars-2"> </span> <span class="org-keyword">return</span> fd
<span class="org-indent-bars-1"> </span> <span class="org-keyword">elif</span> op.op <span class="org-operator">==</span> <span class="org-string">'read'</span>:
<span class="org-indent-bars-1"> </span> <span class="org-indent-bars-2"> </span> <span class="org-variable-name">text</span> <span class="org-operator">=</span> os.read<span class="org-rainbow-delimiters-depth-1">(</span>op.args<span class="org-rainbow-delimiters-depth-2">[</span>0<span class="org-rainbow-delimiters-depth-2">]</span>, 10000<span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-indent-bars-1"> </span> <span class="org-indent-bars-2"> </span> <span class="org-keyword">return</span> text
<span class="org-indent-bars-1"> </span> <span class="org-keyword">elif</span> op.op <span class="org-operator">==</span> <span class="org-string">'close'</span>:
<span class="org-indent-bars-1"> </span> <span class="org-indent-bars-2"> </span> os.close<span class="org-rainbow-delimiters-depth-1">(</span>op.args<span class="org-rainbow-delimiters-depth-2">[</span>0<span class="org-rainbow-delimiters-depth-2">]</span><span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-indent-bars-1"> </span> <span class="org-indent-bars-2"> </span> <span class="org-keyword">return</span> <span class="org-constant">None</span>
<span class="org-keyword">try</span>:
<span class="org-indent-bars-1"> </span> <span class="org-variable-name">op</span> <span class="org-operator">=</span> prog.send<span class="org-rainbow-delimiters-depth-1">(</span><span class="org-constant">None</span><span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-indent-bars-1"> </span> <span class="org-keyword">while</span> <span class="org-constant">True</span>:
<span class="org-indent-bars-1"> </span> <span class="org-indent-bars-2"> </span> <span class="org-keyword">if</span> <span class="org-builtin">isinstance</span><span class="org-rainbow-delimiters-depth-1">(</span>op, Op<span class="org-rainbow-delimiters-depth-1">)</span>:
<span class="org-indent-bars-1"> </span> <span class="org-indent-bars-2"> </span> <span class="org-indent-bars-3"> </span> <span class="org-variable-name">r</span> <span class="org-operator">=</span> runOp<span class="org-rainbow-delimiters-depth-1">(</span>op<span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-indent-bars-1"> </span> <span class="org-indent-bars-2"> </span> <span class="org-indent-bars-3"> </span> <span class="org-variable-name">op</span> <span class="org-operator">=</span> prog.send<span class="org-rainbow-delimiters-depth-1">(</span>r<span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-indent-bars-1"> </span> <span class="org-indent-bars-2"> </span> <span class="org-keyword">elif</span> <span class="org-builtin">isinstance</span><span class="org-rainbow-delimiters-depth-1">(</span>op, types.GeneratorType<span class="org-rainbow-delimiters-depth-1">)</span>:
<span class="org-indent-bars-1"> </span> <span class="org-indent-bars-2"> </span> <span class="org-indent-bars-3"> </span> <span class="org-variable-name">r</span> <span class="org-operator">=</span> runProgram<span class="org-rainbow-delimiters-depth-1">(</span>op<span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-indent-bars-1"> </span> <span class="org-indent-bars-2"> </span> <span class="org-indent-bars-3"> </span> <span class="org-variable-name">op</span> <span class="org-operator">=</span> prog.send<span class="org-rainbow-delimiters-depth-1">(</span>r<span class="org-rainbow-delimiters-depth-1">)</span>
<span class="org-keyword">except</span> <span class="org-type">StopIteration</span> <span class="org-keyword">as</span> e:
<span class="org-indent-bars-1"> </span> <span class="org-keyword">return</span> e.value
</pre>
</div>
</div>
</div>
<div id="outline-container-org3042296" class="outline-2">
<h2 id="org3042296">Final thoughts</h2>
<div class="outline-text-2" id="text-org3042296">
<p>
I think I've shown one way to achieve, at least parts of, what Ken talks about.
The resulting code looks almost like "normal Python". There are some things to
note:
</p>
<ol class="org-ol">
<li>Exception handling is missing. I know of no way to inject an exception into a
generator in Python so I'm guessing that exceptions from running the IO
operations would have to be passed in via <code>generator.send</code> as a normal value,
which means that exception handling code would have to look decidedly
non-Pythonic.</li>
<li>Using this approach means the language must have support for generators (or
some other way to represent <i>the rest of the program</i>). I think this rules
out Java, but probably it can be done in C#.</li>
<li>I've only used a single interpreter here, but I see no issues with combining
interpreters (to combine domains of operations like <i>file operations</i> and *
network operations*). I also think it'd be possible to use it to realize what
De Goes calls <a href="http://degoes.net/articles/modern-fp-part-2#onion-architecture">Onion Architecture</a>.</li>
</ol>
<p>
Would I ever advocate this approach for a larger Python project, or even any
project in an OO/imperative language?
</p>
<p>
I'm not sure! I think that testing using mocks and stubs has lead to a smelly
code base each an every time I've used it, but this approach feels a little too
far from how OO/imperative code usually is written. I would love to try it out
and see what the implications really are.
</p>
</div>
</div>
<div id="footnotes">
<h2 class="footnotes">Footnotes: </h2>
<div id="text-footnotes">
<div class="footdef"><sup><a id="fn.1" class="footnum" href="#fnr.1" role="doc-backlink">1</a></sup> <div class="footpara" role="doc-footnote"><p class="footpara">
I know, I know, <i>coroutines</i>! I'm simplifying and brushing over details
here, but I don't think I'm brushing over any details that are <i>important</i> for
this example.
</p></div></div>
</div>
</div><div class="taglist"><a href="https://magnus.therning.org/tags.html">Tags</a>: <a href="https://magnus.therning.org/tag-python.html">python</a> <a href="https://magnus.therning.org/tag-free_monad.html">free_monad</a> </div>
<div id="comments">Comment <a href=mailto:[email protected]?subject=Comment%20on%20INSERT%20POST%20URL%20HERE>here</a>.</div></div>
<div id="postamble" class="status"><!-- org-static-blog-page-postamble --></div>
</body>
</html>