A202/I211 Assignment 6

Functions and File I/O

Due Thursday, October 14, 3:00 PM

Individual work this week.

In lab

Recall from class the STAIR steps to problem solving:

    1. State the problem (understand it thoroughly)
    2. Review the Tools you have to solve the problem
    3. Devise an Algorithm
    4. Implement your algorithm
    5. Test and Refine your solution, returning to previous steps as necessary
       

In lab

  1. In a file named lab6.py define a procedure, named convertFC that takes as its argument the degrees in fahrenheit, converts it to degrees celsius, and returns this new value.

    >>> convertFC(80)
    26.666666666666668


    >>> convertFC(32)
    0.0

    The calculation from fahrenheit to celsius is

    C = (5/9)(F - 32) where F = degrees fahrenheit and C = degrees celsius

    Hint: Remember the difference between integer division and float division.

  2. Now that you know your convertFC is working properly, the next step is to create your forecastWeather(fileName) procedure. This procedure will need to do the following items:

    As an example, if the user enters 80 then the program should write the following to the file: Today it will be 80 degrees fahrenheit. That is 36.67 degrees celsius.
    Ah, just right!

  3. Our next step is to read the weather forecast from the file. You will create a procedure readWeatherForecast(fileName). That takes the file name and prints the contents of the weather file. The procedure should raise an error if the file name is not valid. The error message raise will look like the following:

    "error reading file {fileName}"
  4. Our next to last step involves setting up our main procedure to call the correct procedure based on information entered from the command line. The format for running this program is the following:

    python lab6.py forecast-weather file

    The "forecast-weather" system argument will let the program know to call the forecastWeather procedure. The program will then take the "file" file name that will be used as the fileName parameter for the forecastWeather procedure. For example,

    python lab6.py forecast-weather todays-weather.txt

    Will write the weather to todays-weather.txt Similarly, we need to set up the application to accept the following usage:

    python lab6.py read-weather file

    The "read-weather" system argument will let the program know to call the readWeatherForecast procedure. The program will then take the "file" file name that will be used as the fileName parameter for the readWeatherForecast procedure. For example,

    python lab6.py read-weather todays-weather.txt

    Will read the weather from todays-weather.txt
  5. Lastly, we need to add validation to our main procedure to make sure the user has entered in the correct number of system arguments. In both usages of our program, the user must have three system arguments (the lab6.py file name, the read/forecast argument --think of this as an operation argument--, and the filename argument). Validate that the number of system arguments is correct. We also need to validate that the read/forecast argument is correct. The value can either be "forecast-weather" or "read-forecast". If the program encounters any other value then it should print "Error: invalid operation"
M:\lab6>python lab6.py forecast-weather
Usage: python l6.py forecast-weather file
python l6.py read-weather file M
:\lab6>python lab6.py forecast-weather weather.txt What temperature in degrees fahrenheit will it be today? 50 writing to file weather.txt ...done M:\lab6>python lab6.py read-weather Usage: python lab6.py forecast-weather file python lab6.py read-weather file M:\lab6>python lab6.py read-weather weather.txt Today it will be 50 degrees fahrenheit. That is 10.00 degrees celsius. Ah just right!

Hints:

When you have completed the last exercise above, or 15 minutes before the lab ends, whichever comes first, submit your lab6.py file as lab 6 in Vincent.

Assignment

  1. In file reverse.py, define a method reverseString. This method takes in a string as a parameter and returns a string that is the reverse of the input string. This method should display an appropriate error message when the input argument is not a string. Within the IDLE this method should work as follows:

    >>> reverseString("abc")
    'cba'

    >>> reverseString(5)
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "reverse.py", line 14, in reverseString
    raise 'Not a string: ' + str(plainString)
    Not a string: 5


    Hint: Use a for loop.

  2. In the same file, create a method reverseWordChars. This method also takes in a string as a parameter and returns a string that is the reverse of the characters of each word in the input string. This method should display an appropriate error message when the input argument is not a string. Within the IDLE this method should work as follows:

    >>> reverseWordChars("abc def")
    'cba fed'

    >>> reverseWordChars("I went to the store.")
    'I tnew ot eht .erots'


    Hint: While using nested for loops will work, making a call to your reverseString method could greatly reduce the difficulty of this method.

  3. In the same file, create a method reverseWordOrder. This method also takes in a string as a parameter and returns a string in which the order of the words are reversed. This method should display an appropriate error message when the input argument is not a string. Within the IDLE this method should work as follows:

    >>> reverseWordOrder("abc def")
    'def abc'

    >>> reverseWordOrder("I went to the store.")
    'store. the to went I'


    Hint: There is a simple solution that uses both the reverseString and reverseWordChars methods. It does not even need a for loop!

  4. In the same file, create a main method and call to this method at the end of the file that turns the file into an application. The main function should display diagnostic data as it runs. You will find this useful while debugging your method. For this reason you should add these as you go, even if their not formated correctly at first, to help you accurately determine the location of errors in your code. The following code is an example of how diagnostic data can be printed:

print “calling sqrt: ”,
sqrt(4)
print “done”

The applications arguments are a <mode> and two file names. The two file names are an input file followed by an output file. The input file should be read into a string that will be passed to one of your functions. Use read() instead of readline(). The <mode> is either text, word_letters, or word_order.
If the mode is text, your method reverseString should be called with the string read in from the input file.
If the mode is word_letters, your method reverseWordChars should be called with the string read in from the input file.
If the mode is word_order, your method reverseWordOrder should be called with the string read in from the input file.

The returned result of whichever method was called should be both written to the output file and printed on the screen.

In the following sample that is run from the command line, input.txt is the input file, and output.txt is the output file, and input.txt contains: I went to the store

M:\lab6>python reverse.py text input.txt output.txt
Checking access of input.txt... done
Checking access of output.txt... done
Opening input.txt... done
Opening output.txt... done
Reversing Text in input.txt... done
Writing to output.txt... done
Closing input.txt... done
Closing output.txt... done
Output:
erots eht ot tnew I M:\lab6>python reverse.py word_letters input.txt output.txt
Checking access of input.txt... done
Checking access of output.txt... done
Opening input.txt... done
Opening output.txt... done
Reversing Word letters in input.txt... done
Writing to output.txt... done
Closing input.txt... done
Closing output.txt... done
Output:
I tnew ot eht erots M:\lab6>python reverse.py word_order input.txt output.txt
Checking access of input.txt... done
Checking access of output.txt... done
Opening input.txt... done
Opening output.txt... done
Reversing Word Order in input.txt... done
Writing to output.txt... done
Closing input.txt... done
Closing output.txt... done
Output:
store the to went I

Note: The diagnostic messages are formatted with a column width of 50, left aligned, before "done" is printed so that all of the “done” messages line up.

Appropriate error messages should be displayed when any of the following conditions are violated:

  1. The main function should take exactly 3 arguments from the command line.
  2. The input file and output file exist and are readable and writable respectively.
  3. The mode must be either text, word_letters, or word_order.
M:\lab6>python reverse.py input.txt output.txt
Usage: python reverse.py <mode> inFile outFile
where <mode> is either text, word_letters, or word_order
Performs various operations on the characters
and words in a file.
M:\lab6>python reverse.py text testone.txt output.txt Checking access of testone.txt... Error Error: testone.txt is not a readable file
M:\lab6>python reverse.py reverse_the_words input.txt output.txt Checking access of input.txt... done Checking access of output.txt... done Opening input.txt... done Opening output.txt... done Error: reverse_the_words is an invalid mode

Examples of what not to do

When you are done, submit your final reverse.py file as a6 using Vincent.