Handling an arbitrary number of appliances

In our initial version of the appliance project we had 4 types of appliances. For each one, we created an instance of the class Appliance and assigned values to the instance variables.  In a more realistic setting we have many appliances and information about will be stored in a file.  We generalize the project in this direction.  In order to do so, we need to discuss how to read data from a text file.

The appliance store records all the appliance information in a text file. See sales_data.txt for an example. The file contains a heading line and then  the information about  8  appliances, one appliance per line.  We don’t want to  create the instances one by one!  We will use a for-loop to create the instances and  code making the assignments will appear only once.  To do this, we  use the  Python file object to read the text file and we create all the appliance instances using  a for- loop. More information about Python file object can be found at  https://docs.python.org/2/tutorial/inputoutput.html#methods-of-file-objects.

Step 1 – Use open() function to open a file
The open() function returns a file object, and is most commonly used with two arguments: open(filename, mode). Mode can be ‘r’ when the file will only be read, ‘w’ for only writing. The mode argument is optional; ‘r’ will be assumed if it’s omitted. In Canopy, you need to check “Keep Directory Synced to Editor” when you open a file in the same directory as your program. The menu is at the right middle section of your screen and shown below.  If this is not done, an I/O error “No such file or directory” is produced.

filefile_directory

## read sales data file
## please put the sales_data.txt in the same folder as your program
sales_data = open('sales_data.txt', 'r') # The second argument 'r' means the file will only be read
## read the first line which is the titles
## you may use it to create a more readable table of commissions
titles = sales_data.readline()

Step 2 – Use a for loop to read the appliance data
We first create an empty list called appliance_list, which will store all the Appliance instances. The list is created by reading lines from a file object and we  use a for loop to read through the file line by line.  We use the split() method which splits the string by spaces and produces a list  containing all the information of the line. For more information about Python string.split(), please see https://docs.python.org/2/library/string.html?highlight=split#string.split.   As all the items in the list are strings, we  need to convert data correctly into the correct type needed.

appliance_list = []
## For reading lines from a file, you can loop over the file object.
## We use a for loop to read the left lines in the file
for line in sales_data:
    attributes = line.split() ## split the line by space
    ## create the appliance instance
    app = Appliance()
    app.type = attributes[0]
    app.price = int(attributes[1])
    app.minimum_quantity = int(attributes[2])
    app.commission_rate = float(attributes[3])
    app.bonus_minimum = int(attributes[4])
    app.bonus = int(attributes[5])
    ## add the appliance to appliance list
    appliance_list.append(app)

Step 3 – Read the salesperson data
We  create an empty list called quantity_list to store the input provided by a user on one salesperson.  We use a for loop to iterate over the appliance_list and ask the user to input the quantity sold of each appliance. Because the raw_input() function always returns a string, we need to convert the input into integer using  int().

quantity_list = [] ## define an empty list to store the input integers
## read the salesperson data with raw_input() function
## the number of quantities needs to be input is based on the number of appliances we have
for app in appliance_list:
    quantity = int(raw_input("The quantity of " + app.type +": "))
    quantity_list.append(quantity)

Step 4 – Iterate the salesperson data list and print out the commission
We can use the print_commission() method to print the commission received for each appliance. In the code below we use the get_commission() method of class Appliance to calculate the commission for every appliance and compute  the total commission  the salesperson receives.

## Calculate and print the commission with instance methods
print "Commission of the Salesperson"
print "--------------------------------------"
total_commission = 0
for i in range(len(appliance_list)):
    quantity = quantity_list[i]
    appliance = appliance_list[i]
    total_commission = total_commission + appliance.get_commission(quantity)
    appliance.print_commission(quantity)

print 'Total Commission:', total_commission

Download the program and data file