44Session Ten: Decorators and Context Managers -- Wrap Up
55*******************************************************
66
7- =====================
8- Web Development Class
9- =====================
10-
11- .. rst-class :: large centered
12-
13- Internet Programming in Python
14-
15- Cris Ewing
16-
177================
188Review/Questions
199================
2010
2111Review of Previous Class
2212------------------------
2313
14+ Any questions???
15+
2416Homework review
2517---------------
2618
2719Homework Questions?
2820
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...
5822
5923Code Review
6024-----------
@@ -64,20 +28,14 @@ Anyone unsatisfied with their solution -- or stuck?
6428Let's do a code review!
6529
6630
67-
68- Iterators
69-
70- Generators
71-
72-
7331Projects
7432--------
7533
76- Due Dec Friday , Dec 11th, 11:59pm PST
34+ Due Sunday , Dec 11th
7735
7836.. rst-class :: medium
7937
80- (that's three days!)
38+ (that's five days!)
8139
8240Push to github or email them to me.
8341
@@ -123,7 +81,7 @@ Decorators
12381
12482 In Python, functions are first-class objects.
12583
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
12785 other objects.
12886
12987 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
263221function is called **decoration **.
264222
265223Because 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:
267227
268228.. code-block :: python
269229
@@ -275,7 +235,8 @@ more *declaratively*: the ``@`` operator -- I told you I'd eventually explain wh
275235 def add (a , b ):
276236 return a + b
277237
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.
279240
280241(demo)
281242
@@ -287,7 +248,7 @@ incomplete.
287248
288249In reality, decorators can be used with anything that is *callable *.
289250
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,
291252or a class that implements the ``__call__ `` special method.
292253
293254So in fact the definition should be updated as follows:
@@ -338,12 +299,14 @@ Let's try that out with a potentially expensive function:
338299 In [58]: sum2x(10000000)
339300 Out[58]: 99999990000000
340301
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?
342304
343305Nested Decorators
344306-----------------
345307
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:
347310
348311.. code-block :: python
349312
@@ -629,7 +592,7 @@ lines of defensive code have been replaced with this simple form:
629592
630593 ``open `` builtin is defined as a *context manager *.
631594
632- The resource it returnes (``file_handle ``) is automatically and reliably closed
595+ The resource it returns (``file_handle ``) is automatically and reliably closed
633596when the code block ends.
634597
635598.. _pep343 : http://legacy.python.org/dev/peps/pep-0343/
@@ -656,20 +619,20 @@ There are a couple of ways you can go.
656619If the resource in questions has a ``.close() `` method, then you can simply use
657620the ``closing `` context manager from ``contextlib `` to handle the issue:
658621
659- ** check example for py3 -- urlib depricated!
660-
661622.. code-block :: python
662623
663- import urllib
624+ from urllib import request
664625 from contextlib import closing
665626
666- with closing(urllib .urlopen(' http://google.com' )) as web_connection:
627+ with closing(request .urlopen(' http://google.com' )) as web_connection:
667628 # do something with the open resource
668629 # and here, it will be closed automatically
669630
670631 But what if the thing doesn't have a ``close() `` method, or you're creating
671632the thing and it shouldn't have a close() method?
672633
634+ (full confession: urlib.request was not a context manager in py2 -- but it is in py3)
635+
673636Do It Yourself
674637--------------
675638
@@ -678,9 +641,11 @@ You can also define a context manager of your own.
678641The interface is simple. It must be a class that implements two
679642more of the nifty python *special methods *
680643
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.
682646
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.
684649
685650The arguments will be the exception raised in the context.
686651
@@ -697,7 +662,7 @@ Consider this code:
697662
698663 class Context (object ):
699664 """ from Doug Hellmann, PyMOTW
700- http ://pymotw.com/2 /contextlib/#module-contextlib
665+ https ://pymotw.com/3 /contextlib/#module-contextlib
701666 """
702667 def __init__ (self , handle_error ):
703668 print (' __init__({} )' .format(handle_error))
@@ -844,15 +809,20 @@ timing should be printed to the file-like object.
844809Projects
845810--------
846811
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.
848814
849815To turn in:
850816 * Put up it up gitHub, and do a pull request
851817 * Put it in its own gitHub repository and point me to it.
852818 * zip up the code an email it to me.
853819
820+ 821+
854822Please do the online course evaluation
855823
856- Anyone want office hours Thursday evening?
824+ Anyone want office hours Sunday?
825+
826+ Or antoher time?
857827
858828Keep writing Python!
0 commit comments