In: Computer Science
Specification
This script reads in class data from a file and creates a
dictionary.
It then prints the data from the dictionary.
The script has the following functions
class_tuple_create
This function has the following header
def class_tuple_create(filename):
The function reads in a file with four fields of data about a
class.
The fields are
Course ID Room number Instructor Start time
The function must print an error message if the file cannot be
opened for any reason.
The function should read in each line of the file and split it into
a list of 4 elements.
It should add the information in the line into a dictionary, where
the key is the course number and the value is a tuple consisting of
the room number, the instructor and the start time.
The function should return this dictionary when it has read all the
lines in the file.
print_classes
This function has the following header
def print_classes(class_info):
It should print the following table header
ID Room Instr Start
Then it should print out the data for each class in alphabetical
order by Class ID.
The data for each course should align with the labels.
Test Code
The script must contain the following test code at the bottom of the file
class_information = class_tuple_create('xxxxx') class_information = class_tuple_create('courses.txt') print_classes(class_information)
For the test code to work you must copy into your hw3 directory the file from /home/ghoffman/course_files/it117_files.
cp /home/ghoffman/course_files/it117_files/courses.txt .
Suggestions
Write this script in stages, testing your script at each step
Output
The output should look something like this
Error: Unable to open xxxxx ID Room Instr Start ------------------------------- CM241 1411 Lee 13:00 CS101 3004 Haynes 8:00 CS102 4501 Smith 9:00 CS103 6755 Rich 10:00 NT110 1244 Burke 11:00
#--------- hw3.py ----------------
#function that reads a file and returns
#dictionary of courses with courses information in file
def class_tuple_create(filename):
try:
#using with open the file
with open(filename,"r") as f:
#empty dictionary
class_info = {}
#for each line in f
for line in f:
#split the line by whitespace(if you don't passs any parameters)
#it will split by whitespace.
data = line.strip().split()
#if length of data is not 4 don't load that course info.
if(len(data)!=4):
print("Invalid Number of fields in line:",line.strip())
else:
#store the course_id
course_id = data[0].upper()
#store the rest of the tuple
course_data = tuple(data[1:])
#insert into dictinoary
class_info[course_id] = course_data
#return the dictionary
return class_info
except Exception as e:
#if any exception occured return None.
print("Error: Unable to open",filename)
return None
#function to print class info dictionar
def print_classes(class_info):
#if passed dictionary is Not None
if(class_info != None):
#to print the header with some allignment use below >8 means right allign by 8 spaces
print("\n{:<8}\t{:>8}\t{:>8}\t{:>8}".format("ID","Room","Instr","Start"))
#for each key in dictionary
for key in class_info:
#using allignment > or <(left allignment) format the output to print the
#values below the headers.
print("{:<8}\t{:>8}\t{:>8}\t{:>8}".format(key,*class_info[key]))
#testing.
class_information = class_tuple_create('xxxxx')
class_information = class_tuple_create('courses.txt')
print_classes(class_information)