I’m trying to learn for my Python class and I’m stuck. Can you help?
a short code that:
Demonstrate an understanding of:
- List creation
- List comprehensions
- Custom exceptions
- Tuples
- Built-in functions: min(), max(), sum(), len()
Specification
Start with your code from assignment six. I will send this one a tutor is chosen!
DataSet Class
Add the following to your DataSet class:
EmptyDatasetError exception
This exception will be raised when a method is asked to do calculation on the dataset, but no data is loaded.
_cross_table_statistics(self, descriptor_one: str, descriptor_two: str)
The two parameters of this function (descriptor_one and descriptor_two) describe a borough and property type. Use a list comprehension to create a list of rents for all properties that match both descriptors. If no properties matched both descriptors, return a tuple None, None, None. Otherwise return the minimum, average, and maximum rent as a tuple of floats.
load_default_data(self)
Later in the course we will load data from a file. For now we just want some data to play with. This function should simply load the following data into self._data as a list of tuples.
Borough
Type
Rate per Night
Staten Island
Private room
70
Brooklyn
Private room
50
Bronx
Private room
40
Brooklyn
Entire home / apt
150
Manhattan
Private room
125
Manhattan
Entire home / apt
196
Brooklyn
Private room
110
Manhattan
Entire home / apt
170
Manhattan
Entire home / apt
165
Manhattan
Entire home / apt
150
Manhattan
Entire home / apt
100
Brooklyn
Private room
65
Queens
Entire home / apt
350
Manhattan
Private room
99
Brooklyn
Entire home / apt
200
Brooklyn
Entire home / apt
150
Brooklyn
Private room
99
Brooklyn
Private room
120
Modifications to Existing Code
There are no modifications needed for this assignment
Unit Test
Our code for this assignment will not generate any different output if you run main(), so create a simple unit test function for _cross_table_statistics. All of these items should be tested within that function, and the function should report pass or fail for each.
- In your unit test, create an instance of the class DataSet. I’ll call it my_set here, you can choose whatever name you like.
- Before calling my_set.load_default_data, call my_set._cross_table_statistics and ensure that it raises EmptyDataSet.
- Next load the default data.
- Calculate the expected results of several calls by hand and compare the result with the return value of my_set._cross_table_statistics. Include:
- A call with a match in the Borough column but an invalid Property Type (should return None, None, None)
- A call with a match in the Property Type column but an invalid Borough (should return None, None, None)
- A call with valid entries for both columns but no match (should return None, None, None)
- A call that will match exactly one item in the table
- A call that will match more than one item in the table
- Any other important cases you can think of
Be sure to include the code for your unit test in your homework submission. In my solution, I just created a function called unit_test() and included it in the same file.
Sample Run
Your output can be different as long as it meets the criteria above.
Testing _cross_table_staticstics
Method Raises EmptyDataSet Error: Pass
Invalid Property Type Returns None Tuple: Pass
Invalid Borough Returns None Tuple: Pass
No Matching Rows Returns None Tuple: Pass
One Matching Row Returns Correct Tuple: Pass
Multiple Matching Rows Returns Correct Tuple: Pass