Generators, Coroutines and Decorators
Due: Friday, June 20th at 11:59PM
Introduction
This lab asks you to use a few of the tools we've recently discussed in
class. The goal is just to help you "Get some dirt under your fingernails".
Generators
- Within a file called generators1.py, write a generator,
generateMultiples(number, start) that can be configured to
generate multiples of number beginning with, and including,
start. Instantiate this generator and use it to print
multiples of 3, starting with 27, up to, but not including 81.
- Within a file called generators2.py, please do the following:
- Implement a generator, wordsFromFile(fileName) that
produces each word of a file, assuming words are space
delimited.
- Implement a generator, noPunctuation(wordGenerator)
that draws words, which possibly include punctuation, from
wordGenerator, and removes any punctuation (commas,
periods, dashes, colons, or semi-colons). It should draw
only one word from wordgenerator and emit only one
word for each request it receives.
- Implement a generator, allCaps(wordGenerator)
that draws words from wordGenerator, and ensures
thata ll letters are capitalized. It should draw
only one word from wordgenerator and emit only one
word for each request it receives.
- Instantiate each of these three generators to form a pipeline
that reads words from a file, removes punctuation, capitalizes
them, and adds them to a Set of words. It should then
print the set of words.
- Within a file called coroutines1.py, write a coroutines,
capitalize that capitalizes and prints each word it is sent.
Instantiate this coroutine, and test it with the phrase,
"The quick brown fox jumped over the lazy old dog."
- Within a file called coroutines2.py, repeate the exercise
described for generators2.py, but use a pipeline of
coroutines. In effect, the "first" corroutine in the pipeline
will strip punctuation and the last will print the word. You can
read the words from the file however you'd like -- including
by using the generator you wrote earlier
- Within a file called decorators.py, write two decorators,
one, nonNegative which assumes an arbitrarily long list of
integer arguments and throws an Exception if any is less than 0,
and another, allCaps, which assumes an arbitrarily long list
of string arguments, and capitalizes them. Then, write a simple
function to test each, wrap it, and demonstrate that each decorator
works.