-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
548 lines (500 loc) · 20.6 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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>PDB PDX</title>
<meta name="description" content="A framework for easily creating beautiful presentations using HTML">
<meta name="author" content="Hakim El Hattab">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black-translucent" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.2.0/css/font-awesome.min.css" rel="stylesheet">
<link rel="stylesheet" href="css/reveal.min.css">
<link rel="stylesheet" href="css/theme/default.css" id="theme">
<!-- For syntax highlighting -->
<link rel="stylesheet" href="lib/css/zenburn.css">
<link rel="stylesheet" href="css/fontawesomefix.css">
<!-- If the query includes 'print-pdf', include the PDF print sheet -->
<script>
if( window.location.search.match( /print-pdf/gi ) ) {
var link = document.createElement( 'link' );
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = 'css/print/pdf.css';
document.getElementsByTagName( 'head' )[0].appendChild( link );
}
</script>
<!--[if lt IE 9]>
<script src="lib/js/html5shiv.js"></script>
<![endif]-->
</head>
<body>
<div class="reveal">
<!-- Any section element inside of this container is displayed as a slide -->
<div class="slides">
<section>
<section>
<h1>PDB PDX <i class="fa fa-bug fa-spin"></i></h1>
<h3>debugging for humans</h3>
<p>
<small>Created by Aleck Landgraf and friends / <a href="http://twitter.com/aleck_landgraf"><i class="fa fa-twitter"> </i></a>@aleck_landgraf / <a href="http://aleck.me/pdbpdx/"><i class="fa fa-slideshare"></i> aleck.me/pdbpdx</a></small>
</p>
</section>
<section>
<h1>about me</h1>
<ul>
<li>VP Software Engineering at Building Energy</li>
<li>startups for the last 4+ years</li>
<li>mostly developing with python, Django, and JS</li>
<li>ping ping advocate</li>
</ul>
</section>
</section>
<section>
<h2>Why debugging? <span class="fragment">WTF?</span></h2>
<pre class="python"><code class="py" data-trim >
for j in js:
mn = j.get('model','unknown')
js2[mn]=js2.get(mn,[])+[j]
</code></pre>
</section>
<section>
<h2>steps to debugging</h2>
<ol>
<li>linter</li>
<li>print debug info</li>
<li>read the traceback</li>
<li>jump into the shell</li>
<li>debugger</li>
<li class="fragment">add tests</li>
</ol>
</section>
<section>
<section>
<h2>linter</h2>
<img width="550" height="auto" src="images/lint.png" alt="lint">
<p>your first line of defense</p>
</section>
<section>
<h2>here's what mine looks like</h2>
<img width="900" height="auto" src="images/linter.png" alt="linter">
</section>
</section>
<section>
<section>
<h2>python debug 101</h2>
<h3>print!</h3>
<pre class="python fragment"><code class="py" data-trim >
for j in js:
mn = j.get('model','unknown')
js2[mn]=js2.get(mn,[])+[j]
</code></pre>
<pre class="python fragment"><code class="py" data-trim >
for j in js:
mn = j.get('model','unknown')
js2[mn]=js2.get(mn,[])+[j]
print 'WTF', js2.get(mn,[])+[j]
</code></pre>
<p class="fragment">better yet</p>
<pre class="python fragment"><code class="py" data-trim >
for j in js:
mn = j.get('model','unknown')
js2[mn]=js2.get(mn,[])+[j]
print '\n\nline 72 of time_utils.py WTF', js2.get(mn,[])+[j], '\n\n'
</code></pre>
</section>
<section>
<h4>and of course, you might want to use logger</h4>
<pre class="python"><code class="py" data-trim >
import logging
logger = logging.getLogger(__name__)
logging.basicConfig(format='%(filename)s:%(lineno)s - %(message)s')
for j in js:
mn = j.get('model','unknown')
js2[mn]=js2.get(mn,[])+[j]
logger.debug(js2.get(mn,[])+[j])
</code></pre>
</section>
<section>
<h3>what to print?</h3>
<aside class="notes">
<ul>
<li>type(obj)</li>
<li>len(obj)</li>
<li>obj.__dict__</li>
<li>iteration counts</li>
<li>code paths hit</li>
</ul>
</aside>
</section>
</section>
<section>
<section>
<h2>tracebacks</h2>
<p>get comfortable reading these</p>
<pre class="shell"><code class="shell" data-trim >
File "django/core/handlers/base.py", line 112, in get_response
response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "seed/utils/api.py", line 91, in _wrapped
return fn(request, *args, **kwargs)
File "annoying/decorators.py", line 164, in wrapper
response = func(request, *args, **kwargs)
File "superperms/orgs/decorators.py", line 197, in _wrapped
return fn(request, *args, **kwargs)
File "django/contrib/auth/decorators.py", line 22, in _wrapped_view
return view_func(request, *args, **kwargs)
File "ecm/views.py", line 150, in update_ecm
ecm.mapped_data.update(ecm_data.get('mapped_data'), {})
TypeError: update expected at most 1 arguments, got 2
</code></pre>
</section>
<section>
<h2>use an error logging service</h2>
<p>like getsentry.com</p>
<img src="images/traceback.png" height="600" alt="traceback">
</section>
<section>
<p>or newrelic.com</p>
<img src="images/newrelic.png" height="600" alt="traceback">
</section>
</section>
<section>
<section>
<h2>entering the shell</h2>
</section>
<section>
<p>
some regular python Kung Fu
</p>
<ul>
<li>_</li>
<li>obj.__dict__</li>
<li>dir(obj)</li>
</ul>
<p class="fragment" data-markdown>
```pycon
In [1]: requests.get('http://api.openweathermap.org/data/2.5/weather?id=5746545')
Out[1]: Response [200]
In [2]: response = _
```
</p>
</section>
<section>
<h3>dir and __dict__</h3>
<p data-markdown>
```pycon
In [1]: class Greeter(object):
def __init__(self, name='Aleck'):
super(Greeter, self).__init__()
self.name = name
def greet(self):
"""prints hello ``self.name``"""
print 'hello {}'.format(self.name)
In [2]: d = Greeter()
```
</p>
<p class="fragment">dir</p>
<p data-markdown class="fragment">
```pycon
In [3]: dir(d)
Out[3]:
['__class__', '__delattr__', '__dict__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', 'greet', 'name']
```
</p>
<p class="fragment">__dict__</p>
<p data-markdown class="fragment">
```pycon
In [4]: d.__dict__
Out[4]: {'name': 'Aleck'}
```
</p>
</section>
<section>
<h3>ipython</h3>
<p>with <strong>ipython's</strong> rich feature set</p>
<ul>
<li>tab completion</li>
<li>? and ??</li>
<li>%history</li>
<li>... so much more</li>
</ul>
<aside class="notes">
<ul>
<li>paste and cpaste</li>
<li>run</li>
<li>time</li>
<li>auto indent</li>
<li>pastbin</li>
<li>ipython notebook</li>
</ul>
</aside>
</section>
<section>
<h3>tab completion</h3>
<p data-markdown>
```pycon
In [1]: import time
In [2]: time.
```
</p>
<p class="fragment">TAB</p>
<p data-markdown class="fragment">
```pycon
In [2]: time.
time.accept2dyear time.clock time.gmtime time.sleep time.struct_time time.tzname
time.altzone time.ctime time.localtime time.strftime time.time time.tzset
time.asctime time.daylight time.mktime time.strptime time.timezone
```
</p>
</section>
<section>
<h3>? and ??</h3>
<p data-markdown>
```pycon
In [1]: class Greeter(object):
...
def greet(self):
"""prints hello ``self.name``"""
print 'hello {}'.format(self.name)
In [2]: d = Greeter()
```
</p>
<p class="fragment">?</p>
<p data-markdown class="fragment">
```pycon
In [3]: d.greet?
Type: instancemethod
String form: <bound method Greeter.greet of <__main__.Greeter object at 0x10396bd50>>
File: /code/pdbpdx/<ipython-input-13-2d3119123697>
Definition: d.greet(self)
Docstring: prints hello ``self.name``
```
</p>
<p class="fragment">??</p>
<p data-markdown class="fragment">
```pycon
In [4]: d.greet??
Type: instancemethod
...
Source:
def greet(self):
"""prints hello ``self.name``"""
print 'hello {}'.format(self.name)
```
</p>
</section>
</section>
<section>
<section>
<h2>picking a debugger</h2>
</section>
<section>
<h2>pdb</h2>
<blockquote>
“batteries included”
</blockquote>
<p data-markdown>
```python
from pdb import set_trace
set_trace()
```
</p>
</section>
<section>
<h2>ipdb</h2>
<p>all the wonder of ipython plus some neat tricks:</p>
<ul>
<li>pip install ipdb</li>
<li data-markdown>90% use case: <p>
```python
from ipdb import set_trace
set_trace()
```
</p></li>
<li data-markdown>`with launch_ipdb_on_exception():`</li>
<li>ipython --pdb main.py</li>
</ul>
</section>
<section>
<h2>pudb</h2>
<img src="images/pudb.png" height="600" alt="traceback">
<aside class="notes">
<ul>
<li>adds context</li>
<li>view local variables: type, repr</li>
<li>create watches</li>
<li>jump into ipython shell (and create vars)</li>
<li>enter exceptions!</li>
<li>jump through stack</li>
<li>view return before returning</li>
</ul>
</aside>
</section>
</section>
<section>
<h3>peanut butter demo time</h3>
<img src="images/Peanut_butter_jelly_time_by_ggrock70-d4fixty.png" width="auto" height="500">
<aside class="notes">
two things to demo: linter, logging, and pudb
</aside>
</section>
<section>
<section>
<h3>common debugging patterns</h3>
<aside class="notes">
Errors over long iterations,
Access local vars with pdb namespace collisions,
Profile,
Linter
</aside>
</section>
<section>
<h3>handle errors over long iterations</h3>
<p data-markdown>
```python
for u in users:
u.set_home_location()
```
</p>
<p class="fragment">BLAMO!!! OK, try this...</p>
<p data-markdown class="fragment">
```python
for u in users:
try:
u.set_home_location()
except KeyError:
import pudb; pudb.set_trace()
```
</p>
<p class="fragment">Can't run in the shell. OK, try this...</p>
<p data-markdown class="fragment">
```python
for u in users:
try:
u.set_home_location()
except KeyError:
from traceback import print_exc
print_exc() # or log
```
</p>
</section>
<section>
<h3>What to do with pdb namespace collisions?</h3>
<p data-markdown>
```python
n = len(cols)
```
<p data-markdown class="fragment">
```python
# assuming ipython shell in pudb
my_n = locals()['n']
# or use !
!n
```
</p>
</section>
<section>
<h3>slow code? profile it</h3>
<p>count and time things!</p>
<aside class="notes">
yunomi (mean time and # of, memory_profiler, DB counts
</aside>
</section>
<section>
<h3>reduce your footprint</h3>
<ul>
<li>map vs. imap</li>
<li>items vs. iteritems</li>
<li>stream files and chuck large DB queries</li>
</ul>
</section>
</section>
<section>
<section>
<h2>steps to prevent debugging</h2>
<ol>
<li>you have a <strong>linter,</strong> right?</li>
<li>access to all errors and tracebacks?</li>
<li><strong>flake8</strong> should pass for tests to pass</li>
<li>draw a line in the sand: enforce code <strong>coverage</strong></li>
<li>add tests to prevent regressions</li>
</ol>
</section>
<section>
<h2>a quick note on coverage</h2>
<img class="fragment" width="500" height="auto" src="images/coverage.png" alt="lint">
</section>
<section>
<h3>How much is enough?</h3>
<p data-markdown >
```console
Name Stmts Miss Cover Missing
-------------------------------------------
espm 8 0 100%
espm.espm 97 3 97% 109-112
espm.models 276 0 100%
espm.util 19 0 100%
-------------------------------------------
TOTAL 400 3 99%
----------------------------------------------------------------------
Ran 32 tests in 0.255s
```
</p>
<p data-markdown>
```console
python setup.py nosetests --cover-min-percentage=100
```
</p>
</section>
</section>
<section>
<h2>Stellar Resources</h2>
<ul>
<li><a href="https://buildingenergy.com/careers" target="_blank">your friends</a></li>
<li><a href="http://stackoverflow.com/" target="_blank">Stack Overflow</a></li>
<li><a href="https://pypi.python.org/pypi/ipython" target="_blank">ipython</a></li>
<li><a href="https://pypi.python.org/pypi/pudb" target="_blank">pudb</a></li>
<li><a href="https://pypi.python.org/pypi/nose-pudb" target="_blank">nose + pudb</a></li>
<li><a href="https://getsentry.com/welcome/" target="_blank">sentry</a></li>
<li><a href="https://newrelic.com/" target="_blank">newrelic</a></li>
<li><a href="https://pypi.python.org/pypi/memory_profiler/" target="_blank">python memory profiler</a></li>
<li><a href="https://github.com/dreid/yunomi" target="_blank">y u no measure it</a></li>
<li><a href="https://pycallgraph.readthedocs.org/en/master/" target="_blank">pycallgraph</a></li>
<li><a href="https://github.com/hobson/pug/blob/dj1.7/pug/debug.py" target="_blank">ipdb post_mortem debug</a></li>
</ul>
</section>
<section>
<h1>THE END</h1>
<h3><i class="fa fa-comments"></i> comments</h3>
<p><small>aleck.landgraf@gmail.com </small></p>
</section>
</div>
</div>
<script src="lib/js/head.min.js"></script>
<script src="js/reveal.min.js"></script>
<script>
// Full list of configuration options available here:
// https://github.com/hakimel/reveal.js#configuration
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
theme: Reveal.getQueryHash().theme, // available themes are in /css/theme
transition: Reveal.getQueryHash().transition || 'default', // default/cube/page/concave/zoom/linear/fade/none
// Parallax scrolling
// parallaxBackgroundImage: 'https://s3.amazonaws.com/hakim-static/reveal-js/reveal-parallax-1.jpg',
// parallaxBackgroundSize: '2100px 900px',
// Optional libraries used to extend on reveal.js
dependencies: [
{ src: 'lib/js/classList.js', condition: function() { return !document.body.classList; } },
{ src: 'plugin/markdown/marked.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/markdown/markdown.js', condition: function() { return !!document.querySelector( '[data-markdown]' ); } },
{ src: 'plugin/highlight/highlight.js', async: true, callback: function() { hljs.initHighlightingOnLoad(); } },
{ src: 'plugin/zoom-js/zoom.js', async: true, condition: function() { return !!document.body.classList; } },
{ src: 'plugin/notes/notes.js', async: true, condition: function() { return !!document.body.classList; } }
]
});
</script>
</body>
</html>