Due Thursday, October 28th, 3:00 PM
Pair programming in this assignment if you like. You may choose any partner in your lab, or work on your own if you prefer.
No examples are provided for this lab exercise. This is deliberate. Examples are often helpful, but it is important to learn to read specifications very carefully and do exactly what is specified. Examples seldom can convey everything in a specification, so the specification needs to be read carefully in any case. Ask your lab instructor if these instructions are not clear to you.
When you have completed the last exercise above, or 15 minutes before the lab ends, whichever comes first, submit your lab8.py file as lab 8 in Vincent. (The file you submit will reflect your effort on part 3 above, not part 2.) If you have time, start the assignment below.
To motivate this assignment, cut several paragraphs of text from a Word document or web page and paste them into a plain-text editor such as an IDLE edit window. Notice that the result is unusable because all the characters in a paragraph are usually in one long line. This is not a problem in a web browser or word processors such as Word because they automatically wrap long lines by starting new lines at appropriate points where there are spaces.
For display as plain text it is necessary to explicitly change spaces to line breaks in appropriate places so the text fills a given number of columns as much as possible without going past the given number of columns. If the text contains a word (sequence of characters unbroken by a space) that is longer than the column limit, that word should be on a line of its own which cannot help extending beyond the column limit.
Write an application named word2txt.py that satisfies the following concise documentation:
Usage: python word2txt.py [-w width | /w width] infile [outfile]
Copy infile to outfile wrapping long lines for a given maximum line length
(default 80). outfile defaults to standard output.
Perform unit if test is true.In your program use the openFile
function developed in lab and write a function wrapLines that takes an
input and output files as in the replaceTabs function, and a keyword
width argument that specifies the wrapping column width, which defaults to
80. If the application is called with improper usage, print the above concise
documentation and terminate the application. If the file names are bad, the
openFile function will prompt repeatedly for a file name until a good one is
entered. Note that either Unix- or Window-style switches are supported, and they
are optional, and the output file name is optional as well. The square brackets
mean optional, as usual, and the vertical bar means one or the other of the
possibilities on either side of it.
Paragraph breaks are indicated in the input by a new line, and in the output by a blank line.
If there are word breaks consisting of multiple whitespace characters, you may handle this in a variety of ways, such as lines that appear to break prematurely (as in the example below) and reducing multiple whitespace characters to one throughout. But one thing is required: this should never result in an output line that contains only whitespace, for that looks like a blank line, which indicates a new paragraph. A blank line should be output only when going from one input line to the next.
For example, given that the file test.txt contains
a partial test follows:This is a file with_a_very_long_word and extra spaces. Try with a width of 6. A second paragraph.
>python word2txt.py /w 12 test.txt out.txt
>type out.txt
This is a
file
with_a_very_long_word
and
extra
spaces. Try
with a width
of 6.
>python word2txt.py test.txt
This is a file
with_a_very_long_word
and extra spaces. Try with a width of 6.
>python word2txt.py -w 6 test.txt
This
is a
file
with_a_very_long_word
and
extra
spaces.
Try
with a
width
of 6.
A second
paragraph.
>python word2txt.py
Usage: python word2txt.py [-w width | /w width] infile [outfile]
Copy infile to outfile wrapping long lines for a given maximum line width
(default 80). outfile defaults to standard output.
Perform unit if test is true.
Your application's main method should take a keyword test parameter that defaults to false as in the lab application. Use this to include a test of your wrapLines function. You will want to change the call to main at the end of the program to perform this test, but be sure to change it back to a call to main with no arguments before submitting your assignment. (If is fine to leave testing code in a program you submit, but it should always be disabled either by commenting it out or bypassing it as in this case with a test that is false when the program is run as it is submitted.)
When you are done, submit your final word2txt.py file as a8 using Vincent.
Hints: