Programming Essentials Python - Manipulating Collections - Performing Total Aggregations

We have pre-existing functions to take care of aggregations such as len, sum, min, max etc. Let us understand how they are typically implemented.

  • Get total number of records for a given month using orders.
  • Generate total revenue for a given order id using order items.
  • Use order items data set and get total number of items sold as well as total revenue generated for a given product_id.
  • Create a collection with sales and commission percentage. Using that collection compute total commission amount. If the commission percent is None or not present, treat it as 0.

Task 1

Use orders and get total number of records for a given month (201401).

  • Develop a function which take orders collection and month as arguments.
  • Month will be passed as integer in the form of yyyyMM (example 201401).
  • Return the order count
def get_order_count(orders, order_month):
    order_count = 0
    for order in orders:
        l_order_month = int(order.split(',')[1][:7].replace('-', ''))
        if l_order_month == order_month: order_count += 1
    return order_count

Task 2

Use order items data set and compute total revenue generated for a given product_id.

  • Define a function which takes order_items and a product_id as arguments.
  • product_id will be passed as integer
  • Compute revenue generated for a given product id using subtotal (5th field)
  • Return the computed product revenue
def get_product_revenue(order_items, product_id):
    product_revenue = 0.0
    for order_item in order_items:
        l_product_id = int(order_item.split(',')[2])
        order_item_subtotal = float(order_item.split(',')[4])
        if l_product_id == product_id: product_revenue += order_item_subtotal
    return product_revenue

Task 3

Use order items data set and get total number of items sold as well as total revenue generated for a given product_id.

  • Define a function which takes order_items and a product_id as arguments.
  • product_id will be passed as integer
  • Get number of items sold for a given product id using quantity (4th field)
  • Compute revenue generated for a given product id using subtotal (5th field)
  • Return the number of items sold as well as revenue generated. When we return more than one value, then the type of the returned results will be tuple.
def get_product_metrics(order_items, product_id):
    product_metrics = (0, 0.0)
    for order_item in order_items:
        l_product_id = int(order_item.split(',')[2])
        order_metric = (int(order_item.split(',')[3]), float(order_item.split(',')[4]))
        if l_product_id == product_id: 
            product_metrics = (product_metrics[0] + order_metric[0], product_metrics[1] + order_metric[1])
    return product_metrics
Alternative approach to get the product metrics. We can return multiple values as part of the return statement.
def get_product_metrics(order_items, product_id):
    product_count, product_revenue = 0, 0.0
    for order_item in order_items:
        l_product_id = int(order_item.split(',')[2])
        order_metric = (int(order_item.split(',')[3]), float(order_item.split(',')[4]))
        if l_product_id == product_id: 
            product_count += order_metric[0]
            product_revenue += order_metric[1]
    return product_count, product_revenue

Task 4

Create a collection with sales and commission percentage. Using that collection compute total commission amount. If the commission percent is None or not present, treat it as 0.

  • Each element in the collection should be a tuple.
  • First element is the sales amount and second element is commission percentage.
  • Commission for each sale can be computed by multiplying commission percentage with sales (make sure to divide commission percentage by 100).
  • Some of the records does not have commission percentage, in that case commission amount for that sale shall be 0
  • Function should take a collection of tuples and return commission amount which is of type float.
def get_commission_amount(sales):
    commission_amount = 0.0
    for sale in sales:
        sale_amount = sale[0]
        commission_pct = round(sale[1]/100, 2) if len(sale) == 2 else 0
        commission_amount += sale_amount * commission_pct
    return round(commission_amount, 2)

Watch the video tutorial here