Welcome to our lesson on mastering data aggregation and data streams with JSON formatting in Python. In this lesson, we'll start by building a basic sales records aggregator. Then, we'll extend its functionality to handle more complex operations such as filtering, data aggregation, and formatting. By the end of this session, you'll be able to manage and format data streams efficiently.
To begin, we'll implement a basic sales record aggregator. Here are the methods we'll be focusing on:
add_sale(self, sale_id: str, amount: float) -> None
- Adds a sale record with a unique identifiersale_id
and anamount
. If a sale with the samesale_id
already exists, it updates the amount.get_sale(self, sale_id: str) -> float | None
- Retrieves the sale amount associated with thesale_id
. If the sale does not exist, it returnsNone
.delete_sale(self, sale_id: str) -> bool
- Deletes the sale record with the givensale_id
. ReturnsTrue
if the sale was deleted andFalse
if the sale does not exist.
Are these methods clear so far? Great! Let's now look at how we would implement them.
Here is the complete code for the starter task:
Explanation:
- The
__init__
method initializes an empty dictionary to store sales records. - The
add_sale
method adds a new sale or updates the amount for an existing sale ID. - The
get_sale
method retrieves the amount for a given sale ID or returnsNone
if the sale does not exist. - The
delete_sale
method removes the sale record for the given sale ID or returnsFalse
if the sale does not exist.
Now that we have our basic aggregator, let's extend it to include more advanced functionalities.
To increase the complexity and usefulness of our sales aggregator, we'll introduce some new methods. These new methods will handle advanced data aggregation, filtering, and formatting functionalities involving JSON.
-
aggregate_sales(self, min_amount: float = 0) -> dict
- Returns a dictionary with the total number of sales and the total amount of sales where the sale amount is abovemin_amount
. The dictionary format looks like this: -
format_sales(self, min_amount: float = 0) -> str
- Returns the sales data, filtered bymin_amount
, formatted as JSON. Includes aggregated sales statistics in the output. -
add_sale(self, sale_id: str, amount: float, date: str) -> None
- Adds or updates a sale record with a unique identifiersale_id
,amount
, and adate
in the format "YYYY-MM-DD". -
get_sales_in_date_range(self, start_date: str, end_date: str) -> list[dict]
- Retrieves all sales that occurred within the given date range, inclusive. Each sale includessale_id
,amount
, anddate
.
Let's implement these methods step-by-step.
We'll first modify the add_sale
method to accept a date.
This ensures that each sale record includes a date in addition to the amount.
Now, we create the aggregate_sales
method:
This method iterates through the sales and sums up those that exceed the min_amount
.
Next, we'll create the format_sales
method to output data in JSON format.
This function formats the sales data as JSON and includes aggregated statistics.
Finally, let's implement the get_sales_in_date_range
method:
This method retrieves all sales within the specified date range.
Congratulations! You've now extended a basic sales aggregator to an advanced one capable of filtering, aggregating, and formatting data in JSON. These skills are crucial for handling data streams efficiently, especially when dealing with large datasets. Feel free to experiment with similar challenges to reinforce your understanding. Well done, and see you in the next lesson!
