Repository navigation
Expand file tree
/
Copy pathpresentation-week08.tex~
More file actions
1200 lines (852 loc) · 22.8 KB
/
Copy pathpresentation-week08.tex~
File metadata and controls
1200 lines (852 loc) · 22.8 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
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
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
\documentclass{beamer}
%\usepackage[latin1]{inputenc}
\usetheme{Warsaw}
\title[Intro to Python: Week 8]{Introduction to Python\\
Decorators -- Debugging -- Packages and Packaging}
\author{Christopher Barker}
\institute{UW Continuing Education / Isilon}
\date{August 22, 2012}
\usepackage{listings}
\usepackage{hyperref}
\begin{document}
% ---------------------------------------------
\begin{frame}
\titlepage
\end{frame}
% ---------------------------------------------
\begin{frame}
\frametitle{Table of Contents}
%\tableofcontents[currentsection]
\tableofcontents
\end{frame}
\section{Review/Questions}
% ---------------------------------------------
\begin{frame}[fragile]{Review of Previous Class}
{\Large Lightning talk today: Peter}
\begin{itemize}
\item Some more OO
\begin{itemize}
\item Multiple inheritance / mix-ins
\item Properties
\item \verb|staticmethod| and \verb|classmethod|
\item Special methods (``dunder'')
\end{itemize}
\item Iterators
\item Generators
\end{itemize}
\end{frame}
% header
% ---------------------------------------------
\begin{frame}[fragile]{Homework review}
{\Large Who added some classes to some ``real'' code?}
{\Large
\begin{itemize}
\item Multiple inheritance / mix-ins ?
\item Property ?
\item \verb|staticmethod| or \verb|classmethod| ?
\item Special methods ?
\item Iterator or Generators ?
\end{itemize}
}
\end{frame}
\section{Decorators}
% ---------------------------------------------
\begin{frame}[fragile]{Decorators}
{\LARGE Decorators are wrappers around functions}
\vfill
{\LARGE They let you add code before and after the execution of a function}
\vfill
{\LARGE Creating a custom version of that function}
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{Decorators}
{\LARGE Syntax:}
\vfill
\begin{verbatim}
@logged
def add(a, b):
"""add() adds things"""
return a + b
\end{verbatim}
\vfill
{\Large Demo and Motivation: \verb|basicmath.py| }
\vfill
PEP: \url{http://www.python.org/dev/peps/pep-0318/}
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{Decorators}
{\LARGE \verb|@| decorator operator is an abbreviation:}
\vfill
\begin{verbatim}
@f
def g:
pass
\end{verbatim}
\vfill
same as
\vfill
\begin{verbatim}
def g:
pass
g = f(g)
\end{verbatim}
\vfill
{\Large ``Syntactic Sugar'' -- but really quite nice}
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{Decorators}
{\LARGE demo:
\vfill
\begin{verbatim}
decorator.py
\end{verbatim}
}
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{Decorator examples}
{\LARGE Examples from the stdlib:}
\vfill
{\Large Does this structure:}
\vfill
\begin{verbatim}
def g:
pass
g = f(g)
\end{verbatim}
\vfill
{\Large look familiar from last class?}
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{Decorator examples}
{\LARGE \verb|staticmethod()|}
\vfill
\begin{verbatim}
class C(object):
def add(a, b):
return a + b
add = staticmethod(add)
\end{verbatim}
\vfill
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{Decorator examples}
{\LARGE \verb|staticmethod()|}
\vfill
{\Large Decorator form:}
\begin{verbatim}
class C(object):
@staticmethod
def add(a, b):
return a + b
\end{verbatim}
\vfill
{\LARGE ( and \verb|classmethod| )}
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{examples}
{\LARGE \verb|property()|}
\vfill
\begin{verbatim}
class C(object):
def __init__(self):
self._x = None
def getx(self):
return self._x
def setx(self, value):
self._x = value
def delx(self):
del self._x
x = property(getx, setx, delx,
"I'm the 'x' property.")
\end{verbatim}
\vfill
becomes...
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{Decorator examples}
\begin{verbatim}
class C(object):
def __init__(self):
self._x = None
@property
def x(self):
return self._x
@x.setter
def x(self, value):
self._x = value
@x.deleter
def x(self):
del self._x
\end{verbatim}
\vfill
Puts the info close to where it is used
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{examples}
{\LARGE CherryPy}
\vfill
\begin{verbatim}
import cherrypy
class HelloWorld(object):
@cherrypy.expose
def index(self):
return "Hello World!"
cherrypy.quickstart(HelloWorld())
\end{verbatim}
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{examples}
{\LARGE Pyramid}
\vfill
\begin{verbatim}
@template
def A_view_function(request)
.....
@json
def A_view_function(request)
......
\end{verbatim}
so you don't need to think about what your view is returning...
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{decorators...}
{\Large For this class:}
\vfill
{\Large Mostly want to you to know how to use decorators that someone else has written}
\vfill
{\Large Have a basic idea what they do when you do use them}
\end{frame}
%-------------------------------
\begin{frame}[fragile]{LAB}
\begin{itemize}
\item Re-write the properties from last week's \verb|Circle| class to use the
decorator syntax (see a couple slides back for an example)\\
(\verb|circle.py| and \verb|test_circle.py|)
\vspace{0.5in}
\item Write a decorator that can be used to wrap any function that returns a
string in a \verb|<p>| element from the html builder from the previous
couple classes (the \verb|P Element| subclass).\\
(\verb|html_gen.py|)
\end{itemize}
\end{frame}
% Lightning Talk Slide
%-------------------------------
\begin{frame}{Lightning Talk}
{\centering
\vfill
{\LARGE Lightning Talk: }
\vfill
{\Huge Peter}
\vfill
}
\end{frame}
\section{Debugging}
% ---------------------------------------------
\begin{frame}[fragile]{Debugging}
{\LARGE Debugging}
\vfill
{\Large Debugging is a methodical process of finding and reducing the number
of bugs, or defects, in a computer program}
\vfill
{\Large We often spend more time debugging that we do writing the code in the
first place}
\end{frame}
\begin{frame}[fragile]{Debugging}
{\LARGE Core Message:}
\vfill
{\Large Well structured code is less prone to bugs}
\vfill
{\Large Well structured code is easier to debug}
\end{frame}
\begin{frame}[fragile]{Types of Bugs}
{\LARGE
\begin{itemize}
\item Syntax Errors
\item Run Time Errors
\item Logic Errors
\end{itemize}
}
\vfill
(Usually show up in that order)
\end{frame}
\begin{frame}[fragile]{Syntax Errors}
{\LARGE Common Causes}
\begin{itemize}
\item Mismatched parenthesis, quotes, brackets, etc...
\item Missing colons
\item ``='' vs ``==''
\item Indentation
\item Using a keyword for a variable name
\end{itemize}
\vfill
Hint: Make sure you are editing the same file you are running!
\end{frame}
\begin{frame}[fragile]{Runtime Errors}
{\LARGE This may seem obvious, but...}
\vfill
{\Large Read the traceback carefully!}
\begin{itemize}
\item What type of error?
\begin{itemize}
\item ValueError
\item TypeError
\item NameError
\item Think about what that type of error means
\end{itemize}
\item What module/function did it occur in?
\item What line did it occur?
\item Where was that function called from?
\end{itemize}
\end{frame}
\begin{frame}[fragile]{Logic Errors}
{\LARGE No hints from the interpreter}
\vfill
\begin{itemize}
\item Make sure the code you think is executing is really executing
\item Simplify your code
\item Boil it down to the simplest version that shows the bug
\begin{itemize}
\item Often you'll find it in the process
\end{itemize}
\item Save (and print) intermediate results from long expressions
\item Try out bits of code at the command line (or iPython)
\end{itemize}
\end{frame}
\begin{frame}[fragile]{Debugging Tools}
{\LARGE
\vfill
Print statements
\vfill
Interactive debuggers
\vfill
Logging
\vfill
Tests
}
\end{frame}
\begin{frame}[fragile]{Print Statements}
{\LARGE
\vfill
Simple
\vfill
Easy to understand
\vfill
Quick (with no compile cycle)
\vfill
Nice if something fails the 1000th time through a loop...
}
\vfill
( I do most of my debugging with print statements )
\end{frame}
\begin{frame}[fragile]{Logging}
{\LARGE ``enterprise level print statements''}
\vfill
Standard library logging module
\vfill
Powerful, awesome, and a bit annoying
\vfill
\url{http://docs.python.org/library/logging.html}
\vspace{0.25in}
\url{http://docs.python.org/howto/logging.html#logging-basic-tutorial}
\end{frame}
\begin{frame}[fragile]{Logging Module}
{\LARGE Using the standard logging module means you can share your logging with third
party packages, etc.}
\vfill
\begin{itemize}
\item Customized levels
\item String interpolation
\item On the fly configuration
\item etc, etc..
\end{itemize}
\end{frame}
\begin{frame}[fragile]{Logging Module}
{\LARGE Output options:}
\vfill
\begin{itemize}
\item StreamHandler
\item FileHandler
\item BaseRotatingHandler
\item RotatingFileHandler
\item TimedRotatingFileHandler
\item SocketHandler
\item SMTPHandler
\item SysLogHandler
\item HTTPHandler
\item NullHandler
\item ...
\end{itemize}
\end{frame}
\begin{frame}[fragile]{Tests}
{\LARGE Test Suites Find Bugs}
\vfill
{\LARGE And keep them from recurring}
\vfill
{\LARGE You can get closer to the bug by writing more tests}
\end{frame}
\begin{frame}[fragile]{Tests}
{\LARGE Test Suites are particularly helpful for Heisenbugs:}
\vfill
heisenbug: /hi-zen-buhg/, n.\\[0.1in]
A bug that disappears or alters its behavior when one attempts to probe or isolate it.
\vfill
\url{http://www.catb.org/jargon/html/H/heisenbug.html}
\vfill
{\Large More on testing next class}
\end{frame}
\begin{frame}[fragile]{Interactive Debuggers}
{\LARGE PDB}
\vfill
{\Large
\begin{itemize}
\item in stdlib
\item command line
\item local
\item in process
\end{itemize}
}
\end{frame}
%--------------------------------
\begin{frame}[fragile]{PDB}
( I've never used it much -- but ...)
\vfill
{\Large Python Debugging Techniques}
\url{http://aymanh.com/python-debugging-techniques}
\vfill
{\Large Use pdb to debug Django (screencast):}
\url{http://ericholscher.com/blog/2008/aug/31/using-pdb-python-debugger-django-debugging-series-/}
\end{frame}
%--------------------------------
\begin{frame}[fragile]{Visual Debuggers}
\vfill
{\Large Visual debuggers in many IDEs:}
\begin{itemize}
\item WingIDE: \url{http://wingware.com/wingide/debugger}
\item PyDEV / Eclipse \url{http://pydev.org/}
\item Spyder \url{http://code.google.com/p/spyderlib/}
\item PyCharm: \url{http://www.jetbrains.com/pycharm/}
\item .....
\end{itemize}
\end{frame}
%-------------------------------
\begin{frame}{LAB}
{\LARGE PDB lab}
\vfill
{\Large Follow this tutorial:}
\vfill
{\Large Getting started with pdb:}
\url{http://pythonconquerstheuniverse.wordpress.com/2009/09/10/debugging-in-python/}\\
\vfill
{\Large Try it on your own code -- or class code}
\vfill
\end{frame}
\section{Packages and Packaging}
% ---------------------------------------------
\begin{frame}[fragile]{Modules and Packages}
\vfill
{\Large A module is a file with python code in it}
\vfill
{\Large A package is a directory with an \verb|__init__.py| file in it}
\vfill
{\Large And usually other modules, packages, etc...}
\begin{verbatim}
my_package
__init__.py
module_a.py
module_b.py
\end{verbatim}
\begin{verbatim}
import my_package
\end{verbatim}
runs \verb|my_package/__init__.py|
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{Modules and Packages}
\vfill
\begin{verbatim}
import sys
for p in sys.path:
print p
\end{verbatim}
\vfill
(demo)
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{Installing Python}
{\Large Linux:}
Usually part of the system -- just use it
\vfill
{\Large Windows:}
\vfill
Use the \url{python.org} version:
\vfill
System Wide
\vfill
Can install multiple versions if need be
\vfill
Third party binaries for it.
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{Installing Python}
{\Large OS-X:}
Comes with the system, but:
\begin{itemize}
\item Apple has never upgraded within a release
\item There are non-open source components
\item Third party packages may or may not support it
\item Apple does use it -- so don't mess with it.
\item I usually recommend the \url{python.org} version
\end{itemize}
(Also Macports, Fink, Home Brew...)
\vfill
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{Installing Packages}
{\Large Every Python installation has its own stdlib and \verb|site-packages| folder}
\vfill
{\Large\verb|site-packages| is the default place for third-party packages}
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{Finding Packages}
{\Large The Python Package Index:}
\vfill
{\LARGE PyPi}
\vfill
\url{http://pypi.python.org/pypi}
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{Installing Packages}
{\Large From source}
(\verb|setup.py install|)
\vfill
{\Large With the system installer (apt-get, yum, etc...)}
\vfill
{\Large From binaries: }
\vfill
{\Large Windows:} MSI installers
\vfill
{\Large OS-X:} dmg installers
(make sure to get compatible packages)
\vfill
{\Large \verb|easy_install| and \verb|pip|}
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{Installing Packages}
{\Large In the beginning, there was the \verb|distutils|:}
\url{....}
{\Large But \verb|distutils| is missing some key features:}
\begin{itemize}
\item package versioning
\item package discovery
\item auto-install
\end{itemize}
\vfill
{\Large And then came \verb|PyPi|}
\vfill
{\Large And then came \verb|setuptools|}
\vfill
{\Large But that wasn't well maintained...}
\vfill
{\Large So now there is \verb|distribute/pip|}
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{Installing Packages}
\vfill
{\LARGE Actually, it's a bit of a mess}
\vfill
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{Packaging Time line}
{\centering
\includegraphics[width=4.5in]{PackagingTimeline.pdf}
}
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{Packaging Tools}
{\centering
\includegraphics[width=4.5in]{packaging1.pdf}
}
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{Current State of Packaging}
{\centering
\includegraphics[width=4.5in]{PackagingCurrentState.pdf}
}
\vfill
\url{http://guide.python-distribute.org/introduction.html}
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{Compiled Packages}
{\LARGE Biggest issue is with compiled extensions\\[0.1in]
\hfill(C/C++, etc)\hfill
}
\vfill
{\Large -- You need the right compiler set up}
\vfill
{\LARGE Dependencies}
\vfill
{\Large -- Here's were it gets really ugly}
\vfill
{\Large -- Particularly on Windows}
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{Compiled Packages}
{\LARGE Linux}
\vfill
{\Large Pretty straightforward:}
\vfill
{\Large 1) Is there a system package \\[0.1in] (rpm, deb, apt-get, etc...)?
}
\vfill
{\Large 2) Install the dependencies, build from source:\\[0.1in]
\verb python setup.py build ; python setup.py install
}
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{Compiled Packages}
{\LARGE Windows}
\vfill
{\Large Sometimes simpler:}
\vfill
{\Large 1) A lot of packages have Windows binaries:\\[0.1in]
- Usually for \url{python.org} builds \\[0.1in]
- Make sure you get 32 or 64 bit correct
}
\vfill
{\Large 2) But if no binaries: \\[0.1in]
- Hope the dependencies are available!\\[0.1in]
- Set up the compiler (MS "Express" version usually works)
}
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{Compiled Packages}
{\LARGE OS-X}
\vfill
{\Large Lots of Python versions:\\[0.1in]
- Apple's built-in (different for each version of OS)\\[0.1in]
- \url{python.org} builds.\\
\hspace{0.5in}- 32 bit PPC+Intel\\
\hspace{0.5in}- 32+64 bit Intel\\[0.1in]
- Macports
- Homebrew
}
\vfill
{\Large Binary Installers (dmg or egg) have to match python version}
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{Compiled Packages}
{\LARGE OS-X}
\vfill
{\Large If you have to build it yourself:}
\vfill
{\Large Xcode compiler (the right version:\\[0.1in]
- Version 3.* for 32 bit PPC+Intel\\[0.1in]
- Version 4.* for 32+64 bit Intel\\
}
\vfill
{\Large If extra dependencies:\\[0.1in]
- macports or home brew often easiest way to build them
}
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{Final Recommendation}
{\Large First try: \verb|pip install|}
\vfill
{\Large If that doesn't work:}
\vfill
{\Large Read the docs of the package you want to install}
\vfill
{\Large Do what they say}
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{virtualenv}
{\Large \verb|virtualenv| is a tool to create isolated Python environments.}
\vfill
{\Large Very useful for developing multiple apps}
\vfill
{\Large Or deploying more than one on one system}
\vfill
\url{http://www.virtualenv.org/en/latest/index.html}
\end{frame}
\begin{frame}[fragile]{The Future}
{\Large keep an eye on}
\vfill
{\Large distutils2 / packaging / pysetup }
\vfill
{\large (They are trying to solve the problem!) }
\vfill
(\url{http://packages.python.org/Distutils2/})
\end{frame}
\section{Distributing}
\begin{frame}[fragile]{Distributing}
{\LARGE What if you need to distribute you own:}
\vfill
{\Large Scripts}
\vfill
{\large Libraries }
\vfill
{\large Applications }
\vfill
\end{frame}
\begin{frame}[fragile]{Scripts}
\vfill
{\LARGE Often you can just copy, share, or check in the script to source
control and call it good.}
\vfill
But only if it's a single file, and doesn't need anything non-standard
\end{frame}
\begin{frame}[fragile]{Scripts}
\vfill
{\LARGE When the script needs more than just the stdlib (or your company standard environment)}
\vfill
{\LARGE You have an application, not a script}
\vfill
\end{frame}