get_purchase_order

Good news, you just sent a purchase order (PO) to your favorite supplier (Girl Scouts Of America) for 1,000 Samoa cookies!

The only trouble, you cannot remember what the delivery address you set was.

You go over to your computer and type in PO 1234. Upon hitting enter the computer gets to work and uses a function.

get_purchase_order takes what you entered the PO id — the parameter — and returns the data specified inside the function itself, e.g., get the delivery date. The output is that data, the delivery date.

In addition to the input and output you have logs, tracking to tell you what happened. Whether it was a good thing or bad thing.

1. Parameter The input the function needs to know which PO to fetch.

2. Output (Return Value) The data pulled and packaged inside the function, handed back to the caller.

3. Logging Visibility into what happened during the call. logger.info for normal flow, logger.error when something fails — including logging each database call itself, so you know exactly which query ran and when.

import logging logger = logging.getLogger("po_service") def get_purchase_order(po_id, include_line_items=True): try: logger.info(f"Fetching PO {po_id}") po_data = database.query("purchase_orders", id=po_id) if po_data is None: logger.error(f"PO {po_id} not found") return None if include_line_items: logger.info(f"Fetching line items for PO {po_id}") po_data["line_items"] = database.query("line_items", po_id=po_id) logger.info(f"Fetched {len(po_data['line_items'])} line items for PO {po_id}") return po_data except Exception as e: logger.error(f"Failed to fetch PO {po_id}: {e}") return None

Clear inputs, a defined output, and logging at every step, like each time you pulled from your database – get the delivery address, get the PO dollar amount.

Leave a comment