Skip to content

Commit 7cd8fb5

Browse files
updating rendered materials
1 parent 7de5834 commit 7cd8fb5

36 files changed

Lines changed: 7379 additions & 148 deletions

‎_sources/exercises/index.rst.txt‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ Lesson 3: Booleans, Sequences, Iteration, and Strings
3636
slicing.rst
3737
list_lab.rst
3838
string_formatting.rst
39-
mailroom-part1.rst
40-
mailroom_tutorial.rst
39+
mailroom/mailroom.rst
40+
mailroom/mailroom_tutorial.rst
4141

4242

4343
Lesson 4: Dictionaries, Sets, and Files
@@ -49,7 +49,7 @@ Lesson 4: Dictionaries, Sets, and Files
4949
dict_lab.rst
5050
file_lab.rst
5151
trigrams/trigrams.rst
52-
mailroom-part2.rst
52+
mailroom/mailroom_with_dicts.rst
5353

5454

5555
Lesson 5: Exceptions, Modules, and Comprehensions
@@ -60,8 +60,9 @@ Lesson 5: Exceptions, Modules, and Comprehensions
6060

6161
exceptions_lab.rst
6262
except_exercise.rst
63+
mailroom/mailroom_with_exceptions.rst
6364
comprehensions_lab.rst
64-
mailroom-part3.rst
65+
mailroom/mailroom_with_comprehensions.rst
6566

6667

6768
Lesson 6: Unit Testing and Advanced Argument Passing
@@ -72,7 +73,7 @@ Lesson 6: Unit Testing and Advanced Argument Passing
7273

7374
unit_testing.rst
7475
args_kwargs_lab.rst
75-
mailroom-part4.rst
76+
mailroom/mailroom_with_tests.rst
7677

7778

7879
Lesson 7: Intro to Object Oriented Programing
@@ -100,7 +101,7 @@ Lesson 9: Multiple Inheritance
100101
.. toctree::
101102
:maxdepth: 1
102103

103-
mailroom-oo.rst
104+
mailroom/mailroom-oo.rst
104105

105106

106107
Lesson 10: Catch up
@@ -124,17 +125,17 @@ The following are not required, but can be useful learning tools. Feel free to t
124125

125126
context-managers-exercise.rst
126127

127-
mailroom-pkg.rst
128+
mailroom/mailroom-pkg.rst
128129

129130
trapezoid.rst
130131

131132
context-managers-exercise.rst
132133

133-
mailroom-decorator.rst
134+
mailroom/mailroom-decorator.rst
134135

135-
mailroom-meta.rst
136+
mailroom/mailroom-meta.rst
136137

137-
mailroom-mock.rst
138+
mailroom/mailroom-mock.rst
138139

139140
threaded_downloader.rst
140141

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
.. _exercise_mailroom_decorator:
2+
3+
4+
Mailroom -- Decoratoring it
5+
===========================
6+
7+
We want to know who is making changes to our database, so make a decorator that will ask for a user's name, before making any changes to our donor list. If you want to get really fancy, figure out how to cache the answer, so you don't have to ask more than once per session.
8+
9+
The Goal
10+
--------
11+
12+
Think about which methods will affect the database. Will sending a letter add a user? (Should it? Maybe this is a good time for refactoring.) These are the methods that should get decorated. The decorator should get the user's name. Eventually, we will want to store this permanently, but that is for a different day. However, you might want to figure out how to store it temporarily, so you don't repeatedly ask the same user for their name. This could be done on a timeout basis, or you could assume that the user only changes when the interface is instantiated.
13+
14+
15+
16+
17+
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
:orphan:
2+
3+
.. _exercise_mailroom_fp:
4+
5+
#####################
6+
Mailroom - Functional
7+
#####################
8+
9+
Making Mailroom Functional
10+
11+
or: Adding a few functional features :-)
12+
13+
A new approach
14+
==============
15+
16+
It was reasonable to build the simple MailRoom program using a single module, a simple data structure, and functions that manipulate that data structure. It was reasonable to refactor Mailroom around classes when it started to become unwieldy with all of the features we wanted to add to it.
17+
18+
Due to the success of Mailroom and the good PR it has generated with all its thank you letters, the donations are pouring in. This has raised the attention of wealthy philanthropists who are willing to contribute matching funds. Our problem now is to expand Mailroom so that it can run projections and account for matching funds offered by philanthropists. Management is tremendously happy with us and has offered to support our expansion of Mailroom toward a distributed architecture capable of running calculations on matching funds across the donor database in parallel.
19+
20+
This is a good candidate for a functional programming approach.
21+
22+
Updating the code
23+
=================
24+
25+
Preparation
26+
-----------
27+
28+
Mailroom is our longest running exercise. At this point you may have several versions of the program in separate files or indeed many versions of the program within the same python source file with some sections commented in and others commented out.
29+
30+
In terms of Mailroom's developmental chronology the original exercise added features over the course of several weeks. A major branch likely occurred when refactoring around object orientation. Another major branch may be occurring here as we investigate functional programming concepts. Is it starting to feel disorganized and a little out of control? Welcome to the world of real software development.
31+
32+
If you are familiar with git as we have been using it so far, it may be a good time to "branch out" (pun intended), and start using git's branching features.
33+
34+
On the other hand, if git is still painful for you, feel free to simply make a copy of your currently working mailroom code (and tests) in a new directory.
35+
36+
IF you want to try branching, there are some notes here: :ref:`git_branching`
37+
38+
Making new files
39+
----------------
40+
41+
If you don't want to mess around with branching:
42+
43+
You will need to come up with a good naming convention for your source files. You could consider separate source file names all within the same directory like this:
44+
45+
46+
.. code-block:: bash
47+
48+
mailroom/mailroom.py
49+
mailroom/mailroom_oo.py
50+
mailroom/mailroom_fp.py
51+
52+
Or separate directories for each of your versions like this:
53+
54+
.. code-block:: bash
55+
56+
mailroom/mailroom.py
57+
mailroom_oo/mailroom.py
58+
mailroom_fp/mailroom.py
59+
60+
Regardless of how you decide to manage names and track versions you may also need to decide where to start on this exercise. Should you start from your most recent version which likely involves classes or do you back out to an earlier version before we studied object orientation? Another option is to start again from scratch. The choice is yours.
61+
62+
63+
Map, Filter, Reduce
64+
-------------------
65+
66+
1. Add a new feature to Mailroom using ``map`` so that each donation on record can be doubled, tripled or indeed multiplied by any arbitrary factor based on the whims of philanthropists who would like to support our cause.
67+
68+
This will require a new function (or method in your donor database class) called ``challenge(factor)`` that takes a multiplier ``(factor)``, and multiplies all the donations of all the donors by the factor. The function returns a NEW donor database, with the new data.
69+
70+
2. Add a new feature to Mailroom using filter so that donations either above or below a specified dollar amount are included in the map operations of #1 above.
71+
72+
You can do this by adding ``min_donation`` ``and max_donation`` optional keyword parameters to your ``challenge`` function. You'll want to filter the donations before passing them to map.
73+
74+
3. Refactor the new features outlined in #1 and #2 above such that they can be used to run projections. Imagine the following scenario. You are an account manager out in the field meeting with philanthropists and talking with them about the many ways they might structure their matching contributions. You would like a feature that could show them, based on past contributions, what their total contribution would become under different scenarios. For instance, based on donations in the current database, show them (a) what their total contribution would come to in dollars if they were to double contributions under $100. And then (b) show them what their total contribution would come to if they were to triple contributions over $50.
75+
76+
This may require another option in your menu-driven interface.
77+
78+
Use ``map``, ``filter`` and either ``sum`` or ``reduce`` to accomplish the goals above.
79+
80+
81+
Distributed Processing
82+
----------------------
83+
84+
Map, filter and reduce lend themselves to parallel, distributed programming. Indeed algorithms that lend themselves gracefully to map/filter solutions tend to lend themselves equally well to parallel processing. We have a name for it: `embarrassingly parallel`_.
85+
86+
Our next expansion of Mailroom stands on the shoulders of the work you did with map, filter and reduce. We are going to take advantage of the embarrassingly parallel property of map/filter algorithms to run map operations in parallel.
87+
88+
We have many avenues open to us in terms of setting up the back-end infrastructure for this exercise. In lieu of a supercomputer cluster, which we could "easily" spin up on any of several cloud computing services, we are going to use `IPython Parallel`_. Note that we could spin up IPython Parallel across a cluster of machines, but for the purposes of this exercise running it locally will suffice.
89+
90+
To get started follow the instructions in this :ref:`ipyparallel_quickstart`.
91+
92+
Look now at the code you created for the map, filter, reduce exercise above. Focus on the map operations. Your task is to replace the map operations using one of IPython Parallel's `apply`_ functions. For this exercise ``apply_sync()`` may be your most straightforward option. For our purposes Python's built-in ``map`` and IPyParallel's ``apply`` are conceptually identical: they both apply a function to all the elements of a data structure.
93+
94+
.. _embarrassingly parallel: https://en.wikipedia.org/wiki/Map_(parallel_pattern)
95+
.. _IPython Parallel: https://ipyparallel.readthedocs.io/en/latest/
96+
.. _apply: http://ipyparallel.readthedocs.io/en/6.0.2/multiengine.html?highlight=apply_sync#calling-python-functions
97+
98+
99+
Closures
100+
--------
101+
102+
**Do this only after you learn about closures next class!**
103+
104+
Closures are another functional programming strategy. They allow you to create functions according to parameters that are known only at runtime and not beforehand.
105+
106+
Your task now is to replace the functions that you have been passing to ``map()`` or ``apply()`` with a closure.
107+
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
.. _exercise_mailroom_meta:
2+
3+
4+
Mailroom -- metaprogramming it!
5+
===============================
6+
7+
So far, your mailroom program may not have any way to save or re-load the donor data. Some of you may have added code to save and load the data in a text file or JSON, but even if you have, you might want a more flexible and extensible system once your data gets more complicated.
8+
9+
10+
JSON
11+
----
12+
13+
`JavaScript Object Notation (JSON) <https://www.json.org/>`_ is a format borrowed from the Web -- Javascript being the de-facto scripting language in browsers. It is a great format for communicating with browsers, but it has become a common serialization format for many other uses: it is simple, flexible, and human-readable and writable.
14+
15+
It also maps pretty much directly to (some of) the core Python datatypes: lists, dictionaries, strings, and numbers.
16+
17+
So JSON is a nice way to save data for a program like mailroom.
18+
19+
Goal
20+
----
21+
22+
Your goal is to use a JSON-save system started in the Metaprogramming Lesson (:ref:`metaprogramming`) to make your model classes saveable and loadable as JSON.
23+
24+
You can download the package here:
25+
26+
:download:`json_save.zip </examples/metaprogramming/json_save.zip>`
27+
28+
And it may also be in your class repo solutions dir:
29+
30+
``solutions/metaprogramming/json_save/``
31+
32+
You can use either the decorator-based or meta-class based approach.
33+
34+
You may need to extend the JSON-save module a bit to make it work for you!
35+
36+
When you are done, your class that holds the database of donors and their data should have ``save`` and ``load`` methods that will, naturally, save and load the entire dataset.
37+
38+
**Make Sure it's Tested!**
39+
40+
41+
42+
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
.. _exercise_mailroom_mocking:
2+
3+
Mocking Mailroom
4+
================
5+
6+
You should now have a nice mailroom program, complete with an object-oriented structure and bundled up into a nice python package.
7+
8+
And a complete set of unit tests for the logic code.
9+
10+
But your command line user interface code is likely not tested. It's hard to auto-test user-interaction...
11+
12+
Your mission is to get mailroom fully tested.
13+
14+
Start with your object oriented mailroom in a proper python package.
15+
16+
Coverage
17+
--------
18+
19+
First run coverage on your current tests. I like pytest-cov:
20+
21+
.. code-block:: bash
22+
23+
$ pip install pytest-cov
24+
25+
$ pytest --cov=mailroom
26+
27+
or, to get the nifty html output:
28+
29+
.. code-block:: bash
30+
31+
$ pytest --cov=mailroom --cov-report html
32+
33+
That will result in a pile of html in an ``htmlcov`` directory -- point your browser to the index.html file in there, and click away...
34+
35+
Once you've run coverage -- add tests to get it up close to 100% on your logic code, if it's not there already.
36+
37+
Fixtures
38+
--------
39+
40+
Fixtures are a really good way to make your tests cleaner and more independent. With mailroom, you should have a couple fixtures that set up a donor database with some data in it -- and maybe one or two for ``Donor`` objects.
41+
42+
Clean up your tests with fixtures -- and keep the coverage up!
43+
44+
One possible use for a fixture is providing a file to write to for the code that writes files. Or maybe a directory to put the files in, and then it can clean up the dir in teardown.
45+
46+
47+
Mocking input
48+
-------------
49+
50+
Once you have 100% coverage for the logic code -- it's time to test the UI.
51+
52+
You should be able to use mocking to mock the ``input`` function, and then actually test your user interface code, too.
53+
54+
Can you get 100% test coverage?
55+
56+
57+

0 commit comments

Comments
 (0)