USING PYTHON complete the template below such that you will
USING PYTHON, complete the template below such that you will provide code to solve the following problem:
Please write and/or extend the project5 attached at the end.
You will have to do the following for this assignment:
Create and complete the methods of the PriceException class.
Create the processFile function.
Modify the main function in project5.py. YOU NEED TO MODIFY THE PROJECT INCLUDED AT THE END AND USE THE INCLUDED DRIVER TO TEST YOUR CODE TO MAKE SURE IT WORK AS EXPECTED. Thanks
You will be creating a PriceException class that extends the Exception class with the following methods. Later, you will modify your main method to use this class. Write the following methods for the PriceException class:
A constructor that takes a message as an argument. The constructor creates this attribute, and sets it to the incoming argument. The constructor also initializes an empty list of items.
A to-string method that can be called with str(...), which returns the message of the exception.
You will write a function called processFile:
You will write a function called processFile that takes as argument a filename, and opens that file and returns its contents, as a list of strings.
Finally, modify your main:
Take two arguments: the original argument of a list of strings, as well as a list of integers. The list of integers corresponds to how many items each shipment is allowed to have; if there are more items in the first argument than allowed by the second argument, the extra items are not included in that shipment and are discarded.
You may modify the classes you wrote as necessary.
Raise a PriceException (the new class you just wrote) when there is something wrong with just the price read in; otherwise, if the price is right (haha!) but the item is still wrong, continue to raise anItemException.
Testing Your Code
When you are ready, right-mouse-click to save the file as driver.py to the same directory as your project5.py. This driver already contains the tests, and will tell you which test number you\'re failing; look at the source code of the driver to see the details of that test. You can run the driver through the terminal with the command: python driver.py
The driver is as follows:
from project5 import *
import traceback
import subprocess
def checkEqual(shipments1,shipments2):
#print [str(s) for s in shipments1]
#print [str(s) for s in shipments2]
ctr = 0
if len(shipments1) != len(shipments2):
return False
while ctr < len(shipments1):
shipment1 = shipments1[ctr]
shipment2 = shipments2[ctr]
if str(shipment1.getId()) != str(shipment2.getId()):
return False
if len(shipment1.getItems()) != len(shipment2.getItems()):
return False
i = 0
while i < len(shipment1.getItems()):
item1 = shipment1.getItems()[i]
item2 = shipment2.getItems()[i]
if str(item1) != str(item2):
return False
i = i + 1
ctr = ctr + 1
return True
def test1():
item = Item(\'sockz\',12346,\'$4.44\')
return str(item) == \'sockz 12346 $4.44\'
def test2():
item = Item(\'sockz\',12346,\'$4.44\')
return str(item.getName()) == \'sockz\'
def test3():
item = Item(\'sockz\',12346,\'$4.44\')
return str(item.getId()) == \'12346\'
def test4():
item = Item(\'sockz\',12346,\'$4.44\')
return str(item.getPrice()) == \'$4.44\'
def test5():
shipment = Shipment(55551555)
return str(shipment) == \'55551555: []\'
def test6():
shipment = Shipment(55551555)
return str(shipment.getId()) == \'55551555\'
def test7():
shipment = Shipment(55551555)
return shipment.getItems() == []
def test8():
item = Item(\'sockz\',12346,\'$4.44\')
shipment = Shipment(55551555)
shipment.addItem(item)
return str(shipment) == \'55551555: [sockz 12346 $4.44]\'
def test9():
item = Item(\'sockz\',12346,\'$4.44\')
shipment = Shipment(55551555)
shipment.addItem(item)
item = Item(\'shirt\',33233,\'$14.78\')
shipment.addItem(item)
return str(shipment) == \'55551555: [sockz 12346 $4.44,shirt 33233 $14.78]\'
def test10():
item = Item(\'sockz\',12346,\'$4.44\')
shipment = Shipment(55551555)
shipment.addItem(item)
result = main([\'55551555\ \',\'sockz 12346\ \',\'$4.44\ \'],[1])
return checkEqual([shipment],result)
def test11():
shipments = []
shipment = Shipment(55551555)
item = Item(\'sockz\',12346,\'$4.44\')
shipment.addItem(item)
item = Item(\'shorts\',33233,\'$42.56\')
shipment.addItem(item)
shipments.append(shipment)
result = main([\'55551555\ \',\'sockz 12346\ \',\'$4.44\ \',\'shorts 33233\ \',\'$42.56\ \'],[2])
return checkEqual(shipments,result)
def test12():
shipments = []
shipment = Shipment(55551555)
item = Item(\'sockz\',12346,\'$4.44\')
shipment.addItem(item)
item = Item(\'shorts\',33233,\'$42.56\')
shipment.addItem(item)
shipments.append(shipment)
item = Item(\'books\',12346,\'$4.44\')
shipment = Shipment(66678)
shipment.addItem(item)
item = Item(\'mirror\',33233,\'$42.56\')
shipment.addItem(item)
shipments.append(shipment)
shipment = Shipment(1)
item = Item(\'shoez\',12346,\'$4.44\')
shipment.addItem(item)
item = Item(\'pencils\',33233,\'$42.56\')
shipment.addItem(item)
shipments.append(shipment)
result = main([\'55551555\ \',\'sockz 12346\ \',\'$4.44\ \',\'shorts 33233\ \',\'$42.56\ \',\'66678\ \',\'books 12346\ \',\'$4.44\ \',\'mirror 33233\ \',\'$42.56\ \',\'necklace 33233\ \',\'$42.56\ \',\'1\ \',\'shoez 12346\ \',\'$4.44\ \',\'pencils 33233\ \',\'$42.56\ \',\'eraser 33233\ \',\'$42.56\ \'],[2,2,2])
return checkEqual(shipments,result)
def test13():
shipments = []
item = Item(\'sockz\',12346,\'$4.44\')
shipment = Shipment(55551555)
shipment.addItem(item)
shipments.append(shipment)
result = main([\'55551555\ \',\'sockz 12346\ \',\'$4.44\ \',\'sockz 12347\ \',\'$4.44\ \'],[1])
return checkEqual(shipments,result)
def test14():
result = False
try:
main([\'55551555\ \',\'sockz12346\ \',\'$4.44\ \',\'sockz 12346\ \',\'$4.44\ \'],[2])
except ItemException:
result = True
return result
def test15():
result = False
try:
main([\'55551555\ \',\'sockz 12346\ \',\'4.44\ \',\'sockz 12346\ \',\'$4.44\ \'],[2])
except PriceException:
result = True
return result
def test16():
result = False
try:
main([\'55551555\ \',\'sockz 12346\ \',\'$4.447\ \',\'sockz 12346\ \',\'$4.44\ \'],[2])
except PriceException:
result = True
return result
def test17():
result = False
try:
main([\'55551555\ \',\'sockz 12346\ \',\'$4.44\ \',\'sockz 12346\ \',\'$-4.44\ \'],[2])
except PriceException:
result = True
return result
def test18():
result = False
try:
main([\'55551555\ \',\'sockz 12346\ \',\'$4.44\ \',\'sockz 12346\ \',\'$4.44.56\ \'],[2])
except PriceException:
result = True
return result
def test19():
file = open(\"data.txt\",\"w\")
file.write(\"5555155555\ Socks 12346\ $5.59\ \")
file.close()
list = processFile(\"data.txt\")
return list == [\"5555155555\ \",\"Socks 12346\ \",\"$5.59\ \"]
def test20():
shipments = []
shipment = Shipment(55551555)
item = Item(\'sockz\',12346,\'$4.44\')
shipment.addItem(item)
item = Item(\'shorts\',33233,\'$42.56\')
shipment.addItem(item)
shipments.append(shipment)
item = Item(\'books\',12346,\'$4.44\')
shipment = Shipment(66678)
shipment.addItem(item)
item = Item(\'mirror\',33233,\'$42.56\')
shipment.addItem(item)
item = Item(\'necklace\',33253,\'$42.56\')
shipment.addItem(item)
shipments.append(shipment)
shipment = Shipment(1)
item = Item(\'sockz\',12346,\'$4.44\')
shipment.addItem(item)
item = Item(\'pencils\',33237,\'$42.56\')
shipment.addItem(item)
item = Item(\'eraser\',33233,\'$42.56\')
shipment.addItem(item)
shipments.append(shipment)
result = main([\'55551555\ \',\'sockz 12346\ \',\'$4.44\ \',\'shorts 33233\ \',\'$42.56\ \',\'shorts 33233\ \',\'$42.56\ \',\'66678\ \',\'books 12346\ \',\'$4.44\ \',\'mirror 33233\ \',\'$42.56\ \',\'necklace 33253\ \',\'$42.56\ \',\'1\ \',\'sockz 12346\ \',\'$4.44\ \',\'pencils 33237\ \',\'$42.56\ \',\'eraser 33233\ \',\'$42.56\ \'],[2,3,3])
return checkEqual(shipments,result)
def test21():
shipments = []
shipment = Shipment(55551555)
item = Item(\'sockz\',12346,\'$4.44\')
shipment.addItem(item)
item = Item(\'shorts\',33233,\'$42.56\')
shipment.addItem(item)
shipments.append(shipment)
item = Item(\'books\',12346,\'$4.44\')
shipment = Shipment(66678)
shipment.addItem(item)
item = Item(\'mirror\',33233,\'$42.56\')
shipment.addItem(item)
shipments.append(shipment)
shipment = Shipment(1)
item = Item(\'shoes\',12346,\'$4.44\')
shipment.addItem(item)
item = Item(\'pencils\',33233,\'$42.56\')
shipment.addItem(item)
shipments.append(shipment)
result = main([\'55551555\ \',\'sockz 12346\ \',\'$4.44\ \',\'shorts 33233\ \',\'$42.56\ \',\'66678\ \',\'books 12346\ \',\'$4.44\ \',\'mirror 33233\ \',\'$42.56\ \',\'necklace 33233\ \',\'$42.56\ \',\'1\ \',\'shoes 12346\ \',\'$4.44\ \',\'pencils 33233\ \',\'$42.56\ \',\'eraser 33233\ \',\'$42.56\ \'],[2,2,2])
return checkEqual(shipments,result)
failed = False
ctr = 1
while ctr <= 21:
try:
if eval(\"test\"+str(ctr)+\"()\") == True:
print \"PASSED test\"+str(ctr)+\"!\"
else:
print \"Please check your code for test\"+str(ctr)
failed = True
except Exception as e:
traceback.print_exc()
print \"Please check your code for test\"+str(ctr)+\", it raised an undesired exception\"
failed = True
ctr = ctr + 1
if not failed:
print \"You PASSED ALL TESTS\"
result = subprocess.check_output(\"curl -k https://cs.gmu.edu/~kdobolyi/sparc/process.php?user=sparc_JjNcihjCCN7CpKRh-sample_ass9_3-COMPLETED\", shell=True)
else:
print \"At least one test is not passing yet.\"
result = subprocess.check_output(\"curl -k https://cs.gmu.edu/~kdobolyi/sparc/process.php?user=sparc_JjNcihjCCN7CpKRh-sample_ass9_3-PROGRESS\", shell=True)
The following part (project5.py) is for reference only:
class Item:
def __init__(self,name,id,price):
self.name = name
self.Id = id
self.price = price
def getName(self):
return self.name
def getPrice(self):
return self.price
def getId(self):
return self.Id
def __str__(self):
return str(self.name)+ \' \' + str(self.Id)+ \' \' + str(self.price)
class Shipment:
def __init__(self,id):
self.id = id
self.items = []
def getId(self):
return self.id
def getItems(self):
return self.items
def addItem(self,item):
self.items.append(item)
def __str__(self):
result = str(self.id) + \': [\'
ctr = 0
while ctr < len(self.items):
if ctr == len(self.items)-1:
result += str(self.items[ctr])
else:
result += str(self.items[ctr]) + \',\'
ctr += 1
return result + \']\'
class ItemException:
def __init__(self,message):
self.message = message
self.empty_lis = []
def __str__(self):
return str(self.message)
class PriceException:
def __init__(self):
self.messages = messages
self.empty = []
def __str__(self):
return str(self.message)
def processFile(filename):
file = open(\'filename\',\'r\')
contents = file.readlines()
list = str(contents)
return list
def main(lis):
final = []
ctr = 0
#This will split up multiple shipments into lists within a list (will also leave single shipments in a list of a list)
while ctr < len(lis):
seperate_items = []
if lis[ctr][:-1].isdigit():
seperate_items.append(lis[ctr][:-1])
ctr2 = ctr + 1
while ctr2 < len(lis):
if not lis[ctr2][:-1].isdigit():
seperate_items.append(lis[ctr2][:-1])
else:
break
ctr2 += 1
final.append(seperate_items)
ctr += 1
#[[\'55555555\', \'socks 12345\', \'$4.56\', \'shorts 33333\', \'$42.56\']]
#Now I need to make the code run with the new values like it should to pass
#This section needs to be modified to sort...
#the name of the items by their name in alphabetical order
#and shipments need to be sorted by their ids in numeric order (ascending).
ctr3 = 0
result = []
while ctr3 < len(final):
ctr4 = 1
shipmentId = final[ctr3][ctr4-1]
shipment = Shipment(shipmentId)
while ctr4 < len(final[ctr3]):
if not final[ctr3][ctr4].isdigit():
shipitem = final[ctr3][ctr4].split(\' \')[0]
#raise an exception for there being a missing space
#between an item and its ID
if len(final[ctr3][ctr4].split(\' \')) == 1:
raise ItemException(Exception)
itemId = final[ctr3][ctr4].split(\' \')[1]
itemPrice = final[ctr3][ctr4+1]
if itemPrice[0] != \'$\' or itemPrice[-3] != \'.\' or \'-\' in itemPrice or itemPrice.count(\'.\') > 1:
raise ItemException(Exception)
item = Item(shipitem, itemId, itemPrice)
shipment.addItem(item)
ctr4 += 2
ctr3 += 1
result.append(shipment)
#result is the origonal end of the code in Project 5
#[55555555: [socks 12345 $4.56,shorts 33333 $42.56],66678: [books 12345 $4.56,mirror 33333 $42.56,necklace 333333 $42.56],1: [shoes 12345 $4.56,pencils 33333 $42.56,eraser 33333 $42.56]]
#Make a loop to sort the shipmentId in numerical order (ascending)
new_list = []
while result:
minimum = result[0]
for x in result:
if x < minimum:
minimum = x
new_list.append(minimum)
result.remove(minimum)
#Make a loop to sort the items in alphabetical order
alpha_list = []
ctr5 = 0
while new_list:
minimum2 = new_list[ctr5][0]
for i in new_list[ctr5]:
if i < minimum2:
minimum2 = i
alpha_list.append(minimum2)
ctr5 += 1
return alpha_list
| A constructor that takes a message as an argument. The constructor creates this attribute, and sets it to the incoming argument. The constructor also initializes an empty list of items. |
| A to-string method that can be called with str(...), which returns the message of the exception. |
Solution
# project5.py
class Item:
def __init__(self,name,id,price):
self.name = name
self.Id = id
self.price = price
def getName(self):
return self.name
def getPrice(self):
return self.price
def getId(self):
return self.Id
def __str__(self):
return str(self.name)+ \' \' + str(self.Id)+ \' \' + str(self.price)
class Shipment:
def __init__(self,id):
self.id = id
self.items = []
def getId(self):
return self.id
def getItems(self):
return self.items
def addItem(self,item):
self.items.append(item)
def __str__(self):
result = str(self.id) + \': [\'
ctr = 0
while ctr < len(self.items):
if ctr == len(self.items)-1:
result += str(self.items[ctr])
else:
result += str(self.items[ctr]) + \',\'
ctr += 1
return result + \']\'
class ItemException:
def __init__(self,message):
self.message = message
self.empty_lis = []
def __str__(self):
return str(self.message)
class PriceException:
def __init__(self, message):
self.message = message
self.items = []
def __str__(self):
return str(self.message)
def processFile(filename):
contents = []
with open(filename,\'r\') as fp:
for line in fp:
contents.append(str(line))
return contents
def main(lis, lis_i):
final = []
ctr = 0
#This will split up multiple shipments into lists within a list (will also leave single shipments in a list of a list)
index = -1
while ctr < len(lis):
seperate_items = []
if lis[ctr][:-1].isdigit():
index += 1
count = 0
seperate_items.append(lis[ctr][:-1])
ctr2 = ctr + 1
while ctr2 < len(lis):
if not lis[ctr2][:-1].isdigit():
count = count + 1
if count <= 2*lis_i[index]:
seperate_items.append(lis[ctr2][:-1])
else:
break
ctr2 += 1
final.append(seperate_items)
ctr += 1
#[[\'55555555\', \'socks 12345\', \'$4.56\', \'shorts 33333\', \'$42.56\']]
#Now I need to make the code run with the new values like it should to pass
#This section needs to be modified to sort...
#the name of the items by their name in alphabetical order
#and shipments need to be sorted by their ids in numeric order (ascending).
ctr3 = 0
result = []
while ctr3 < len(final):
ctr4 = 1
shipmentId = final[ctr3][ctr4-1]
shipment = Shipment(shipmentId)
while ctr4 < len(final[ctr3]):
if not final[ctr3][ctr4].isdigit():
#raise an exception for there being a missing space
#between an item and its ID
if len(final[ctr3][ctr4].split(\' \')) == 1:
raise ItemException(Exception)
shipitem = final[ctr3][ctr4].split(\' \')[0]
itemId = final[ctr3][ctr4].split(\' \')[1]
itemPrice = final[ctr3][ctr4+1]
if itemPrice[0] != \'$\' or itemPrice[-3] != \'.\' or \'-\' in itemPrice or itemPrice.count(\'.\') > 1:
raise PriceException(Exception)
item = Item(shipitem, itemId, itemPrice)
shipment.addItem(item)
ctr4 += 2
ctr3 += 1
result.append(shipment)
return result
# pastebin link http://pastebin.com/SnfvctnJ









