-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathpresentation-week-06.tex
More file actions
751 lines (544 loc) · 15.4 KB
/
presentation-week-06.tex
File metadata and controls
751 lines (544 loc) · 15.4 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
\documentclass{beamer}
%\usepackage[latin1]{inputenc}
\usetheme{Warsaw}
\title[Python Certificate: System Development]{Week 6: Advanced OO -- Numpy}
\author{Christopher Barker}
\institute{UW Continuing Education}
\date{April 30, 2013}
\usepackage{listings}
\usepackage{hyperref}
\begin{document}
% ---------------------------------------------
\begin{frame}
\titlepage
\end{frame}
% ---------------------------------------------
\begin{frame}
\frametitle{Table of Contents}
%\tableofcontents[currentsection]
\tableofcontents
\end{frame}
\begin{frame}[fragile]{Some updates:}
\Large{Some folks have asked to learn about Desktop GUIs}
\vfill
\Large{And the class is smaller than it may have been}
\vfill
\Large{So: week 9 will be Desktop GUIS with wxPython}
\vfill
\Large{And we'll do all the student presentations on week 10: May 28th}
\end{frame}
\begin{frame}[fragile]{BlueBox VMs}
\Large{Does anyone still need their BlueBox VMs?}
\vfill
\large{ ( Conor, you're all set) }
\end{frame}
\section{Advanced-OO}
% ---------------------------------------------
\begin{frame}[fragile]{class creation}
{\Large What happens when a class instance is created?}
\vfill
\begin{verbatim}
class Class(object):
def __init__(self, arg1, arg2):
self.arg1 = arg1
self.arg2 = arg2
.....
\end{verbatim}
\vfill
\begin{itemize}
\item A new instance is created
\item \verb|__init__| is called
\item The code in \verb|__init__| is run to initialize the instance
\end{itemize}
\vfill
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{class creation}
{\Large What if you need to do something before creation?}
\vfill
{\Large Enter: \verb|__new__|}
\vfill
\begin{verbatim}
class Class(object):
def __new__(cls, arg1, arg2):
some_code_here
return cls()
.....
\end{verbatim}
\vfill
\begin{itemize}
\item \verb|__new__| is called: it returns a new instance
\item The code in \verb|__new__| is run to pre-initialize
\item \verb|__init__| is called
\item The code in \verb|__init__| is run to initialize the instance
\end{itemize}
\vfill
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{class creation}
{\large \verb|__new__| is a static method -- but it must be called with a class object as the first argument. And it should return a class instance: }
\vfill
\begin{verbatim}
class Class(superclass):
def __new__(cls, arg1, arg2):
some_code_here
return superclass.__new__(cls)
.....
\end{verbatim}
\vfill
\begin{itemize}
\item \verb|__new__| is called: it returns a new instance
\item The code in \verb|__new__| is run to pre-initialize
\item \verb|__init__| is called
\item The code in \verb|__init__| is run to initialize the instance
\end{itemize}
\vfill
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{class creation}
{\Large When would you need to use it:}
\begin{itemize}
\item subclassing an immutable type:\\
-- It's too late to change it once you get to \verb|__init__|
\item When \verb|__init__| not called:
\begin{itemize}
\item unpickling
\item copying
\end{itemize}
{\large You may need to put some code in \verb|__new__| to make sure things go right}
\end{itemize}
\vfill
{\large More detail here:}
\url{http://www.python.org/download/releases/2.2/descrintro/#__new__}
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{LAB}
{\large Demo: \verb|code/__new__/new_example.py|}
\vfill
{\Large Write a subclass of int that will always be an even number: round the input to the closest even number}
\vfill
{\large \verb|code/__new__/even_int.py|}
\vfill
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{multiple inheritance}
{\Large Multiple inheritance:\\
\hspace{0.2in} Pulling from more than one class}
\vfill
\begin{verbatim}
class Combined(Super1, Super2, Super3):
def __init__(self, something, something else):
Super1.__init__(self, ......)
Super2.__init__(self, ......)
Super3.__init__(self, ......)
\end{verbatim}
(calls to the super classes \verb|__init__| are optional -- case dependent)
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{multiple inheritance}
\vfill
{\Large Method Resolution Order -- left to right}
\begin{enumerate}
\item Is it an instance attribute ?
\item Is it a class attribute ?
\item Is it a superclass attribute ?
\begin{enumerate}
\item is it an attribute of the left-most superclass?
\item is it an attribute of the next superclass?
\item ....
\end{enumerate}
\item Is it a super-superclass attribute ?
\item ...also left to right...
\end{enumerate}
\vfill
{\large ( This can get complicated...more on that later...)}
\end{frame}
% ---------------------------------------------
\begin{frame}[fragile]{mix-ins}
{\Large Why would you want to do this?}
\vfill
{\Large Hierarchies are not always simple:}
\vfill
\begin{itemize}
\item Animal
\begin{itemize}
\item Mammal
\begin{itemize}
\item GiveBirth()
\end{itemize}
\item Bird
\begin{itemize}
\item LayEggs()
\end{itemize}
\end{itemize}
\end{itemize}
\vfill
{\Large Where do you put a Platypus or an Armadillo?}
\vfill
{\Large Real World Example: \verb|wxPython FloatCanvas|}
\end{frame}
%--------------------------------
\begin{frame}[fragile]{super}
{\Large getting the superclass:}
\begin{verbatim}
class SafeVehicle(Vehicle):
"""
Safe Vehicle subclass of Vehicle base class...
"""
def __init__(self, position=0, velocity=0, icon='S'):
Vehicle.__init__(self, position, velocity, icon)
\end{verbatim}
{\Large
\vfill
not DRY
\vfill
also, what if we had a bunch of references to superclass?
}
\end{frame}
\begin{frame}[fragile]{super}
{\Large getting the superclass:}
\begin{verbatim}
class SafeVehicle(Vehicle):
"""
Safe Vehicle subclass of Vehicle base class
"""
def __init__(self, position=0, velocity=0, icon='S'):
super(SafeVehicle, self).__init__(position, velocity, icon)
\end{verbatim}
\vfill
{\Large but super is about more than just DRY...}
\vfill
{\Large Remember the method resolution order?}
\end{frame}
\begin{frame}[fragile]{What does super() do?}
\vfill
{\Large \verb|super| returns a \\
``proxy object that delegates method calls''}
\vfill
{\Large It's not returning the object itself -- but you can call methods on it}
\vfill
{\Large It runs through the method resolution order (MRO) to find the method you call.}
\vfill
{\Large Key point: the MRO is determined \emph{at run time} }
\vfill
\url{http://docs.python.org/2/library/functions.html#super}
\end{frame}
\begin{frame}[fragile]{What does super() do?}
\vfill
{\Large Not the same as calling one superclass method...}
\vfill
{\Large \verb|super()| will call all the sibling superclass methods}
\begin{verbatim}
class D(C, B, A):
def __init__(self):
super(D, self).__init__()
\end{verbatim}
same as
\begin{verbatim}
class D(C, B, A):
def __init__(self):
C.__init__()
B.__init__()
A.__init__()
\end{verbatim}
{\Large you may not want that...}
\end{frame}
\begin{frame}[fragile]{super}
{\Large Two seminal articles about \verb|super()|:}
\vfill
{\LARGE ``Super Considered Harmful''}\\[0.1in]
{\Large \hspace{0.5in}-- James Knight }
\vfill
\url{https://fuhm.net/super-harmful/}
\vfill
{\LARGE ``super() considered super!''}\\[0.1in]
{\Large \hspace{0.5in}-- Raymond Hettinger }
\vfill
\url{http://rhettinger.wordpress.com/2011/05/26/super-considered-super/}
\vfill
{\large (Both worth reading....)}
\end{frame}
\begin{frame}[fragile]{super issues...}
{\Large Both actually say similar things:}
\begin{itemize}
\item The method being called by super() needs to exist
\item Every occurrence of the method needs to use super():\\
Use it consistently, and document that you use it, as it is part of the external interface for your class, like it or not.
\item The caller and callee need to have a matching argument signature:
\begin{itemize}
\item Never call super with anything but the exact arguments you received, unless you really know what you're doing.
\item When you use it on methods whose acceptable arguments can be altered on a subclass via addition of more optional arguments, always accept *args, **kwargs, and call super like \verb|super(MyClass, self).method(args_declared, *args, **kwargs)|.
\end{itemize}
\end{itemize}
\end{frame}
%\section{meta-classes}
%-------------------------------
\begin{frame}[fragile]{Wrap Up}
{\LARGE Thinking OO in Python:}
\vfill
{\large Think about what makes sense for your code:}
\begin{itemize}
\item {\large Code re-use}
\item {\large Clean APIs}
\item {\large ... }
\end{itemize}
\vfill
{\large Don't be a slave to what OO is \emph{supposed} to look like. }
\vfill
{\large Let OO work for you, not \emph{create} work for you}
\end{frame}
%-------------------------------
\begin{frame}[fragile]{Wrap Up}
{\Large OO in Python:}
\vfill
{\Large The Art of Subclassing}: Raymond Hettinger
\vfill
{\small \url{http://pyvideo.org/video/879/the-art-of-subclassing}}
\vfill
''classes are for code re-use -- not creating taxonomies''
\vfill
{\Large Stop Writing Classes}: Jack Diederich
\vfill
{\small \url{http://pyvideo.org/video/880/stop-writing-classes}}
\vfill
``If your class has only two methods -- and one of them is \verb|__init__|
-- you don't need a class ''
\end{frame}
\section{numpy}
%---------------------------
\begin{frame}[fragile]{numpy}
\vfill
\begin{center}
{\LARGE numpy}
\vfill
{\Large Not just for lots of numbers!\\[0.1in]
(but it's great for that!)}
\end{center}
\vfill
\url{http://www.numpy.org/}
\end{frame}
\begin{frame}[fragile]{what is numpy?}
{\Large An N-Dimensional array object}
\vfill
{\Large A whole pile of tools for operations on/with that object.}
\vfill
\end{frame}
%----------------------------------
\begin{frame}[fragile]{Why numpy?}
{\Large Classic answer: Lots of numbers}
\vfill
\begin{itemize}
\item Faster
\item Less memory
\item More data types
\end{itemize}
\vfill
{\Large Even if you don't have lot of numbers:}
\begin{itemize}
\item N-d array slicing
\item Vector operations
\item Flexible data types
\end{itemize}
\end{frame}
% %----------------------------------
\begin{frame}[fragile]{why numpy?}
{\Large Wrapper for a block of memory:}
\begin{itemize}
\item Interfacing with C libs
\item PyOpenGL
\item GDAL
\item NetCDF4
\item Shapely
\end{itemize}
{\Large Image processing:}
\begin{itemize}
\item PIL
\item WxImage
\item ndimage
\end{itemize}
\end{frame}
%----------------------------------
\begin{frame}[fragile]{What is an nd array?}
\begin{itemize}
\item N-dimensional (up to 32!)
\item Homogeneous array:
\begin{itemize}
\item Every element is the same type\\
(but that type can be a pyObject)
\item Int, float, char -- more exotic types
\end{itemize}
\item ``rank'' – number of dimensions
\item Strided data:
\begin{itemize}
\item Describes how to index into block of memory
\item PEP 3118 -- Revising the buffer protocol
\end{itemize}
\end{itemize}
\vfill
{\large demos: \verb|memory.py| and \verb|structure.py|}
\end{frame}
%----------------------------------
\begin{frame}[fragile]{Built-in Data Types}
\begin{itemize}
\item Signed and unsigned Integers\\
8, 16, 32, 64 bits
\item Floating Point\\
32, 64, 96, 128 bits (not all platforms)
\item Complex\\
64, 128, 192, 256 bits
\item String and unicode\\
Static length
\item Bool \\
8 bit
\item Python Object \\
Really a pointer
\end{itemize}
\vfill
{\large demo: \verb|object.py|}
\end{frame}
%----------------------------------
\begin{frame}[fragile]{Compund dtypes}
{\Large
\begin{itemize}
\item Can define any combination of other types \\
Still Homogenous: Array of structs.
\item Can name the fields
\item Can be like a database table
\item Useful for reading binary data
\end{itemize}
}
\vfill
{\Large demo: \verb|dtypes.py|}
\end{frame}
%----------------------------------
\begin{frame}[fragile]{Array Constructors:}
{\Large From scratch:}
\verb|ones(), zeros(), empty(), arange(), linspace(), logspace()|
( Default dtype: np.float64 )
\vfill
{\Large From sequences:}
\verb|array(), asarray()|
( Build from any sequence )
\vfill
{\Large From binary data:}
\verb|fromstring(), frombuffer(), fromfile()|
\vfill
{\Large Assorted linear algebra standards:}
\verb|eye(), diag(), etc.|
\vfill
{\large demo: \verb|constructors.py|}
\end{frame}
%----------------------------------
\begin{frame}[fragile]{Broadcasting:}
{\Large Element-wise operations among two different rank arrays:}
\vfill
{\Large Simple case: scalar and array:}
\begin{verbatim}
In [37]: a
Out[37]: array([1, 2, 3])
In [38]: a*3
Out[38]: array([3, 6, 9])
\end{verbatim}
{\Large Great for functions of more than one variable on a grid}
\vfill
{\large demo: \verb|broadcasting.py|}
\end{frame}
%----------------------------------
\begin{frame}[fragile]{Slicing -- views:}
{\Large a slice is a ``view'' on the array:\\
new object, but shares memory:}
\begin{verbatim}
In [12]: a = np.array((1,2,3,4))
In [13]: b = a[:]
# for lists -- [:] means copy -- not for arrays!
In [15]: a is b
Out[15]: False
# it's new array, but...
In [16]: b[2] = 5
In [17]: a
Out[17]: array([1, 2, 5, 4])
# a and b share data
\end{verbatim}
\vfill
{\large demo: \verb|slice.py|}
\end{frame}
%----------------------------------
\begin{frame}[fragile]{Working with compiled code:}
{\Large Wrapper around a C pointer to a block of data}
\begin{itemize}
\item Some code can't be vectorized
\item Interface with existing libraries
\end{itemize}
\vfill
{\Large Tools:}
\begin{itemize}
\item C API: you don't want to do that!
\item Cython: typed arrays
\item Ctypes
\item SWIG: numpy.i
\item Boost: boost array
\item f2py
\end{itemize}
\vfill
Example of numpy+cython: \url{http://wiki.cython.org/examples/mandelbrot}
\end{frame}
%----------------------------------
\begin{frame}[fragile]{numpy persistance:}
{\Large \verb|.tofile() / fromfile()|}\\
-- Just the raw bytes, no metadata
\vfill
{\Large pickle }
\vfill
{\Large \verb|savez()| -- numpy zip format}\\
Compact: binary dump plus metadata
\vfill
{\Large netcdf}
\vfill
{\Large Hdf}
\begin{itemize}
\item Pyhdf
\item pytables
\end{itemize}
\end{frame}
%----------------------------------
\begin{frame}[fragile]{Other stuff:}
\begin{itemize}
\item Masked arrays
\item Memory-mapped files
\item Set operations: unique, etc
\item Random numbers
\item Polynomials
\item FFT
\item Sorting and searching
\item Linear Algebra
\item Statistics
\end{itemize}
{\large (And all of scipy!)}
\end{frame}
%----------------------------------
\begin{frame}[fragile]{numpy docs:}
\url{www.numpy.org}\\
-- Numpy reference Downloads, etc
\vfill
\url{www.scipy.org}\\
-- lots of docs
\vfill
{\large Scipy cookbook}\\
\url{http://www.scipy.org/Cookbook}
\vfill
{\large ``The Numpy Book''}\\
\url{http://www.tramy.us/numpybook.pdf}
\end{frame}
% %----------------------------------
% \begin{frame}[fragile]{Other stuff:}
% \end{frame}
%-------------------------------
\begin{frame}[fragile]{Next Week:}
\vfill
{\LARGE Threading / Multiprocessing}\\[0.2in]
{\Large \hspace{0.5in} -- Jeff}
\vfill
{\Large And of course, your projects...}
\vfill
\end{frame}
\end{document}