Project 6 Designing with Functions

In this project you'll redesign project 5 to use functions.

Exercises

Turn in Exercises 18, 19, 20 and 21 with this project.

Refactoring Project 5

In programming refactoring is when you change code to improve it's functionality or readability. In this project you will refactor project 5 to use functions. Before you start make sure that your project 5 code works reliably. The best way to refactor is to make small changes, then retest your code to make sure the changes didn't break anything.

Begin with the following code:

import sys 
 
def main(): 
    pass
 
if __name__ == '__main__' :
    prog, madfile = sys.argv 
    main()

Start by taking all of the code from your project 5 Mad Lib generator and moving it into the main() function. Retest your Mad Lib generator to make sure it still works after you move code into main(). Once you're satisfied that your program still works add the following functions:

  • read_madlib_file
    • This function reads the Mad Lib file and returns data from it.
    • Arguments (one total):
      • The name of a file to read
    • Returns values (four total):
      • The Mad Lib template
      • The three word types
  • do_madlib_input
    • This function prompts the user for the words in the Mad Lib file and returns them to the caller
    • Arguments (three total):
      • The three word types
    • Return values (three total):
      • The three words input by the user
  • display_madlib
    • This function displays the Mad Lib and saves the output to a file
    • Arguments (two total)
      • The completed Mad Lib
      • The name of the file to save
    • The function doesn't return any values.

Write your functions one at a time and retest your code after each one. Be sure to close all files. Each function must have a docstring that describes the function, its arguments and return values where applicable.

Testing Your Program

With your code factored into functions you can safely import your project as a module. That's an excellent way to test your functions (and it's how my grader works). Use the Python REPL to test your code. The example shows what the correct arguments and return values for each function should look like. Be sure that you can call your code this way:

$ python3.6 
Python 3.6.2 (default, Nov  6 2017, 15:08:10) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
>>> import project6
>>>
>>> project6.read_madlib_file('madlib_sample2.txt') 
('The {} fox {} over the lazy {}.\n', 'adjective\n', 'verb\n', 'noun')
>>>
>>> project6.do_madlib_input('verb', 'adjective', 'noun')
Please enter a(n) verb: run
Please enter a(n) adjective: fast
Please enter a(n) noun: Mike
('run', 'fast', 'Mike')
>>>
>>> project6.display_madlib('This is a fun, complete madlib', 'output_filename.txt')
This is a fun, complete madlib
>>> 
>>> with open('output_filename.txt') as f :
...   print (f.read())
... 
This is a fun, complete madlib
>>> 

Notice at the end I open the complete madlib file and print it out. Pay special attention to the fact that the filename is exactly the same as the name given to the function. This means you should change the name outside of the display_madlib() function.

Requirements

  • There should be no code outside of a function except:
    • Any import statements you need
    • The if __name__ == "__main__" statements
  • Your code should have the functions listed, and they must be named exactly as above
    • Each function must have a docstring that describes it
    • Each function must have the described arguments and return values
  • Your code must be in a file name project6.py

Advanced

This week you'll take an important step to getting your code on a full-scale hosting platform, Google App Engine. App Engine only supports Python 2.7 and you'll need to use the virtualenv system to create a Python 2.7 environment that can be packaged and sent to App Engine. Also, you'll need to make sure that your web app works well with both Python 2 and Python 3. Luckily, with Flask it's relatively easy.

  • Start by following the instructions in the class lecture for setting up a virtual environment for Python 2.7.
  • Install Flask in your virtual environment
  • Port your code so that it works properly with Python 2.7
    • You will need to put from __future__ import print_function at the top of your Python file to get Python 3's print function.
    • You can't use f-strings (use format() instead)
  • Once you're satisfied that your new project works use pip freeze to generate your requirements file.

Submit the following for the advanced project:

  • project6_adv.py
  • requirements.txt

Your project will be run using Python 2.7 so it be sure you got your virtual environment right!

Grading

  • 5 points for exercises
  • 5 points for a correct submission
  • 10 points for the functionality of your program