-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
374 lines (374 loc) · 18.4 KB
/
index.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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<!-- Presentation metadata -->
<title>Abstraction for students of all the things</title>
<meta name="description" content="Become a better programmer by thinking deeply about abstraction">
<meta name="author" content="Chris Waigl">
<!-- reveal.js CSS -->
<link rel="stylesheet" href="css/reveal.css">
<!-- The id attribute must be defined for the reveal.js-menu themes panel -->
<link rel="stylesheet" href="css/theme/league.css" id="theme">
<!-- Devicons CSS -->
<link rel="stylesheet" href="css/devicons.min.css">
<!-- Font Awesome CSS -->
<link rel="stylesheet" href="css/fa-solid.css">
<link rel="stylesheet" href="css/fa-regular.css">
<link rel="stylesheet" href="css/fa-brands.css">
<link rel="stylesheet" href="css/fontawesome.css">
<!-- Custom CSS -->
<link rel="stylesheet" href="css/custom.css">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match( /print-pdf/gi ) ? 'css/print/pdf.css' : 'css/print/paper.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
</script>
<style>
.container{
display: flex;
}
.col{
flex: 1;
}
</style>
</head>
<body>
<div class="reveal">
<div class="slides">
<section>
<h2>Abstraction<br>for students of all the things</h2>
<p>PyCascades 2019</p>
<p>Chris Waigl</p>
</section>
<section>
<img src="img/2uhw12.jpg">
<p class="smallsmall">See <a href="http://hyperboleandahalf.blogspot.com/2010/06/this-is-why-ill-never-be-adult.html">
Hyperbole and a Half</a></p>
</section>
<section>
<p>I'm an earth scientist.</p>
<div class="container">
<div class="col">
<img src="img/ottercode2.jpg">
</div>
<div class="col">
<p class="smaller fragment fade-in">Dirty secret: Scientists often write mediocre or outright bad code.</p>
<img src="img/ottercode1.jpg">
</div>
</div>
</section>
<section>
<p>Alice Harpole:
<a href="https://www.software.ac.uk/blog/2017-09-12-how-write-code-scientist">
https://www.software.ac.uk/blog/2017-09-12-how-write-code-scientist</a></p>
<div class="container">
<div class="col">
<img src="img/aliceh.png">
</div>
<div class="col">
<p class="smaller"><i>"I've regularly read papers describing exciting new codes,
only to find that there are number of issues preventing me from looking
at or using the code itself. The code is often not open source, which means I can’t
download or use it. Code commonly has next to no documentation,
<span class="fragment highlight-current-green">so even if I can download
it, it's very difficult to work out how it runs</span>. There can be questionable approaches to
testing with an overreliance on replicating "standard" results, but no unit tests exist
to demonstrate that the individual parts of the code work as they should."</i></p>
</div>
</div>
<aside class="notes" data-markdown>
<textarea data-template>
I've regularly read papers describing exciting new codes, only to find that there are number of issues preventing me from looking at or using the code itself. The code is often not open source, which means I can’t download or use it. Code commonly has next to no documentation, so even if I can download it, it's very difficult to work out how it runs. There can be questionable approaches to testing with an overreliance on replicating "standard" results, but no unit tests exist to demonstrate that the individual parts of the code work as they should.
</textarea>
</aside>
</section>
<section>
<p>Bozhidar Bozhanov:
<a href="https://techblog.bozho.net/the-astonishingly-low-quality-of-scientific-code/">
https://techblog.bozho.net/the-astonishingly-low-quality-of-scientific-code/</a></p>
<div class="container">
<div class="col">
<img src="img/bozho.png">
</div>
<div class="col">
<p class="smaller"><i>"Scientists in general
can’t write good code. They
<span class="fragment highlight-current-green">write code simply to
achieve their immediate goal</span>,
and then either throw it away, or keep using it for themselves.
They [...] don’t seem to be concerned with code quality,
code coverage, API design. Not to mention scientific infrastructure, deployment on
multiple servers, managing environment. <span class="fragment highlight-current-green">
These things are rarely done properly in the
scientific community.
</span>"</i></p>
</div>
</div>
<aside class="notes" data-markdown>
<textarea data-template>
Scientists in general can’t write good code. They write code simply to achieve their immediate goal, and then either throw it away, or keep using it for themselves. They [...] don’t seem to be concerned with code quality, code coverage, API design. Not to mention scientific infrastructure, deployment on multiple servers, managing environment. These things are rarely done properly in the scientific community.
</textarea>
</aside>
</section>
<section>
<p>Neil Saunders:
<a href="https://nsaunders.wordpress.com/2014/05/14/this-is-why-code-written-by-scientists-gets-ugly/">
https://nsaunders.wordpress.com/2014/05/14/this-is-why-code-written-by-scientists-gets-ugly/</a></p>
<div class="container">
<div class="col">
<img src="img/nsaunders.png">
</div>
<div class="col">
<p><i>"One answer: we begin with exploratory data analysis
and
<span class="fragment highlight-current-green">never get around to cleaning it up.</span> "</i></p>
</div>
</div>
</section>
<section>
<h3>Remedies?</h3>
<p class="fragment fade-up">Teach scientists code organization, version control,
software engineering practices</p>
<p class="fragment fade=up">Hire dedicated scientific programmers</p>
<p class="fragment fade-in"><span class="fragement fade-in">"It's all good, scientists need to make a mess!"</span><span class="fragment fade-in"> NOT!</span></p>
<p class="fragment fade-up"><i>... ok, but ...</i></p>
<aside class="notes" data-markdown>
<textarea data-template>
* Put your speaker notes here.
</textarea>
</aside>
</section>
<section>
<h3>What about the quality of the code itself?</h3>
<p class="fragment fade-in">We can <span class="fragment highlight-green"><i>and should</i> </span> also work on writing better code.</p>
<p class="fragment fade-in">
<span class="fragment highlight-green">Thinking about code through the lens of abstraction is going to help us.</span> </p>
</section>
<section>
<h2>Abstraction and mental chunking</h2>
<p class="fragment fade-in"><i>(abstract != concrete)</i></p>
</section>
<section>
<h3>Abstraction in the Python documentation</h3>
<p class="fragment fade-in">The Python Language Reference</p>
<img class="fragment fade-in" src="img/pydoc1.png">
</section>
<section>
<p>Module documentation of <span style="font-family: monospace">urllib</span> and <span style="font-family: monospace">ssl</span></p>
<img class="fragment fade-in" src="img/pydoc2.png">
<img class="fragment fade-in" src="img/pydoc3.png">
</section>
<section>
<p>The Programming FAQ warns of "excessive abstraction" (in the "performance" section).</p>
<img class="fragment fade-in" src="img/pydoc4.png">
<p class="fragment fade-in">(Note the notion of <i>levels</i> of abstraction.)</p>
</section>
<section>
<p>Basic abstraction-related example in student code.</p>
<pre><code data-trim>
with open("data_file.json", "r") as read_file:
data = json.load(read_file)
</code></pre>
<pre class="fragment fade-in"><code data-trim>
json_string = """
{
"dataset": {
"temp": 28.4,
"species": "Betula",
"datestamp": "2019-01-03 14:55:22"
}
}
"""
data = json.loads(json_string)
</code></pre>
</section>
<section>
<pre><code data-trim>
>>> data['dataset']['temp']
28.4
>>> data['dataset']['datestamp']
'2019-01-03 14:55:22'
</code></pre>
<pre class="fragment fade-in"><code data-trim>
>>> type(data['dataset']['datestamp'])
</code></pre>
<p class="fragment fade-in">• Not a datestamp, or a datetime,
but a string. </p>
</section>
<section>
<h2>First encounters<br>with abstraction in Python</h2>
</section>
<section>
<h3>Duck typing is about abstraction</h3>
<p>Let's quote the Python Language Reference again:</p>
<blockquote>An object’s type determines the operations that the object supports</blockquote>
</section>
<section>
<p>For example, iterating through a list.</p>
<p>Step 1: A beginner who writes this...</p>
<pre><code data-trim>
mylist = [1, 5, 8, 2, 9]
for ii in range(len(mylist)):
do_something(mylist[ii])
</code></pre>
<p class="fragment fade-in" >... thinks of a list as some sort of array with an index that can be incremented.</p>
</section>
<section>
<p>Step 2: When we teach her to write code like this ... </p>
<pre><code data-trim>
mylist = [1, 5, 8, 2, 9]
for element in mylist:
do_something(element)
</code></pre>
<p class="fragment fade-in">... we teach a different mental concept of a list: a list is composed of elements that can be accessed sequentially.</p>
</section>
<section>
<p>Step 3: Then we generalize: the concept is really about iterables: </p>
<pre><code data-trim>
myiterable = ... # could be list, set, tuple, string, dictionary ....
for element in myiterable:
do_something(element)
</code></pre>
<p class="fragment fade-in">Note: The consistency of Python becomes evident. Compare with MATLAB:</p>
<pre class="fragment fade-in"><code data-trim>
v = [1 5 8 17]
for element = v
do_something(element)
end
</code></pre>
</section>
<section>
<p>Another example in Python: file-like objects.</p>
<p>Using the <span style="font-family: monospace">io</span> module
(<span style="font-family: monospace">StringIO</span> and <span style="font-family: monospace">BytesIO</span>
we can treat strings as streams the same way as we do files: We can open and close
them, read from them with buffer, write to them....</p>
<p class="fragment fade-in">(Sockets and serial interfaces work very much the same way.)</p>
</section>
<section>
<h2>Writing your own abstractions</h2>
</section>
<section>
<h3>... by writing functions:</h3>
<ul>
<li class="fragment fade-in">How do you segment the processing flow into functions? (... and modules, packages...)</li>
<li class="fragment fade-in">This is an expression of how you conceptualize your task</li>
<li class="fragment fade-in">You can: Re-use. Pass around data. Hide implementation choices behind interfaces.</li>
</ul>
<p class="fragment fade-in"><i>Abstraction for problem-solving </i></p>
</section>
<section>
<h3>... by choosing your data representation:</h3>
<ul>
<li class="fragment fade-in">What comes first, the data or the functions that handle the data? </li>
<li class="fragment fade-in">The abstractions that represent data and the abstractions that represent programming steps will be developed iteratively, hand-in-hand.</li>
<li class="fragment fade-in">Data representation means to decide what the objects <i>are</i> that your code is manipulating</li>
</ul>
<p class="fragment fade-in"><i>Abstraction for reasoning about the overall shape of your problem</i> </p>
</section>
<section>
<h3>Tying it together </h3>
<ul>
<li class="fragment fade-in">The transition to object-oriented design becomes natural</li>
<li class="fragment fade-in">Many scientific Python users never write their own objects. But it becomes easy once we have clarified what our objects are, how they behave, and what operations are performed on them. </li>
</ul>
<p class="fragment fade-in">(And we have reached the point where we can talk about abstraction and encapsulation.) </p>
</section>
<section>
<h2>Abstraction pitfalls</h2>
</section>
<section>
<h3>Abstractions leak.<br>Python abstractions leak quite a bit.</h3>
<ul>
<li class="fragment fade-in">Error messages and exceptions may refer to underlying code that is at a deeper abstraction level </li>
<li class="fragment fade-in">Objects are not watertight: <span style="font-family: monospace">myobject.__dict__</span> allows access to attributes under the hood.</li>
</ul>
</section>
<section>
<h3>Some Python gotchas are related to abstraction</h3>
<p>The mutable default function argument gotcha.</p>
</section>
<section>
<h3>Remember the warning about performance<br>from the Programming FAQ?</h3>
<ul>
<li class="fragment fade-in">"Abstraction, simplicity, performance: choose two"</li>
<li class="fragment fade-in">Scientific Python libraries to the rescue: They offer a wide choice of abstractions that are optimised for performance or expressiveness.</li>
</ul> </section>
<section>
<div class="container">
<div class="col">
<img src="img/jack.jpg">
</div>
<div class="col">
<img src="img/lara.jpg">
</div>
</div>
<p style="font-size: 75%!important">
<a href="https://github.com/chryss/abstraction-for-students-of-all-the-things">
<i class="fab fa-firefox"></i>https://github.com/chryss/abstraction-for-students-of-all-the-things
</a>
<br>
<a href="https://github.com/chryss">
<i class="fab fa-github"></i>chryss
</a>
<a href="https://twitter.com/chrys">
<i class="fab fa-twitter"></i>@chrys
</a>
</p>
<p style="font-size: 75%!important">
<a href="mailto:chris@threeowlsdata.com">
<i class="fas fa-envelope"></i>chris@threeowlsdata.com
</a>
</p>
<p>
<a rel="license" href="http://creativecommons.org/licenses/by/4.0/">
<i class="fab fa-creative-commons"></i>
<i class="fab fa-creative-commons-by"></i>
</a>
</p>
</section>
<section>
<h3>Create your other slides here</h3>
<aside class="notes" data-markdown>
<textarea data-template>
* Put your speaker notes here.
</textarea>
</aside>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.js"></script>
<script>
// More info about config & dependencies:
// - https://github.com/hakimel/reveal.js#configuration
// - https://github.com/hakimel/reveal.js#dependencies
Reveal.initialize({
history: true,
dependencies: [
{ src: 'plugin/markdown/marked.js' },
{ src: 'plugin/markdown/markdown.js' },
{ src: 'plugin/notes/notes.js', async: true },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
// Zoom in and out with Alt+click
{ src: 'plugin/zoom-js/zoom.js', async: true },
{ src: 'third_party_plugins/menu.js' }
],
menu: {
themes: true,
transitions: true,
openButton: false,
loadIcons: false
}
});
</script>
</body>
</html>