Skip to content

Commit 662300e

Browse files
committed
updated session 6
1 parent bf05ebf commit 662300e

File tree

3 files changed

+110
-49
lines changed

3 files changed

+110
-49
lines changed

Syllabus.rst

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,11 +298,9 @@ Advanced Argument passing
298298
Week 7: November 15
299299
...................
300300

301-
Lambda
302-
303-
Functions as Objects
301+
Object Oriented Programming:
304302

305-
Object Oriented Programming: classes, instances, and methods
303+
classes, instances, methods, inheritance
306304

307305

308306
Week 8: November 22
@@ -314,6 +312,9 @@ Emulating built-in types
314312

315313
Week 9: November 29
316314
...................
315+
Lambda
316+
317+
Functions as Objects
317318

318319
Iterators and Generators
319320

slides_sources/source/session06.rst

Lines changed: 94 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
.. include:: include.rst
22

3-
*****************************************************************************
4-
Session Six: Testing, Advanced Argument Passing, lambda, functions as objects
5-
*****************************************************************************
3+
***********************************************
4+
Session Six: Testing, Advanced Argument Passing
5+
***********************************************
66

77
======================
88
Lightning Talks Today:
@@ -86,18 +86,20 @@ if you need to do along string literal, sometimes a triple quoted string is perf
8686
so having the line endings automatic is great.
8787
"""
8888

89-
But yo don't always want the line endings quite like that. And you may not want all that whitespace when fitting it into indented code.
89+
But you don't always want the line endings quite like that. And you may not want all that whitespace when fitting it into indented code.
9090

91-
It turns out that when you put a multiple strings together with no commas or anythign in between -- pyhton will join them::
91+
It turns out that when you put a multiple strings together with no commas or anythign in between -- python will join them:
9292

9393
.. code-block:: ipython
9494
9595
In [81]: "this is " "a string " "built up of parts"
9696
Out[81]: 'this is a string built up of parts'
9797
98-
If it's parentheses, you can put the next chunk on the next line:
98+
.. nextslide::
9999

100-
.. code-block:: ipython
100+
If it's in parentheses, you can put the next chunk on the next line:
101+
102+
.. code-block:: python
101103
102104
print("{} is from {}, and he likes "
103105
"{} cake, {} fruit, {} salad, "
@@ -111,7 +113,7 @@ If it's parentheses, you can put the next chunk on the next line:
111113
pretty print
112114
------------
113115

114-
if you need to print our nested (or large) data structure in a more readable fashion, the "pretty print" module is handy:
116+
If you need to print our nested (or large) data structure in a more readable fashion, the "pretty print" module is handy:
115117

116118
.. code-block:: ipython
117119
@@ -128,6 +130,22 @@ if you need to print our nested (or large) data structure in a more readable fas
128130
'pasta': 'lasagna',
129131
'salad': 'greek'}
130132
133+
Exceptions
134+
----------
135+
136+
Adding stuff to an Exception:
137+
138+
Example from slack
139+
140+
141+
Anything else?
142+
--------------
143+
144+
.. rst-class:: center medium
145+
146+
Anything else you want me to go over?
147+
148+
131149
Lightning Talks
132150
----------------
133151

@@ -146,14 +164,14 @@ Testing
146164
.. rst-class:: build left
147165
.. container::
148166

149-
You've already seen some a very basic testing strategy.
167+
You've already seen a very basic testing strategy.
150168

151169
You've written some tests using that strategy.
152170

153171
These tests were pretty basic, and a bit awkward in places (testing error
154172
conditions in particular).
155173

156-
.. rst-class:: centered
174+
.. rst-class:: centered large
157175

158176
**It gets better**
159177

@@ -283,7 +301,9 @@ There are several other options for running tests in Python.
283301

284302
* ... (many frameworks supply their own test runners: e.g. django)
285303

286-
Both are very capable and widely used. I have a personal preference for pytest -- so we'll use it for this class
304+
Both are very capable and widely used. I have a personal preference for pytest
305+
306+
-- so we'll use it for this class
287307

288308
Installing ``pytest``
289309
---------------------
@@ -313,13 +333,17 @@ Pre-existing Tests
313333

314334
Let's take a look at some examples.
315335

316-
``IntroPython2016\Examples\Session06``
336+
in ``IntroPython2016\Examples\Session06``
337+
338+
.. code-block:: bash
317339
318-
`` $ py.test``
340+
$ py.test
319341
320342
You can also run py.test on a particular test file:
321343

322-
``py.test test_cigar_party.py``
344+
.. code-block:: bash
345+
346+
$ py.test test_random_unitest.py
323347
324348
The results you should have seen when you ran ``py.test`` above come
325349
partly from these files.
@@ -328,68 +352,90 @@ Let's take a few minutes to look these files over.
328352

329353
[demo]
330354

331-
.. nextslide:: What's Happening Here.
355+
What's Happening Here.
356+
----------------------
332357

333358
When you run the ``py.test`` command, ``pytest`` starts in your current
334359
working directory and searches the filesystem for things that might be tests.
335360

336361
It follows some simple rules:
337362

338-
.. rst-class:: build
339-
340363
* Any python file that starts with ``test_`` or ``_test`` is imported.
364+
341365
* Any functions in them that start with ``test_`` are run as tests.
366+
342367
* Any classes that start with ``Test`` are treated similarly, with methods that begin with ``test_`` treated as tests.
343368

344-
( don't worry about "classes" just yet ;-) )
369+
( don't worry about "classes" part just yet ;-) )
345370

346-
.. nextslide:: pytest
371+
pytest
372+
------
347373

348374
This test running framework is simple, flexible and configurable.
349375

350376
Read the documentation for more information:
351377

352378
http://pytest.org/latest/getting-started.html#getstarted
353379

380+
It will run ``unittest`` tests for you.
381+
382+
But in addition to finding and running tests, it makes writting tests simple, and provides a bunch of nifty utilities to support more complex testing.
383+
384+
354385
Test Driven Development
355386
-----------------------
356387
in the Examples dir, try::
357388

358389
$ py.test test_cigar_party
359390

360-
What we've just done here is the first step in what is called **Test Driven
361-
Development**.
391+
What we've just done here is the first step in what is called:
392+
393+
.. rst-class:: centered
394+
395+
**Test Driven Development**.
362396

363397
A bunch of tests exist, but the code to make them pass does not yet exist.
364398

365-
The red you see in the terminal when we run our tests is a goad to us to write
366-
the code that fixes these tests.
399+
The red you see in the terminal when we run our tests is a goad to us to write the code that fixes these tests.
367400

368401
Let's do that next!
369402

370-
============================
371403
Test Driven development demo
372-
============================
404+
-----------------------------
405+
406+
Open up:
407+
408+
``Examples/Session06/test_cigar_party.py``
409+
410+
and:
373411

412+
``Examples/Session06/cigar_party.py``
374413

375-
In ``Examples/Session06/test_cigar_party.py``
414+
and run::
376415

416+
$ py.teset test_cigar_party.py
417+
418+
Now go in to ``cigar_party.py`` and let's fix the tests.
419+
420+
Let's play with codingbat.py also...
377421

378422
===
379423
LAB
380424
===
381425

382-
Pick an example from codingbat:
426+
.. rst-class:: left
427+
428+
Pick an example from codingbat:
383429

384-
``http://codingbat.com``
430+
``http://codingbat.com``
385431

386-
Do a bit of test-driven development on it:
432+
Do a bit of test-driven development on it:
387433

388-
* run something on the web site.
389-
* write a few tests using the examples from the site.
390-
* then write the function, and fix it 'till it passes the tests.
434+
* run something on the web site.
435+
* write a few tests using the examples from the site.
436+
* then write the function, and fix it 'till it passes the tests.
391437

392-
Do at least two of these...
438+
Do at least two of these...
393439

394440
Lightning Talk
395441
--------------
@@ -473,7 +519,7 @@ This is a **very** important point -- I will repeat it!
473519
Function arguments in variables
474520
-------------------------------
475521

476-
function arguments are really just
522+
When a function is called, its arguments are really just:
477523

478524
* a tuple (positional arguments)
479525
* a dict (keyword arguments)
@@ -758,21 +804,24 @@ You get a new list every time the function is called
758804
Homework
759805
========
760806

761-
Finish up the Labs
807+
.. rst-class:: left
762808

763-
Material to review for next week
764-
--------------------------------
809+
Finish up the Labs
765810

766-
Lambda:
811+
Write a complete set of unit tests for your mailroom program.
767812

768-
http://www.blog.pythonlibrary.org/2015/10/28/python-101-lambda-basics/
813+
* You will likely find that it is really hard to test without refactoring.
769814

770-
https://pythonconquerstheuniverse.wordpress.com/2011/08/29/lambda_tutorial/
815+
* This is Good!
771816

772-
Functions as Objects.
817+
* If code is hard to test -- it probably should be refactored.
773818

774819

775-
Object Oriented Programming:
820+
821+
Material to review for next week
822+
--------------------------------
823+
824+
Next week, we'll get started on Object Oriented Methods. It's a good idea to read up on it first -- so we can dive right in:
776825

777826
* Dive into Python3: 7.2 -- 7.3
778827
http://www.diveintopython3.net/iterators.html#defining-classes
@@ -785,9 +834,9 @@ Object Oriented Programming:
785834

786835
[note that in py3 you don't need to inherit from object]
787836

788-
Talks by Raymond Hettinger:
837+
Talk by Raymond Hettinger:
789838

790-
https://youtu.be/HTLu2DFOdTg
839+
Video of talk: https://youtu.be/HTLu2DFOdTg
791840

792-
https://speakerdeck.com/pyconslides/pythons-class-development-toolkit-by-raymond-hettinger
841+
Slides: https://speakerdeck.com/pyconslides/pythons-class-development-toolkit-by-raymond-hettinger
793842

slides_sources/source/session08.rst

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,17 @@ Write a class for a sparse array:
748748
Homework
749749
========
750750

751+
reading:
752+
753+
Lambda:
754+
755+
http://www.blog.pythonlibrary.org/2015/10/28/python-101-lambda-basics/
756+
757+
https://pythonconquerstheuniverse.wordpress.com/2011/08/29/lambda_tutorial/
758+
759+
Functions as Objects.
760+
761+
751762
.. rst-class:: left
752763

753764
Complete the Circle class

0 commit comments

Comments
 (0)