4
4
Session Ten: Decorators and Context Managers -- Wrap Up
5
5
*******************************************************
6
6
7
- =====================
8
- Web Development Class
9
- =====================
10
-
11
- .. rst-class :: large centered
12
-
13
- Internet Programming in Python
14
-
15
- Cris Ewing
16
-
17
7
================
18
8
Review/Questions
19
9
================
20
10
21
11
Review of Previous Class
22
12
------------------------
23
13
14
+ Any questions???
15
+
24
16
Homework review
25
17
---------------
26
18
27
19
Homework Questions?
28
20
29
- Did you all get a trapedzoidal rule function working?
30
-
31
- Anyone get the "passing through of arguments"?
32
-
33
- How about the adaptive solutions?
34
-
35
-
36
- Notes on Floating point
37
- -----------------------
38
-
39
- Did anyone look at the isclose() function?
40
-
41
- How to make a range of numbers in floating point?
42
-
43
- Anyone do something like this?:
44
-
45
- .. code-block :: python
46
-
47
- s = []
48
- x = a
49
- while x <= b:
50
- s.append(x)
51
- x += delta_x
52
-
53
- -- see my solution.
54
-
55
- Some notes about FP issues:
56
-
57
- https://docs.python.org/3.5/tutorial/floatingpoint.html
21
+ From any of the Exercises...
58
22
59
23
Code Review
60
24
-----------
@@ -64,20 +28,14 @@ Anyone unsatisfied with their solution -- or stuck?
64
28
Let's do a code review!
65
29
66
30
67
-
68
- Iterators
69
-
70
- Generators
71
-
72
-
73
31
Projects
74
32
--------
75
33
76
- Due Dec Friday , Dec 11th, 11:59pm PST
34
+ Due Sunday , Dec 11th
77
35
78
36
.. rst-class :: medium
79
37
80
- (that's three days!)
38
+ (that's five days!)
81
39
82
40
Push to github or email them to me.
83
41
@@ -123,7 +81,7 @@ Decorators
123
81
124
82
In Python, functions are first-class objects.
125
83
126
- This means that you can bind names to them, pass them around, etc, just like
84
+ This means that you can bind names to them, pass them around, etc. , just like
127
85
other objects.
128
86
129
87
Because of this fact, you can write functions that take functions as
@@ -263,7 +221,9 @@ Rebinding the name of a function to the result of calling a decorator on that
263
221
function is called **decoration **.
264
222
265
223
Because this is so common, Python provides a special operator to perform it
266
- more *declaratively *: the ``@ `` operator -- I told you I'd eventually explain what was going on under the hood with that wierd `@ ` symbol:
224
+ more *declaratively *: the ``@ `` operator
225
+ -- I told you I'd eventually explain what was going on under the hood with
226
+ that wierd `@ ` symbol:
267
227
268
228
.. code-block :: python
269
229
@@ -275,7 +235,8 @@ more *declaratively*: the ``@`` operator -- I told you I'd eventually explain wh
275
235
def add (a , b ):
276
236
return a + b
277
237
278
- The declarative form (called a decorator expression) is far more common, but both have the identical result, and can be used interchangeably.
238
+ The declarative form (called a decorator expression) is far more common,
239
+ but both have the identical result, and can be used interchangeably.
279
240
280
241
(demo)
281
242
@@ -287,7 +248,7 @@ incomplete.
287
248
288
249
In reality, decorators can be used with anything that is *callable *.
289
250
290
- Remember from two weeks ago , a *callable * is a function, a method on a class,
251
+ Remember from last week , a *callable * is a function, a method on a class,
291
252
or a class that implements the ``__call__ `` special method.
292
253
293
254
So in fact the definition should be updated as follows:
@@ -338,12 +299,14 @@ Let's try that out with a potentially expensive function:
338
299
In [58]: sum2x(10000000)
339
300
Out[58]: 99999990000000
340
301
341
- It's nice to see that in action, but what if we want to know *exactly * how much difference it made?
302
+ It's nice to see that in action, but what if we want to know *exactly *
303
+ how much difference it made?
342
304
343
305
Nested Decorators
344
306
-----------------
345
307
346
- You can stack decorator expressions. The result is like calling each decorator in order, from bottom to top:
308
+ You can stack decorator expressions. The result is like calling each
309
+ decorator in order, from bottom to top:
347
310
348
311
.. code-block :: python
349
312
@@ -629,7 +592,7 @@ lines of defensive code have been replaced with this simple form:
629
592
630
593
``open `` builtin is defined as a *context manager *.
631
594
632
- The resource it returnes (``file_handle ``) is automatically and reliably closed
595
+ The resource it returns (``file_handle ``) is automatically and reliably closed
633
596
when the code block ends.
634
597
635
598
.. _pep343 : http://legacy.python.org/dev/peps/pep-0343/
@@ -656,20 +619,20 @@ There are a couple of ways you can go.
656
619
If the resource in questions has a ``.close() `` method, then you can simply use
657
620
the ``closing `` context manager from ``contextlib `` to handle the issue:
658
621
659
- ** check example for py3 -- urlib depricated!
660
-
661
622
.. code-block :: python
662
623
663
- import urllib
624
+ from urllib import request
664
625
from contextlib import closing
665
626
666
- with closing(urllib .urlopen(' http://google.com' )) as web_connection:
627
+ with closing(request .urlopen(' http://google.com' )) as web_connection:
667
628
# do something with the open resource
668
629
# and here, it will be closed automatically
669
630
670
631
But what if the thing doesn't have a ``close() `` method, or you're creating
671
632
the thing and it shouldn't have a close() method?
672
633
634
+ (full confession: urlib.request was not a context manager in py2 -- but it is in py3)
635
+
673
636
Do It Yourself
674
637
--------------
675
638
@@ -678,9 +641,11 @@ You can also define a context manager of your own.
678
641
The interface is simple. It must be a class that implements two
679
642
more of the nifty python *special methods *
680
643
681
- **__enter__(self) ** Called when the ``with `` statement is run, it should return something to work with in the created context.
644
+ **__enter__(self) ** Called when the ``with `` statement is run, it should
645
+ return something to work with in the created context.
682
646
683
- **__exit__(self, e_type, e_val, e_traceback) ** Clean-up that needs to happen is implemented here.
647
+ **__exit__(self, e_type, e_val, e_traceback) ** Clean-up that needs to
648
+ happen is implemented here.
684
649
685
650
The arguments will be the exception raised in the context.
686
651
@@ -697,7 +662,7 @@ Consider this code:
697
662
698
663
class Context (object ):
699
664
""" from Doug Hellmann, PyMOTW
700
- http ://pymotw.com/2 /contextlib/#module-contextlib
665
+ https ://pymotw.com/3 /contextlib/#module-contextlib
701
666
"""
702
667
def __init__ (self , handle_error ):
703
668
print (' __init__({} )' .format(handle_error))
@@ -844,15 +809,20 @@ timing should be printed to the file-like object.
844
809
Projects
845
810
--------
846
811
847
- Projects due this Friday. We'll review them over the weekend.
812
+ Projects due this Sunday. We'll review them early next week. If you turn
813
+ it in early, we should review it sooner.
848
814
849
815
To turn in:
850
816
* Put up it up gitHub, and do a pull request
851
817
* Put it in its own gitHub repository and point me to it.
852
818
* zip up the code an email it to me.
853
819
820
+
821
+
854
822
Please do the online course evaluation
855
823
856
- Anyone want office hours Thursday evening?
824
+ Anyone want office hours Sunday?
825
+
826
+ Or antoher time?
857
827
858
828
Keep writing Python!
0 commit comments