Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

PYTHON JUST FROM QUESTION #3 ON .. I already know parts 1 and 2. Assignment Statementh Read CSV files You may use the csv module

PYTHON

JUST FROM QUESTION #3 ON.. I already know parts 1 and 2.

Assignment Statementh

Read CSV files You may use the csv module and its methods for this project. The two CSV files are here: https://www.dropbox.com/s/6gmbyz82z0v95g1/MLB2008.csv?dl=0 https://www.dropbox.com/s/87wmx4s53ul79aj/StockValuations.csv?dl=0

Load the content of the CSV files into usable records Use polymorphism to hold data Use inheritance to process data

Requirements:

To extract the raw data (row) into a usable format that can be relied upon by the rest of your application, we will define the concept of a record. 1. Create class: AbstractRecord a. This class will contain 1 (instance) member: name

2. Create a record class for each of the files you want to load. When a record is referenced in the requirements, we are referring to one of the following records: BaseballStatRecord or StockStatRecord. And including the following functionality: a. inherit the AbstractRecord b. have an initializer method that takes the data you want to load as arguments: For stocks: Stock symbol (ticker) this should be stored in the name member Company name (company_name) Exchange country (exchange_country) Stock Price (price) Exchange Rate (exchange_rate) Shares Outstanding (shares_outstanding) Net Income (net_income) Market Value in USD (market_value_usd) This value is calculated in step 4e Price/Earnings Ratio (pe_ratio) This step is calculated in step 4e

c. for baseball player name this should be stored in the name member salary G (Games played) AVG (which is the batting average)

d. For each record type, override __str__() (https://docs.python.org/3/reference/datamodel.html#object.__str__ ) to return a string of the form: ( , , <...> ) using str.format. For floats please only display 2 decimal numbers (2 numbers after the comma)

To load the data we are going to need a CSV reader.

3. Create 1 AbstractCSVReader class a. The class should have an initializer method taking the path to the file to be read b. The class should have the method: row_to_record(row) Where row is a row from the CSV as a dictionary This method should be implemented by simply raising NotImplementedError. c. The class should have the method: load() that returns a list of records. Load should: i. Use with to open the CSV files ii. read each row from the file into a dictionary iii. call the row_to_record method and send the row as a parameter iv. handle the BadData exception raised by row_to_record by skipping the record For more on BadData Exception see step 5 v. If no exception is raised: then the record should be added to the list of records. vi. Once all records are loaded into the list, returns the list.

4. Create a CSV reader class for each of the files you want to load (i.e. BaseballCSVReader and StocksCSVReader) a. The class should inherit the AbstractCSVReader b. Each class should implement its own row_to_record method. The input is a dictionary of unvalidated data, it should validate the data, parse it, create new record and return the record created. c. The validation depends on your concrete record: Validation fails for any row that is missing any piece of information Validation fails if the name (symbol or player name) is empty Validation fails if any of the numbers (int or float) cannot be parsed (watch out of the division by zero!!) d. If validation fails: this method should raise a BadData exception (requirement #5) e. StocksCSVReader should have two calculations using the extracted records: market_value_usd = Price * ExchangeRate * SharesOutstanding pe_ratio = Price * SharesOutstanding / NetIncome

5. Create a BadData custom exception to handle record creation errors

6. From your main section ( https://docs.python.org/3/library/__main__.html ) a. load the CSV (e.g. BaseballCSVReader('path to my CSV').load()) b. Print each record to the console. You are to use: print(record)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Data Access Patterns Database Interactions In Object Oriented Applications

Authors: Clifton Nock

1st Edition

0321555627, 978-0321555625

More Books

Students also viewed these Databases questions

Question

u = 5 j , v = 6 i Find the angle between the vectors.

Answered: 1 week ago