You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: source/exercises/mailroom/mailroom_with_tests.rst
+10-4Lines changed: 10 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,11 @@ Guidelines
19
19
20
20
Here are some suggestions on what should be refactored in your mailroom code.
21
21
22
-
As mentioned above, testing user interaction code (code with ``print`` and ``input`` functions) is harder than testing the rest of your code. Testing user interaction code requires more advanced unit testing methodologies that will be revisited in future courses. Therefore, you should refactor your code so that the user interaction code contains as little business logic as possible. It should only interact with the user either by asking them for input or by responding to their request to print out data. Separating business logic from user interaction code is a good practice in general and we will come back to this concept in later lessons.
22
+
As mentioned above, testing user interaction code (code with ``print`` and ``input`` functions) is harder than testing the rest of your code.
23
+
Testing user interaction code requires more advanced unit testing methodologies that will be revisited in future courses.
24
+
Therefore, you should refactor your code so that the user interaction code contains as little business logic as possible.
25
+
It should only interact with the user either by asking them for input or by responding to their request to print out data.
26
+
Separating business logic from user interaction code is a good practice in general and we will come back to this concept in later lessons.
23
27
24
28
The refactoring in this lesson will allow you to unit test the functions with business logic, even if you don't test the user interaction code.
25
29
@@ -68,7 +72,9 @@ Send Letters
68
72
............
69
73
70
74
This function should require very little or no change to make it unit-testable.
71
-
To make it testable, you'll need to follow the same "separation of concerns" approach: the code that creates the letters should be separate from the code that prints them to the screen. This both allows you to test the letter creation, and leaves the door open to do something else with the letters: save to to a file, send as an email, etc. So the code that makes a letter likley will return a string with the entire letter contents.
75
+
To make it testable, you'll need to follow the same "separation of concerns" approach: the code that creates the letters should be separate from the code that prints them to the screen.
76
+
This both allows you to test the letter creation, and leaves the door open to do something else with the letters: save to to a file, send as an email, etc.
77
+
So the code that makes a letter likely will return a string with the entire letter contents.
72
78
73
79
For example:
74
80
@@ -83,15 +89,15 @@ For example:
83
89
expected ="Frank, thanks a lot!"
84
90
assert get_letter_text("Frank") == expected
85
91
86
-
Note that some thought should go into the test of the letter. IF it's really simple, then checking comparing to a full letter is OK. But it might be better to test the important parts of the letter: Does it contain the correct name? does it contain the right amounts of money? rather than the entire text.
92
+
Note that some thought should go into the test of the letter. If it's really simple, then simiply comparing to a full letter is OK. But it might be better to test the important parts of the letter: Does it contain the correct name? does it contain the right amounts of money? rather than the entire text.
87
93
88
94
When you are done, every function in mailroom that does not contain a ``print`` or ``input`` call should be tested.
89
95
90
96
And, critically: every function that contains a ``print`` or ``input`` should contain *no other logic at all*.
91
97
92
98
Yes, that does mean that that you'll have some very simple functions like:
Let's look at each of the four tuple elements in turn:
30
33
@@ -61,7 +64,7 @@ So you need to find a string formatting operator that will "pad" the number with
61
64
Task Two
62
65
--------
63
66
64
-
Using your results from Task One, repeat the exercise, but this time using an alternate type of format string (hint: think about alternative ways to use .format() (keywords anyone?), and also consider f-strings if you've not used them already).
67
+
Using your results from Task One, repeat the exercise, but this time use an alternate type of format string (hint: think about alternative ways to use .format() (keywords anyone?, or indexes?), and also consider f-strings if you've not used them already).
65
68
66
69
67
70
Task Three
@@ -87,7 +90,7 @@ Hint: You can pass in a tuple of values to a function with a ``*``:
87
90
88
91
The idea here is that you may have a tuple of three numbers, but might also have 4 or 5 or 2 or....
89
92
90
-
so you can dynamically build up the format string to accommodate the length of the tuple.
93
+
So you can dynamically build up the format string to accommodate the length of the tuple.
91
94
92
95
The string object has the ``format()`` method, so you can call it with a string that is bound to a name, not just a string literal. For example:
93
96
@@ -187,7 +190,7 @@ or
187
190
188
191
189
192
Task Six
190
-
---------
193
+
--------
191
194
Often it's convenient to display data in columns. String formatting helps to make this straightforward.
192
195
193
196
Suppose you'd like to display something like:
@@ -206,7 +209,7 @@ Then you will need to use alignment specifiers. Do some research on this using t
206
209
207
210
* Write some Python code to print a table of several rows, each with a name, an age and a cost. Make sure some of the costs are in the hundreds and thousands to test your alignment specifiers.
208
211
209
-
* And for an extra task, given a tuple with 10 consecutive numbers, can you work how to quickly print the tuple in columns that are 5 charaters wide? It can be done on one short line!
212
+
* And for an extra task, given a tuple with 10 consecutive numbers, can you work how to quickly print the tuple in columns that are 5 characters wide? It can be done on one short line!
Put the file in your student directory in a new subdirectory named for this lesson, and add it to your clone early.
233
+
234
+
Add the file to the develop brnach of your repo for this excercise.
231
235
232
236
Make frequent commits with good, clear messages about what you're doing and why.
233
237
234
-
When you're done and ready for the instructors to review your work, push your changes to your gitHub fork and then go to the gitHub website and make a pull request.
238
+
When you're done and ready for the instructors to review your work, push your changes to gitHub fork and then go to the gitHub website and make a pull request.
235
239
236
-
Copy the gitHub link to the pull request, and use the +Submit Assignment link located in the top right corner to submit the URL here.
240
+
Copy the gitHub link to the pull request, and provide it to the insstructors when you submit it in your LMS.
0 commit comments