Case File: The Fee Table
The finance team has a table of fees: weight bands, express delivery that adds half again, and insurance of two percent with a minimum. Turn it into a small function with default arguments, use it for a list of orders, and check the weights that sit on the edges of the bands. Learn how to write a function that you can trust, call from several places and test on its own. The orders are generated for you alone.
Sabi Logistics Β· Python 5
The Fee Table
Customers have been quoted three different prices for the same parcel, depending on who answered the phone
Halima Operations manager Β· Phone call
Halima. Customers are being quoted different prices for the same parcel, depending on who picks up the phone. Finance has a fee table, but everybody applies it from memory. I want one function that gives the price, and I want to be able to run it on a whole list of orders and see the total.
The table is on your screen. The part I worry about most is the edges: 1 kilo, 1.1 kilos, exactly 20 kilos, and what insurance costs on a small parcel. If your function gets those right, I will trust it.
The brief
What you need to know first.
- A function packages a rule:
def fee(kg, express=False, insured_value=0):names the function and its inputs; the indented lines are the rule;return basehands the answer back.express=Falseis a default value: a caller who gives no express value gets False. Defaults make the common case short. - Rounding up.
math.ceil(kg)(afterimport math) rounds up to the next whole number: 1.1 becomes 2, 20 stays 20, 20.1 becomes 21. A part of a kilo is billed as a whole kilo. - The table. Billable kilos up to 1: 500 naira; up to 5: 900; up to 20: 1800; above 20: 1800 plus 120 for each kilo above 20. Test the bands in order with
if ... elif ... else. Express adds half of the base again:base = base + base // 2(//is division that drops the fraction, which keeps whole naira). Insurance, when the value is more than 0, addsmax(100, insured_value * 2 // 100): two percent, but never less than 100. - Using the function.
price_orders(req)loops over the orders and callsfee(o["kg"], o.get("express", False), o.get("insured_value", 0))for each;o.get(name, default)reads a key that may be missing without an error. It returns{"fees": [...], "total": ..., "biggest": ...}, with 0 for no orders. - Fill in
fee,price_orders, thentotalandbiggest. The checks runprice_orderson lists that you cannot see: weights of 1, 1.1, 5, 20 and 20.1 kg, a tiny parcel with insurance, and no orders.
The lab gives you the data for this case, invented and generated for you alone.
Preparing your dataβ¦
Halima: βThe function is right, but finance changes the table twice a year. Do I call you every time?β
π The debrief unlocks when you close the case. Finish the lab above.
The debrief
Halima: βSo the table should be data, not code.β
What an experienced person would do next. Move the numbers of the table into a data structure (a list of (up to kilos, fee) pairs, kept in a file or at the top of the program), so that the function reads the table and a change is a change of data, reviewed by finance. Keep a short list of tests next to the function: the edge weights and their prices worked out by hand, so that any change to the table is checked at once. Keep the function pure: the same inputs always give the same answer, and nothing else changes. Everybody who needs a price calls the same function. That is how three prices become one.
The trap. Rounding to the nearest kilo instead of up (round instead of math.ceil), and computing insurance on the fee instead of on the insured value.
Write the fee function note
Write the note for the operations manager: the rules of the fee table as your function applies them (rounding up to whole kilos, the bands, the express half, the insurance of two percent with a minimum of 100), the total and the biggest fee for the orders in the case, the edge weights that you checked and their prices (1, 1.1, 5, 20 and 20.1 kg), and how to keep the table up to date safely (the table as data, tests with answers worked out by hand, one function that everybody calls).
π§ͺ Want to practise this on your own computer? See how to set up your lab →