IT 117: Introduction to Scripting
Homework 5

Due

Sunday, February 26th at 11:59 PM

Deliverables

There is one deliverable for this assignment

Make sure the script obeys all the rules in the Script Requirements page.

Specification

The script must have 4 functions:

get_filenames

This function must have the following header

def get_filenames():

The function should check that number of command line arguments.

If there are fewer than 2 command line arguments, it should print an error message and exit the script.

If there are at least 2 command line arguments it should return the first two command line arguments.

open_file_read

This function must have the following header

def open_file_read(filename):

It must try to create a file object for reading on the file whose name is given by the parameter filename.

If it is succesfull in creating the file object it should return the object.

If it is cannot create the object it should print an error message and return None.

set_from_file

This function must have the following header

def set_from_file(file):

The function should loop through a text file with one word on each line.

It should add all words to a set and return the set.

print_ordered_set

This function must have the following header

def print_ordered_set(s):

This function should print all entries in a set in sorted order.

Script for this assignment

Open an a text editor and create the file hw5.py.

You can use the editor built into IDLE or a program like Sublime.

Test Code

Your hw5.py file must contain the following test code at the bottom of the file

filename_1, filename_2 = get_filenames()
print(filename_1, filename_2)
file_1 = open_file_read(filename_1)
file_2 = open_file_read(filename_2)
if file_1 and file_2:
	set_1 = set_from_file(file_1)
	set_2 = set_from_file(file_2)
	print(len(set_1))
	print(len(set_2))
	print(len(set_1 | set_2))
	print(len(set_1 & set_2))
	print_ordered_set(set_1 & set_2)

Suggestions

Write this program in a step-by-step fashion using the technique of incremental development.

In other words, write a bit of code, test it, make whatever changes you need to get it working, and go on to the next step.

  1. Create the file hw5.py.
    Write a statement to import the sys module.
    Enter the headers for get_filenames, set_from_file and print_ordered_set.
    Under each header write the Python statement pass.
    Run the script.
    Fix any errors you find.
  2. Replace the pass statement in get_filenames with an if statement that checks that the number of command line arguments is at least two.
    If not the script should print an error message and exit the script.
    See this section of Class Notes 10 for an example on how to do this.
    If there are two command line arguments, return both.
    Copy the two lines of the test code into the bottom of the script.
    Run the script with 0, 1 and 2 command line arguments.
    You should see an error message with 0 0=or 1 command line arguments.
    Fix any errors you find.
  3. Copy the function open_file_read from your hw2.py.
    Copy the first two lines of the test code into the bottom of the file.
    Run the script entering both a real filename and the name of a file that does not exists.
    Copy the next two lines of the test code to the bottom of the script.
    Run the script with 2 bad filenames, line "xxxxx" and "yyyy", as command line arguments.
    You should see error messages.
    Now run the script with "words_1.txt" and "words_2.txt" as command line arguments.
    Fix any errors you find.
  4. Remove the pass statement from set_from_file.
    Write a statement that defines the empty set s.
    Print s.
    Copy the next three lines in the test code, the lines starting with the if statement, to the bottom of the script.
    Run the script with "words_1.txt" and "words_2.txt" as command line arguments.
    Fix any errors you find.
  5. Remove the print statement from set_from_file.
    Write a for loop that prints each line in the file.
    Run the script with "words_1.txt" and "words_2.txt" as command line arguments.
    Fix any errors you find.
  6. Remove the print statement after the for loop in set_from_file.
    Replace it with a statement adds the line, with the linefeed character removed, to the set s.
    Outside the for loop, print the set s.
    Run the script with "words_1.txt" and "words_2.txt" as command line arguments.
    Fix any errors you find.
  7. Remove the print statement from the last line of set_from_file.
    Replace it with a line that returns the set s.
    Remove the pass statement from print_ordered_set .
    Replace it with a statement that print the parameter s.
    Copy the last lines of the test code to the bottom of the script.
    Run the script with "words_1.txt" and "words_2.txt" as command line arguments.
    Fix any errors you find.
  8. Remove the print statement from print_ordered_set.
    Replace it with a for loop that prints the sorted list of elements in the set.
    Run the script with "words_1.txt" and "words_2.txt" as command line arguments.
    Fix any errors you find.

Testing on Your Machine

Unix Setup

Copy the file to Unix

Testing the script on Unix

Copyright © 2020 Glenn Hoffman. All rights reserved. May not be reproduced without permission.