Skip to content

Commit 92ba061

Browse files
Everything restructued into Topics. Some cleanup left to do.
1 parent 20784ce commit 92ba061

File tree

19 files changed

+203
-239
lines changed

19 files changed

+203
-239
lines changed

source/exercises/mailroom/mailroom-oo.rst

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,12 +107,13 @@ The main data structure in your class can be a dictionary with a key as donor na
107107

108108

109109
.. code-block:: python
110+
110111
class DonorCollection:
111112
def __init__(self, *donors):
112113
self.donors = {obj.name: obj for obj in donors}
113114
114115
115-
this design allows you to quickly look up donor by their name and get a donor object instance to work with.
116+
This design allows you to quickly look up donor by their name and get a donor object instance to work with.
116117

117118
Another option is to simply use a list of donor objects. You get to choose which you think is more appropriate.
118119

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.. _exercise_mailroom_full_testing:
2+
3+
4+
Mailroom With Full Unit Tests
5+
=============================
6+
7+
You should now have a version of the mailroom program that has complete tests for all the logic code.
8+
That is: every function in mailroom that does not contain a ``print`` or ``input`` call should be tested.
9+
10+
And, critically: every function that contains a ``print`` or ``input`` should contain *no other logic at all*.
11+
12+
But now you've learned about more advanced techniques, like mocking and parameterized tests, so you should be able get your mailroom tests to 100% coverage -- and maybe even make the tests more efficient and complete with some other techniques.
13+
14+
The Task
15+
--------
16+
17+
Use mocking of the ``input()`` function to write a full set of tests for the interactive portion of your mailroom program.
18+
19+
Then run ``coverage`` and make sure everything is covered -- if not, then find those holes and fill them. You should be able to provide a report indicating 100% coverage.
20+
21+
22+
23+

source/exercises/packaging/package_lab.rst

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ LAB: A Small Example Package
1212
- ``at least one working test``
1313

1414

15-
1615
* Here is a ridiculously simple and useless package to use as an example:
1716

1817
:download:`capitalize.zip <capitalize.zip>`

source/index.rst

Lines changed: 81 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -197,42 +197,42 @@ Topics in the Program
197197
198198
199199
200-
Properties and Magic Methods
201-
----------------------------
200+
.. Properties and Magic Methods
201+
.. ----------------------------
202202
203-
.. toctree::
204-
:maxdepth: 1
203+
.. .. toctree::
204+
.. :maxdepth: 1
205205
206-
modules/Properties
207-
modules/StaticAndClassMethods
208-
modules/SpecialMethodsAndProtocols
206+
.. modules/Properties
207+
.. modules/StaticAndClassMethods
208+
.. modules/SpecialMethodsAndProtocols
209209
210-
Exercises:
211-
..........
210+
.. Exercises:
211+
.. ..........
212212
213-
.. toctree::
214-
:maxdepth: 1
213+
.. .. toctree::
214+
.. :maxdepth: 1
215215
216-
exercises/circle/circle_class
217-
exercises/sparse_array
216+
.. exercises/circle/circle_class
217+
.. exercises/sparse_array
218218
219-
Subclassing
220-
-----------
219+
.. Subclassing
220+
.. -----------
221221
222-
.. toctree::
223-
:maxdepth: 1
222+
.. .. toctree::
223+
.. :maxdepth: 1
224224
225-
modules/SubclassingAndInheritance
225+
.. modules/SubclassingAndInheritance
226226
227227
228-
Exercises:
229-
..........
228+
.. Exercises:
229+
.. ..........
230230
231-
.. toctree::
232-
:maxdepth: 1
231+
.. .. toctree::
232+
.. :maxdepth: 1
233233
234-
exercises/html_renderer.rst
235-
exercises/html_renderer_tutorial.rst
234+
.. exercises/html_renderer.rst
235+
.. exercises/html_renderer_tutorial.rst
236236
237237
238238
Multiple Inheritance
@@ -244,102 +244,98 @@ Multiple Inheritance
244244
modules/MultipleInheritance
245245

246246

247-
Introduction to Functional Programming
248-
--------------------------------------
249-
250-
.. toctree::
251-
:maxdepth: 1
247+
.. Introduction to Functional Programming
248+
.. --------------------------------------
252249
253-
modules/OO_vs_functional
254-
modules/Lambda
255-
modules/MapFilterReduce
256-
modules/IPythonParallel
250+
.. .. toctree::
251+
.. :maxdepth: 1
257252
258-
Exercises
259-
.........
253+
.. modules/OO_vs_functional
254+
.. modules/Lambda
255+
.. modules/MapFilterReduce
256+
.. modules/IPythonParallel
260257
261-
.. toctree::
262-
:maxdepth: 1
258+
.. Exercises
259+
.. .........
263260
264-
exercises/trapezoid
261+
.. .. toctree::
262+
.. :maxdepth: 1
265263
264+
.. exercises/trapezoid
266265
267-
Advanced Testing
268-
----------------
269266
270-
.. toctree::
271-
:maxdepth: 1
267+
.. Advanced Testing
268+
.. ----------------
272269
273-
modules/Testing_advanced
270+
.. .. toctree::
271+
.. :maxdepth: 1
274272
273+
.. modules/Testing_advanced
275274
276-
Extras
277-
------
278275
279-
The following are some extra topics that might be of interest
276+
.. Extras
277+
.. ------
280278
281-
.. toctree::
282-
:maxdepth: 1
279+
.. The following are some extra topics that might be of interest
283280
284-
modules/Pep8
285-
modules/CodeReviews
286-
modules/PersistanceAndSerialization
287-
modules/Unicode
281+
.. .. toctree::
282+
.. :maxdepth: 1
288283
289-
modules/Lambda
290-
exercises/lambda_magic
291-
modules/IteratorsAndGenerators
284+
.. modules/Pep8
285+
.. modules/CodeReviews
286+
.. modules/PersistanceAndSerialization
287+
.. modules/Unicode
292288
293-
modules/Closures
289+
.. modules/IteratorsAndGenerators
294290
295-
modules/Decorators
296-
modules/ContextManagers
297-
exercises/context-managers-exercise.rst
291+
.. modules/Decorators
292+
.. modules/ContextManagers
293+
.. exercises/context-managers-exercise.rst
298294
299-
modules/MetaProgramming
295+
.. modules/MetaProgramming
300296
301-
modules/Logging
302-
modules/Debugging
297+
.. modules/Logging
298+
.. modules/Debugging
303299
304300
305-
modules/NoSQL
306-
modules/GraphDatabases
301+
.. modules/NoSQL
302+
.. modules/GraphDatabases
307303
308304
309-
modules/Concurrency
310-
modules/Async
311-
modules/Coroutines
312-
modules/ThreadingMultiprocessing
313-
exercises/threaded_downloader
305+
.. modules/Concurrency
306+
.. modules/Async
307+
.. modules/Coroutines
308+
.. modules/ThreadingMultiprocessing
309+
.. exercises/threaded_downloader
314310
315-
modules/Profiling
311+
.. modules/Profiling
316312
317313
318-
10 Lesson Course Program
319-
========================
314+
.. 10 Lesson Course Program
315+
.. ========================
320316
321-
.. toctree::
322-
:maxdepth: 2
317+
.. .. toctree::
318+
.. :maxdepth: 2
323319
324-
class_schedule/index
320+
.. class_schedule/index
325321
326322
327-
All Topics
328-
==========
323+
.. All Topics
324+
.. ==========
329325
330-
.. toctree::
331-
:maxdepth: 1
326+
.. .. toctree::
327+
.. :maxdepth: 1
332328
333-
modules/index
329+
.. modules/index
334330
335331
336-
All Exercises:
337-
===============
332+
.. All Exercises:
333+
.. ===============
338334
339-
.. toctree::
340-
:maxdepth: 1
335+
.. .. toctree::
336+
.. :maxdepth: 1
341337
342-
exercises/index
338+
.. exercises/index
343339
344340
345341
About this Site

source/modules/Closures.rst

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,29 @@ Closures and Function Currying
66

77
Defining specialized functions on the fly.
88

9+
A "Closure" is a fancy CS term that can be very hard to understand. Partly because they are expressed a little differently in every computer language that supports them But this is easiest definition I could find:
10+
11+
Closure is when a function “remembers” its lexical scope even when the function is executed outside that lexical scope
12+
13+
This definition is provided by Kyle Simpson
14+
(by way of `an article about closures in Javascript <https://medium.com/beginners-guide-to-mobile-web-development/closures-in-functional-programming-and-javascript-3ed730e08fc2>`_).
15+
16+
The basic idea behind the concept of a closure lies in the understanding of the fact that closure is a mechanism by which an inner function will have access to the variables defined in its outer function’s lexical scope even after the outer function has returned.
17+
18+
Which brings us to the key practical part of how this works in Python:
19+
920

1021
Scope
1122
=====
1223

1324
In order to get a handle on all this, it's important to understand variable scoping rules in Python.
1425

15-
"Scope" is the word for where the names in your code are accessible. Another word for a scope is namespace.
26+
"Scope" is the word for `where` the names in your code are accessible. Another word for a scope is namespace.
1627

1728
Global Scope
1829
------------
1930

20-
The simplest is the global scope. This is where all the names defined right in your code file (module) are. When running in an interactavive interpreter, it is in the global namespace as well.
31+
The simplest is the global scope. This is where all the names defined right in your code file (module) are. When running in an interactive interpreter, it is in the global namespace as well.
2132

2233
You can get the global namespace with the ``globals()`` function, but ...
2334

@@ -79,6 +90,9 @@ names are created by assignment, and by ``def`` and ``class`` statements. We alr
7990
test
8091
TestClass
8192
93+
Always keep in mind that in Python, "global" means "global to the module", *not* global to the entire program. In the case of the interactive interpreter, the module is the "__main__" module (remember ``if __name__ == __main__:``?). But in a particular python file (usually one file is one module), the global scope is global to that one file.
94+
95+
8296
Local Scope
8397
-----------
8498

@@ -188,7 +202,7 @@ But any names not defined in an inner scope will be found by looking in the encl
188202
this is in outer
189203
this is in inner
190204
191-
Look carefully to see where each of those names came from. All the print statements are in the inner function, so its local scope is searched first, and then the outer function's scope, and then the global scope. ``name1`` is only defined in the global scope, so that one is found.
205+
Look carefully to see where each of those names came from. All the print statements are in the inner function, so its local scope is searched first, and then the outer function's scope, and then the global scope. ``name1`` is only defined in the global scope, so that one is found. but ``name2`` is redfined in the scope of the ``outer`` function, so that one is found. And ``name3`` is only defined in the ``inner`` function scope.
192206

193207
The ``global`` keyword
194208
----------------------
@@ -218,6 +232,8 @@ Global names can be accessed from within functions, but not if that same name is
218232
219233
The problem here is that ``x += 5`` is the same as ``x = x + 5``, so it is creating a local name, but it can't be incremented, because it hasn't had a value set yet.
220234

235+
How does the interpreter know that ``x`` is a local name? When it compiles the function definition, it marks all the names assigned in the function as local. So when the function runs, it knows that ``x`` is local, and thus it won't go look in the global scope for it.
236+
221237
The ``global`` keyword tells python that you want to use the global name, rather than create a new, local name:
222238

223239
.. code-block:: ipython
@@ -233,12 +249,13 @@ The ``global`` keyword tells python that you want to use the global name, rather
233249
In [42]: x
234250
Out[42]: 10
235251
236-
**NOTE:** The use of ``global`` is frowned upon -- having global variables manipulated in arbitrary other scopes makes for buggy, hard to maintain code!
252+
**NOTE:** The use of ``global`` is frowned upon -- having global variables manipulated in arbitrary other scopes makes for buggy, hard to maintain code! You hardly ever need to use ``global`` -- if a function needs to manipulate a value, you should pass that value into the function, or have it return a value that can then be used to change the global name.
253+
237254

238255
``nonlocal`` keyword
239256
--------------------
240257

241-
The other limitation with ``global`` is that there is only one global namespace, so what if you are in a nested scope, and want to get at the value outside the current scope, but not all the way up at the global scope:
258+
The other limitation with ``global`` is that there is only one global namespace. What if you are in a nested scope, and want to get at the value outside the current scope, but not all the way up at the global scope:
242259

243260
.. code-block:: ipython
244261
@@ -285,7 +302,8 @@ But if we use ``global``, we'll get the global ``x``:
285302
286303
This indicates that the global ``x`` is getting changed, but not the one in the ``outer`` scope.
287304

288-
This is enough of a limitation that Python 3 added a new keyword: ``nonlocal``. What it means is that the name should be looked for outside the local scope, but only as far as you need to go to find it:
305+
This is enough of a limitation that Python 3 added a new keyword: ``nonlocal``.
306+
What it means is that the name should be looked for outside the local scope, but only as far as you need to go to find it:
289307

290308
.. code-block:: ipython
291309
@@ -337,6 +355,7 @@ But it will go up multiple levels in nested scopes:
337355
In [17]: outer()
338356
x in outer is: 20
339357
358+
340359
Closures
341360
========
342361

source/modules/Testing_advanced.rst

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -970,9 +970,6 @@ In your test file, you would do this:
970970
new_mocked_input.return_value = 'blue'
971971
self.assertEqual(mock_input.get_input(), 'blue')
972972
973-
Exercise
974-
........
975973
976-
See if you can use mocking of the ``input()`` function to write a full set of tests for the interactive portion of your mailroom program.
977974
978975

0 commit comments

Comments
 (0)