Skip to content

Commit 51b5681

Browse files
refactorinfg the trigrams exercsise
(and a typo in teh TDD page)
1 parent 20b43ed commit 51b5681

9 files changed

Lines changed: 72 additions & 9 deletions

File tree

‎source/class_schedule/lesson04.rst‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,4 +39,6 @@ Exercises:
3939
../exercises/unit_testing.rst
4040
../exercises/dict_lab.rst
4141
../exercises/mailroom-part2.rst
42-
../exercises/kata_fourteen.rst
42+
../exercises/trigrams/trigrams.rst
43+
44+

‎source/exercises/index.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ Lesson 4: Dictionaries, Sets, and Files
4848

4949
dict_lab.rst
5050
file_lab.rst
51-
kata_fourteen.rst
51+
trigrams/trigrams.rst
5252
mailroom-part2.rst
5353

5454

File renamed without changes.
Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Trigrams -- Simple Text Manipulation
55
====================================
66

7-
.. rubric:: Kata Fourteen: Tom Swift Under Milk Wood
7+
.. rubric:: Kata Fourteen: Tom Swift Under the Milk Wood
88

99
Adapted from Dave Thomas's work:
1010

@@ -140,6 +140,54 @@ This assignment has two parts: the key one is the trigrams exercise itself, but
140140

141141
I suggest you write the trigrams part first; it's more interesting :-)
142142

143+
Test Driven Development
144+
-----------------------
145+
146+
You've recently learned about unit testing and Test Driven Development (TDD). That's put that to work on this exercise. Remember that the key to TDD is that you first decide on what you need a piece of your code to do, then you write a test to check that it does that, and only then do you write the actual code itself.
147+
148+
Because you're new to this, we're going to give you some tests to get you started. You'll find them in: :download:`test_trigrams.py <./test_trigrams.py>`, which should be with this assignment.
149+
150+
Running the Tests
151+
.................
152+
153+
To run the tests, use the ``pytest`` test runner: set your working directory the dir with the test file, and run the ``pytest`` command:
154+
155+
.. code-block:: bash
156+
157+
$ pytest
158+
======================= test session starts =======================
159+
platform darwin -- Python 3.8.2, pytest-5.4.3, py-1.8.2, pluggy-0.13.1
160+
rootdir: /Users/chris.barker/Personal/UWPCE/Python210CourseMaterials/source/exercises/trigrams
161+
collected 0 items / 1 error
162+
163+
============================= ERRORS ==============================
164+
________________ ERROR collecting test_trigrams.py ________________
165+
ImportError while importing test module '/Users/chris.barker/Personal/UWPCE/Python210CourseMaterials/source/exercises/trigrams/test_trigrams.py'.
166+
Hint: make sure your test modules/packages have valid Python names.
167+
Traceback:
168+
test_trigrams.py:17: in <module>
169+
import trigrams
170+
E ModuleNotFoundError: No module named 'trigrams'
171+
===================== short test summary info =====================
172+
ERROR test_trigrams.py
173+
!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!
174+
======================== 1 error in 0.13s =========================
175+
176+
You should have gotten something like that error: it is indicating that the "trigrams" module does not exist -- which makes sense, as you haven't written in yet. So the first step is the create your code file: name it ``trigrams.py`` and put it in the same directory as the ``test_trigrams.py`` file. It can be empty for now. Now try running pytest again, and it should get farther: you'll have a lot of test failures, but the test should actually run. You should get something like:
177+
178+
.. code-block:: bash
179+
180+
...
181+
> tris = trigrams.build_trigram(IWISH)
182+
E AttributeError: module 'trigrams' has no attribute 'build_trigram'
183+
184+
test_trigrams.py:29: AttributeError
185+
====================== short test summary info =======================
186+
FAILED test_trigrams.py::test_trigrams_keys - AttributeError: modul...
187+
========================= 1 failed in 0.15s ==========================
188+
189+
You get an Attribute error, as you haven' defined anything in your ``trigrams.py`` file. But now it's time to actually work on the code!
190+
143191
trigrams
144192
--------
145193
@@ -164,6 +212,13 @@ This produces an (ordered) list of words::
164212
165213
Now you've got some words to play with. Once you think you've got it working, try a bit longer piece of text. But this will do for now, and it's small and simple enough that you can immediately see if your code is working.
166214
215+
You will find that example in the test file, so we can write tests agains it:
216+
217+
.. code-block:: python
218+
219+
IWISH = ["I", "wish", "I", "may", "I", "wish", "I", "might"]
220+
221+
167222
The trigrams structure
168223
----------------------
169224

‎source/index.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ Exercises:
198198

199199
exercises/file_lab.rst
200200
exercises/mailroom-part2.rst
201-
exercises/kata_fourteen.rst
201+
exercises/trigrams/trigrams.rst
202202

203203

204204
Modules and Packages

‎source/modules/IteratorsAndGenerators.rst‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ the list -- not so good if memory is tight. Note that they are shallow copies, s
208208

209209
Nevertheless, we can do better:
210210

211-
The ``itertools`` module has a ``islice()`` (iterable slice) function.
211+
The ``itertools`` module has an ``islice()`` (iterable slice) function.
212212
It returns an iterator over a slice of a sequence -- so no more copies:
213213

214214
.. code-block:: ipython
@@ -248,9 +248,11 @@ Usually returns the iterator object itself.
248248
Returns the next item from the container. If there are no further items,
249249
raises the ``StopIteration`` exception.
250250

251+
An *iterable*, on the other hand, must have a ``__iter__`` method that returns an initialized iterator (which may or may not be itself). It doesnot have a ``__next__`` method. So you oculd say that the difference between and iterator and a iterable, is htat iterables do not have ``__next__`` methods: you cannont call ``next()`` on them.
252+
251253

252254
Making an Iterator
253-
-------------------
255+
------------------
254256

255257
A simple version of ``range()``
256258

‎source/modules/TestDrivenDevelopment.rst‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ Test Driven Development
1010
"Testing" is any strategy for making sure your code behaves as expected. "Unit Testing" is a particular strategy, that:
1111

1212
* is easy to run in an automated fashion.
13-
* utilized isolated tests for each individual
13+
* utilized isolated tests for each individual function.
1414

1515
"Test Driven Development" (TDD) is a development strategy that integrates the development of unit tests with the code itself. In particular, you write the tests *before* you write the code, which seems pretty backward, but it has some real strengths.
1616

‎source/modules/Testing.rst‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,9 +263,13 @@ Read the documentation for more information:
263263

264264
http://pytest.org/latest/getting-started.html#getstarted
265265

266-
It will run ``unittest`` tests for you, so can be used as a test runner.
266+
Those docs are a bit intimidating, but with pytest, as they say:
267267

268-
But in addition to finding and running tests, it makes writing tests simple, and provides a bunch of nifty utilities to support more complex testing.
268+
"the easy stuff is easy, and the hard stuff is possible"
269+
270+
-- and you can get very far with the easy stuff.
271+
272+
In addition to finding and running tests, it makes writing tests simple, and provides a bunch of nifty utilities to support more complex testing.
269273

270274
Now download this file:
271275

0 commit comments

Comments
 (0)