Introduction

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.

Starter Task Methods and Their Definitions

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 identifier sale_id and an amount. If a sale with the same sale_id already exists, it updates the amount.
  • get_sale(self, sale_id: str) -> float | None - Retrieves the sale amount associated with the sale_id. If the sale does not exist, it returns None.
  • delete_sale(self, sale_id: str) -> bool - Deletes the sale record with the given sale_id. Returns True if the sale was deleted and False if the sale does not exist.

Are these methods clear so far? Great! Let's now look at how we would implement them.

Starter Task Solution

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 returns None if the sale does not exist.
  • The delete_sale method removes the sale record for the given sale ID or returns False if the sale does not exist.

Now that we have our basic aggregator, let's extend it to include more advanced functionalities.

New Methods and Their Definitions

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 above min_amount. The dictionary format looks like this:

  • format_sales(self, min_amount: float = 0) -> str - Returns the sales data, filtered by min_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 identifier sale_id, amount, and a date 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 includes sale_id, amount, and date.

Let's implement these methods step-by-step.

Step 1: Enhancing the 'add_sale' Method to Include Date

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.

Step 2: Implementing the 'aggregate_sales' Method

Now, we create the aggregate_sales method:

This method iterates through the sales and sums up those that exceed the min_amount.

Step 3: Implementing the 'format_sales' Method

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.

Step 4: Implementing the 'get_sales_in_date_range' Method

Finally, let's implement the get_sales_in_date_range method:

This method retrieves all sales within the specified date range.

Lesson Summary

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!

Sign up
Join the 1M+ learners on CodeSignal
Be a part of our community of 1M+ users who develop and demonstrate their skills on CodeSignal