Skip to content

Commit f91df56

Browse files
committed
in pretty good shape for session 09
1 parent 58f2c08 commit f91df56

4 files changed

Lines changed: 109 additions & 37 deletions

File tree

Examples/Session09/memoize.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
class Memoize:
2+
"""
3+
memoize decorator from avinash.vora
4+
http://avinashv.net/2008/04/python-decorators-syntactic-sugar/
5+
"""
6+
def __init__(self, function): # runs when memoize class is called
7+
self.function = function
8+
self.memoized = {}
9+
10+
def __call__(self, *args): # runs when memoize instance is called
11+
try:
12+
return self.memoized[args]
13+
except KeyError:
14+
self.memoized[args] = self.function(*args)
15+
return self.memoized[args]
16+
17+
18+
@Memoize
19+
def sum2x(n):
20+
return sum(2 * i for i in xrange(n))
21+
22+
import time
23+
24+
25+
def timed_func(func):
26+
def timed(*args, **kwargs):
27+
start = time.time()
28+
result = func(*args, **kwargs)
29+
elapsed = time.time() - start
30+
print "time expired: %s" % elapsed
31+
return result
32+
return timed
33+
34+
@timed_func
35+
@Memoize
36+
def sum2x(n):
37+
return sum(2 * i for i in xrange(n))

lightning_schedule.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,11 @@ week 8: Alireza Hashemloo
2626
week 8: Arielle R Simmons
2727
week 8: Eric W Westman
2828
week 8: Ryan J Albright
29-
week 9: Alexandra N Kazakova ** needs to be re-sheduled **
30-
week 9: Erik Ivan Lottsfeldt ** dropped**
29+
week 9: Carolyn J Evans
30+
week 9: Danielle G Marcos
3131
week 9: Louis John Ascoli
3232
week 9: Ralph P Brand
3333
week 10: Bryan L Davis
34-
week 10: Carolyn J Evans
34+
week 10:
3535
week 10: Changqing Zhu
3636
week 10: Alexandra N Kazakova

slides_sources/source/session09.rst

Lines changed: 66 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Lightning Talks Today:
1919

2020
Danielle Marcos
2121

22-
person 4
22+
Carolyn Evans
2323

2424
================
2525
Review/Questions
@@ -59,15 +59,15 @@ A Definition
5959
There are many things you can do with a simple pattern like this one.
6060
So many, that we give it a special name:
6161

62-
.. rst-class:: centered
62+
.. rst-class:: centered medium
6363

6464
**Decorator**
6565

66-
.. rst-class:: build
66+
.. rst-class:: build centered
6767
.. container::
6868

69-
A decorator is a function that takes a function as an argument and
70-
returns a function as a return value.
69+
"A decorator is a function that takes a function as an argument and
70+
returns a function as a return value.""
7171

7272
That's nice and all, but why is that useful?
7373

@@ -180,7 +180,7 @@ function is called **decoration**.
180180
Because this is so common, Python provides a special operator to perform it
181181
more *declaratively*: the ``@`` operator:
182182

183-
( I told you I'd eventually explain what was going on under the hood
183+
(I told you I'd eventually explain what was going on under the hood
184184
with that wierd `@` symbol)
185185

186186
.. code-block:: python
@@ -324,21 +324,23 @@ for you to be writing your own.
324324

325325
We've seen a few already:
326326

327+
.. nextslide::
327328

328329
For example, ``@staticmethod`` and ``@classmethod`` can also be used as simple
329330
callables, without the nifty decorator expression:
330331

331332
.. code-block:: python
332333
333334
# the way we saw last week:
334-
# and the decorator form
335335
class C(object):
336336
@staticmethod
337337
def add(a, b):
338338
return a + b
339339
340340
Is exactly the same as:
341341

342+
.. code-block:: python
343+
342344
class C(object):
343345
def add(a, b):
344346
return a + b
@@ -354,7 +356,7 @@ The ``classmethod()`` builtin can do the same thing:
354356

355357
.. code-block:: python
356358
357-
# and in declarative style
359+
# in declarative style
358360
class C(object):
359361
@classmethod
360362
def from_iterable(cls, seq):
@@ -367,7 +369,8 @@ The ``classmethod()`` builtin can do the same thing:
367369
from_iterable = classmethod(from_iterable)
368370
369371
370-
.. nextslide:: property()
372+
property()
373+
-----------
371374

372375
Remember the property() built in?
373376

@@ -408,9 +411,13 @@ But this could also be accomplished like so:
408411
x = property(getx, setx, delx,
409412
"I'm the 'x' property.")
410413
414+
.. nextslide::
415+
411416
Note that in this case, the decorator object returned by the property decorator
412417
itself implements additional decorators as attributes on the returned method
413-
object. S oyou could actually do this:
418+
object. So you could actually do this:
419+
420+
414421

415422
.. code-block:: python
416423
@@ -432,7 +439,7 @@ But that's getting really ugly!
432439
LAB
433440
----
434441

435-
**``p-wrapper`` Decorator**
442+
**p_wrapper Decorator**
436443

437444
Write a simple decorator you can apply to a function that returns a string.
438445

@@ -452,6 +459,7 @@ HTML 'p' tag:
452459
simple test code in
453460
:download:`Examples/Session09/test_p_wrapper.py <../../Examples/Session09/test_p_wrapper.py>`
454461

462+
455463
Lightning Talks
456464
----------------
457465

@@ -464,9 +472,9 @@ Lightning Talks
464472
|
465473
466474

467-
================
475+
=================
468476
Context Managers
469-
================
477+
=================
470478

471479
**A Short Digression**
472480

@@ -550,16 +558,14 @@ when the code block ends.
550558
At this point in Python history, many functions you might expect to behave this
551559
way do:
552560

553-
.. rst-class:: build
554-
555561
* ``open`` and ``io.open`` both work as context managers.
556562
(``io.open`` is good for working with unicode)
557563
* networks connections via ``socket`` do as well.
558564
* most implementations of database wrappers can open connections or cursors as
559565
context managers.
560566
* ...
561567

562-
But what if you are working with a library that doesn't support this
568+
* But what if you are working with a library that doesn't support this
563569
(``urllib``)?
564570

565571
.. nextslide:: Close It Automatically
@@ -581,23 +587,21 @@ the ``closing`` context manager from ``contextlib`` to handle the issue:
581587
But what if the thing doesn't have a ``close()`` method, or you're creating
582588
the thing and it shouldn't have a close() method?
583589

584-
.. nextslide:: Do It Yourself
590+
Do It Yourself
591+
----------------
585592

586593
You can also define a context manager of your own.
587594

588-
The interface is simple. It must be a class that implements these two
589-
more of the nifty python *special methods*:
595+
The interface is simple. It must be a class that implements two
596+
more of the nifty python *special methods*
590597

591-
``__enter__(self)``:
592-
Called when the ``with`` statement is run, it should return something to work
593-
with in the created context.
598+
**__enter__(self)** Called when the ``with`` statement is run, it should return something to work with in the created context.
594599

595-
``__exit__(self, e_type, e_val, e_traceback)``:
596-
Clean-up that needs to happen is implemented here.
600+
**__exit__(self, e_type, e_val, e_traceback)** Clean-up that needs to happen is implemented here.
597601

598-
The arguments will be the exception raised in the context.
602+
The arguments will be the exception raised in the context.
599603

600-
If the exception will be handled here, return True. If not, return False.
604+
If the exception will be handled here, return True. If not, return False.
601605

602606
Let's see this in action to get a sense of what happens.
603607

@@ -622,6 +626,9 @@ Consider this code:
622626
print '__exit__(%r, %r, %r)' % (exc_type, exc_val, exc_tb)
623627
return self.handle_error
624628
629+
:download:`Examples/Session09/context_managers.py <../../Examples/Session09/context_managers.py>`
630+
631+
625632
.. nextslide::
626633

627634
This class doesn't do much of anything, but playing with it can help
@@ -666,8 +673,7 @@ What if we try with ``False``?
666673
667674
.. nextslide:: ``contextmanager`` decorator
668675

669-
``contextlib.contextmanager`` turns generator functions into context managers
670-
676+
``contextlib.contextmanager`` turns generator functions into context managers.
671677
Consider this code:
672678

673679
.. code-block:: python
@@ -732,7 +738,7 @@ LAB
732738
----
733739
**Timing Context Manager**
734740

735-
Create a context manager that will print to stdout the elapsed time taken to
741+
Create a context manager that will print the elapsed time taken to
736742
run all the code inside the context:
737743

738744
.. code-block:: ipython
@@ -756,7 +762,7 @@ Lightning Talks
756762
|
757763
| Danielle Marcos
758764
|
759-
| ???
765+
| Carolyn Evans
760766
|
761767
762768

@@ -1135,6 +1141,19 @@ Once your setup.py is written, you can:
11351141
bdist_wininst create an executable installer for MS Windows
11361142
upload upload binary package to PyPI
11371143

1144+
wheels
1145+
------
1146+
1147+
"wheels" are the "new" packge format for python.
1148+
1149+
A wheel is essentially a zip file of the entire package, ready to be
1150+
unpacked in the right place on installation.
1151+
1152+
``pip`` will look for wheels for OS-X and Windows on PyPi, and auto-intall
1153+
them if they exist
1154+
1155+
This is particularly nice for pacakges with non-python dependiencies.
1156+
11381157

11391158
More complex packaging
11401159
----------------------
@@ -1223,13 +1242,27 @@ Write a setup.py for a script of yours
12231242

12241243
(my example: ``Examples/Session09/capitalize``)
12251244

1226-
========
1245+
==========
1246+
Next Week
1247+
==========
1248+
1249+
We'll be talking about Unicode. Read:
1250+
1251+
rst-class:: medium centered
1252+
1253+
The Absolute Minimum Every Software Developer Absolutely, Positively
1254+
Must Know About Unicode and Character Sets (No Excuses!)
1255+
1256+
http://www.joelonsoftware.com/articles/Unicode.html
1257+
1258+
Also: Cris Ewing will come by to talk about the second quarter
1259+
web development class
1260+
12271261
Homework
1228-
========
1262+
---------
12291263

12301264
Finish up the labs
12311265

12321266
Work on your project
12331267

12341268
And *do* let me know what you're doing if you haven't yet!
1235-

slides_sources/source/supplements/virtualenv.rst

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
.. _virtualenv_section:
2+
13
***********************
24
Working with Virtualenv
35
***********************
@@ -57,7 +59,7 @@ command available to you from your shell:
5759
5860
$ virtualenv --help
5961
Usage: virtualenv [OPTIONS] DEST_DIR
60-
62+
6163
Options:
6264
--version show program's version number and exit
6365
-h, --help ...

0 commit comments

Comments
 (0)