Introduction to Python

davidbpython.com




Project Discussion, Session 9



9.1 Notes typing assignment. Please write out this week's transcription notes.
 

 

 

PROJECTS 9.2 - 9.5: CONVERTING PROGRAMS TO FUNCTIONS

  • For each project below you will write a function. The function will do work we have done in previous assignments.
  • The input to the function will always be the argument. The argument is supplied by the test code when you run it. (The function does not read from input() or sys.argv).
  • The output from the function will always be returned. (The function does not print anything.)

To write the function:

  • read the description in the docstring
  • start with the argument - what is its expected type and value?
  • consider the return value - what value(s) are expected to be returned from the function?
  • fill in the middle - how can I get from the argument to the return value(s)?
Lastly, you must test your function using the test code provided.
  • Each function comes with additional code.
  • When you are ready to test, run the program.
  • The test code will call your function and you should see the output described in the comments.
Gotchas - please avoid the following:
  • Your function must use variable names that are different from variables outside the function.
  • Your function must work only with the argument passed to it. It does not use input() or sys.argv.
  • Your function must not print anything!. Its values are returned from the function only.

 
9.2 Function get_input_as_int. All you need to do is write a function that takes one argument, converts that value to an int and returns the int. If the argument is not an int value (i.e., not all digits) then the code should raise a ValueError.

Please make special note of the argument and return value specified in the docstring. Make sure your function expects one argument and returns one return value. Make sure not to print the result inside the function. When you are are ready to test, you should be able to run the program and see the results indicated.

 
9.3 Function file_count. This is based on the word count program that was an extra credit in Session 4. It opens the file and counts the number of lines, words and/or characters in the file.

The overall purpose of this assignment is to get you to think of a file as a whole string, a list of strings, and as a list of words. To determine the number of characters in the file, you need only think of the file as a string, and get the string length. To determine the number of lines in the file, you need only think of the file as a list of lines, and get the list length. To determine the number of words in the file, you need only think of the file as a list of words, and get the list length. How can we think of the file as a string? file.read() returns the file contents as a string. We can use len() on a string to see how many characters are in the string. How can we think of the file as a list of lines? str.splitlines() returns the string split on the newline character, i.e. a list of lines. We can use len() on a list to see how many elements are in the list. How can we think of the file as a list of words? str.split() returns a string split on whitespace, i.e. a list of words. We can use len() on the list to see how many words are in the list. Therefore you should not open the file more than once, because both lines and words can be derived from the single string returned by .read(). Function mechanics: instead of taking user input, simply have the function expect the filename and 'items=' as arguments. So your program WILL NOT USE input(). Of course the optional argument items= must have a default value. I suggest making the default None and using the test if not items to see if there is no value.

To see if items is not any of 'c', 'w', 'l' or None, use this idiom:
if items not in ['c', 'w', 'l', None]:
    # raise ValueError here

Please make special note of the argument and return values specified in the docstring. Make sure your function expects one argument and returns one or three return values. Use a return statement to return the value(s). Make sure not to print the result inside the function. When you are are ready to test, you should be able to run the program and see the results indicated. If you get an error, make sure to review the possible errors noted in the assignment.

 
9.4 Function get_lookup_dict. This function takes a filename, reads the file line by line, and builds a dict with the first column of values as the key and the 2nd column of values as the value in the dict.

Please make special note of the argument and return value specified in the docstring. Make sure your function expects one argument and returns one return value. Make sure not to print the result inside the function. So the function starts with the filename argument. You can use some of the code you wrote in the "lookup dict" assignment to build the dict. Loop through the file line by line, split each line into two items, and set the first item as the key and the 2nd item as the value inside the dict. When you are are ready to test, you should be able to run the program and see the results indicated. Make sure not to print the result inside the function. If the second call shows you the items from both pacman_ghosts.csv and the new file, consider how the code is executing, at what point the dict is being updated, and run through the execution of the code in your mind. Obviously when the second call is made, the dict already has the pacman data in it -- why?

 
9.5 Build a module of your functions.

  • Open a new file saved as funclib.py
  • Copy the the 3 functions that you wrote and tested into the file
  • Make sure that funclib.py is in the main Session directory, i.e. in the same directory as test_funclib.py
  • Open the Terminal tab in PyCharm and cd into the session_09_funcs_modules folder
  • At the command line, issue the command pytest.
  • You should see 'good' printed 3 times. Or, you may see an error.
  • If you see an AssertionError, open test_funclib.py and look to the line where the error occurred.
  • The assert() statement is testing to see that the values returned from a function call are as expected. Or, it may be testing to see whether an exception is correctly raised.
  • Given the assert() statement that failed, or exception that was not raised, consider how the test code is using your function and whether or not the function is returning the correct value. Correct your module code so that the test is satisfied.
  • YOU MUST INCLUDE THE RESULTS OF THE TEST RUN IN YOUR SUBMISSION.

 
9.6 Write a sentence or two describing how you'd like to use Python. Go wild!
 
[pr]