...
/Solution: Build a Sales Report Generator
Solution: Build a Sales Report Generator
Learn how to implement the coded solution of a sales report generator system using functional programming intersection.
We'll cover the following...
We'll cover the following...
Read sales data
First create a function named read_sales_data() that will assist us in reading the data written in a CSV file. The path of the file will be given as a parameter to the function.
Code explanation
- Line 4–6: Passed the
file_pathas input and opened the file usingopen()method. - Line 7: Created a
csv.DictReaderobject to read the contents of the file as a dictionary. - Line 8–9: Iterated each row in the CSV file and added it to the
sales_datalist. - Line 10: Finally, returned the
sales_datalist.
Calculate total sales
Now implement a calculate_total_sales() function to accept the sales_data as a parameter. This function calculates the total sales of each country and returns a dictionary containing the required data.
Code explanation
- Line 13: Passed the
sales_dataas input, which is a list of dictionaries representing the sales data. - Line 15–18: Iterated over each transaction in the
sales_data. For each transaction, it extracted thecountry,quantity, andpricevalues. - Line 19: Calculated the sales by multiplying the