Skip to content

Commit c7513e5

Browse files
committed
updating session 10 (and a bit of 9)
1 parent f6cd36b commit c7513e5

File tree

5 files changed

+49
-81
lines changed

5 files changed

+49
-81
lines changed

slides_sources/source/conf.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@
3535
'sphinx.ext.todo',
3636
'sphinx.ext.coverage',
3737
# 'sphinx.ext.pngmath',
38-
#'sphinx.ext.mathjax',
39-
'sphinx.ext.jsmath',
38+
'sphinx.ext.mathjax',
39+
#'sphinx.ext.jsmath',
4040
'sphinx.ext.ifconfig',
4141
'IPython.sphinxext.ipython_console_highlighting',
4242
'IPython.sphinxext.ipython_directive',

slides_sources/source/exercises/lambda_magic.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ incremented by an increasing number.
2323

2424
Use a for loop, ``lambda``, and a keyword argument
2525

26-
( Extra credit ):
26+
**Extra credit:**
2727

2828
Do it with a list comprehension, instead of a for loop
2929

@@ -56,6 +56,6 @@ Example calling code
5656
5757
.. nextslide::
5858

59-
See the test code in Examples/Session06
59+
See the test code in Examples/Session09
6060

6161

slides_sources/source/exercises/trapezoid.rst

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,29 +8,27 @@ Passing functions around
88
=========================
99

1010

11-
.. rst-class:: large left
11+
.. rst-class:: medium left
1212

1313
Goal:
1414

15-
.. rst-class:: medium left
15+
.. rst-class:: left
1616

1717
Making use of functions as objects -- functions that act on functions.
1818

1919

2020
Trapezoidal rule
2121
----------------
2222

23-
.. rst-class:: medium
24-
25-
The "trapezoidal rule":
23+
The "trapezoidal rule":
2624

27-
https://en.wikipedia.org/wiki/Trapezoidal_rule
25+
https://en.wikipedia.org/wiki/Trapezoidal_rule
2826

29-
Is one of the easiest "quadrature" methods.
27+
Is one of the easiest "quadrature" methods.
3028

31-
Otherwise known as computing a definite integral, or, simply,
29+
Otherwise known as computing a definite integral, or, simply,
3230

33-
Computing the area under a curve.
31+
Computing the area under a curve.
3432

3533
The task
3634
--------
@@ -163,7 +161,7 @@ https://www.python.org/dev/peps/pep-0485/
163161

164162
In earlier pythons -- you'll need your own. There is one in:
165163

166-
``Examples/Session06/test_trapz.py``
164+
``Examples/Session09/test_trapz.py``
167165

168166

169167

@@ -232,7 +230,7 @@ or
232230

233231
This is pretty conceptually challenging -- but it's very little code!
234232

235-
If you are totally lost -- look at the lecture notes from last class -- how can you both accept and pass arbitrary arguments to/from a function?
233+
If you are totally lost -- look at the lecture notes from previous classes -- how can you both accept and pass arbitrary arguments to/from a function?
236234

237235
.. nextslide::
238236

slides_sources/source/session09.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ filter
116116
------
117117

118118
``filter`` "filters" a sequence of objects with a boolean function --
119-
It keeps only those for which the function is True -- filtering our the rest.
119+
It keeps only those for which the function is True -- filtering out the rest.
120120

121121
To get only the even numbers:
122122

@@ -160,7 +160,7 @@ Yes:
160160
Functional Programming
161161
----------------------
162162

163-
Comprehensions, map, and filter are all "functional programming" approaches}
163+
Comprehensions, map, and filter are all "functional programming" approaches
164164

165165
``map, filter`` and ``reduce`` pre-date comprehensions in Python's history
166166

@@ -174,7 +174,7 @@ And "map-reduce" is a big concept these days for parallel processing of "Big Dat
174174
A bit more about lambda
175175
------------------------
176176

177-
It is very useful for specifying sorting as well:
177+
It is very useful for specifying the sorting key:
178178

179179
.. code-block:: ipython
180180

slides_sources/source/session10.rst

Lines changed: 33 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -4,57 +4,21 @@
44
Session 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
================
188
Review/Questions
199
================
2010

2111
Review of Previous Class
2212
------------------------
2313

14+
Any questions???
15+
2416
Homework review
2517
---------------
2618

2719
Homework 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

5923
Code Review
6024
-----------
@@ -64,20 +28,14 @@ Anyone unsatisfied with their solution -- or stuck?
6428
Let's do a code review!
6529

6630

67-
68-
Iterators
69-
70-
Generators
71-
72-
7331
Projects
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

8240
Push 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
263221
function is called **decoration**.
264222

265223
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:
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

288249
In 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,
291252
or a class that implements the ``__call__`` special method.
292253

293254
So 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

343305
Nested 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
633596
when 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.
656619
If the resource in questions has a ``.close()`` method, then you can simply use
657620
the ``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
671632
the 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+
673636
Do It Yourself
674637
--------------
675638

@@ -678,9 +641,11 @@ You can also define a context manager of your own.
678641
The interface is simple. It must be a class that implements two
679642
more 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

685650
The 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.
844809
Projects
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

849815
To 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+
854822
Please 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

858828
Keep writing Python!

0 commit comments

Comments
 (0)