@@ -19,7 +19,7 @@ Lightning Talks Today:
1919
2020 Danielle Marcos
2121
22- person 4
22+ Carolyn Evans
2323
2424================
2525Review/Questions
@@ -59,15 +59,15 @@ A Definition
5959There are many things you can do with a simple pattern like this one.
6060So 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**.
180180Because this is so common, Python provides a special operator to perform it
181181more *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
184184with that wierd `@ ` symbol)
185185
186186.. code-block :: python
@@ -324,21 +324,23 @@ for you to be writing your own.
324324
325325We've seen a few already:
326326
327+ .. nextslide ::
327328
328329For example, ``@staticmethod `` and ``@classmethod `` can also be used as simple
329330callables, 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
372375Remember 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+
411416Note that in this case, the decorator object returned by the property decorator
412417itself 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!
432439LAB
433440----
434441
435- **``p-wrapper`` Decorator **
442+ **p_wrapper Decorator **
436443
437444Write 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+
455463Lightning Talks
456464----------------
457465
@@ -464,9 +472,9 @@ Lightning Talks
464472|
465473
466474
467- ================
475+ =================
468476Context Managers
469- ================
477+ =================
470478
471479**A Short Digression **
472480
@@ -550,16 +558,14 @@ when the code block ends.
550558At this point in Python history, many functions you might expect to behave this
551559way 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
582588the thing and it shouldn't have a close() method?
583589
584- .. nextslide :: Do It Yourself
590+ Do It Yourself
591+ ----------------
585592
586593You 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
602606Let'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
627634This 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.
671677Consider this code:
672678
673679.. code-block :: python
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
736742run 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
11391158More 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+
12271261Homework
1228- ========
1262+ ---------
12291263
12301264Finish up the labs
12311265
12321266Work on your project
12331267
12341268And *do * let me know what you're doing if you haven't yet!
1235-
0 commit comments