Answer this lab 10 of python and make goes it works 100% and putwhich code goes in which files, thank you.
Goals
To raise and handle exceptions in a program
To use file system operations
You will be creating a Python program that takes in a book orderon the command-line, validates it and writes the order to a file.You will raise exceptions to report errors and handle those errorsby displaying them to the user.
For this lab, you are given unit tests that will verify thatyour Python code produces the correct results.
Start by creating a Lab 10 project in PyCharm.
Part A – Raising Exceptions and FileExistence
Copy the test_book_order_utils.py file into your project inPyCharm.
Then create a Python module called book_order_utils.py.
Create a function with the following signature:
def validate_book_order_details(order_num,title, author,
isbn, year_pub, quantity,
cost_cad):
This function will validate each of the order details,represented by the parameters. The table below describes thevalidation for each parameter and the type of exception that needsto be raised. You may use regular expressions (or other means) todo the validation. Make sure you import re if you use regularexpressions.
Parameter
Validation
Exception
order_num
One or more integer values. Note that both 1 and 0001 would bevalid.
ValueError
Order Number is invalid
title
One or more lower or upper case letters or spaces.
ValueError
Title is invalid
author
Zero or more lower or upper case letters, spaces orapostrophes.
ValueError
Author is invalid
isbn
Must be integers.
TypeError
ISBN must be an integer
Must be between 4 and 20 digits, inclusive
ValueError
ISBN is invalid
year_pub
Must be integers.
TypeError
Year must be an integer
Must be 4 digits exactly.
ValueError
Year is invalid
quantity
Must be integers.
TypeError
Quantity must be an integer
Must be between 0 and 1000, inclusive
ValueError
Quantity is invalid
cost_cad
Must be a floating point value with exactly 2 decimalplaces.
ValueError
Cost is invalid
Run the test_book_order_details.py unit test to verifyyour valid_book_order_details function. You will get a grade of7.5/11 if your validation is correct. If you do not, look forerrors related to ValueError or TypeError in the unit testoutput.
Create a function with the following signature:
def calculate_per_book_cost_cad(cost_cad,quantity):
Assume cost in Canadian dollars is a floating point value andquantity is an integer value. This function should just return afloating point value that is the cost per book in the order (i.e.,the cost divided by the quantity). Note that this function willraise a ZeroDivisionError exception (done automatically by Python)when the quantity (i.e., the denominator) is zero.
Run the test_book_order_details.py unit test to verifyyour calculate_per_book_cost_cad function. You will get a grade of9.5/11 if your implementation is correct (along withvalid_book_order_details).
Create a function with the following signature:
def write_book_order_details(filename,title, author,
isbn, year_pub, quantity, cost_cad, unit_cost_cad):
This function will create a file with the given filename andwrite the order details as follows:
BOOK ORDER
title=Intro to Python
author=Bill Smith
isbn=123456
year_pub=2010
quantity=10
cost_cad=$500.50
unit_cost=$50.05
If a file with the provided filename already exists, it shouldraise a ValueError exception with the message “Order file namealready exists!”. Use a function in os.path to check for the fileexistence. Make sure to import os into your module.
Run the test_book_order_details.py unit test to verifyyour write_book_order_details function. You will get a grade of11/11 if your implementation is correct (along withvalid_book_order_details and calculate_per_book_cost).
Part B – Handling Exceptions
Now create a Python script called book_order.py and create andcall your main function.
Your main function should do the following:
Verify there are exactly 8 command-line arguments
Get the following from the command-line (in this order):
order_num
title
author
isbn
year_pub
quantity
cost in Canadian dollars
Note that you need to import sys toretrieve the command line arguments
If the value of your command lineargument includes spaces, you need to put quotes around it whenyour script is called.
Call the validate_book_order_details function in thebook_order_utils module to validate your command-linearguments.
Note: Make sure to import your book_order_utils module
Call the calculate_per_book_cost_cad function in thebook_order_utils module to get the unit cost of each book inCanadian dollars. Make sure to pass in the cost in Canadian dollarsas a float and the quantity as an int.
Call the write_book_order_details function in thebook_order_utils module to write the book order to file.
Add exception handling to you main function. Put the calls tothe functions in book_order_utils above into a try block and addexception handlers for the following exceptions:
ValueError – Print “Value Error: “ then the message from theValueError exception.
TypeError – Print “Type Error: “ then the message from theTypeError exception.
ZeroDivisionError – Print “No Books in Order”
Run the test_book_order.py unit test to verify your codein your script. Modify your code until all unit tests pass. Youwill get a grade of 4/4 if they all pass.
Answer this lab 10 of python and make goes it works 100% and put which code goes in which files, thank you. Goals To rai
-
- Site Admin
- Posts: 899603
- Joined: Mon Aug 02, 2021 8:13 am