question 217 Modules & Packages · medium
A packing slip from the slip package
The warehouse prints fixed-width packing slips. A provided package, slip, already knows the layout and the units; turn an order into the lines of its slip using it. Provided files sit next to your program (they aren't editor tabs); print(Path("slip/layout.py").read_text(encoding="utf-8")) shows one's source.
Function
packing_slip(order)ordera dictionary with
order_id and items, a list of dictionaries with name, quantity and weight_kg (per unit)Returnsa list of lines:
"ORDER <order_id>"; then for each item columns("<quantity> x <name>", "<pounds for the whole line, 1 decimal> lb"); then columns("TOTAL WEIGHT", "<total pounds, 1 decimal> lb")Exampleline 2 for 2 × Deluxe Pillow at 1.12 kg is
'2 x Deluxe Pillow 4.9 lb'slip/__init__.py
from .layout import columns and from .units import to_lb, so from slip import columns, to_lb worksslip/layout.py
columns(left, right, width=30) → left, spaces, then right ending at column width (always at least one space)slip/units.py
to_lb(kg) → pounds (kg * 2.20462)Examples
callpacking_slip({'order_id': 'O104839', 'items': [{'name': 'Deluxe Pillow', 'quantity': 2, 'weight_kg': 1.12}, {'name': 'Pro Honey', 'quantity': 3, 'weight_kg': 0.56}]})returns['ORDER O104839', '2 x Deluxe Pillow 4.9 lb', '3 x Pro Honey 3.7 lb', 'TOTAL WEIGHT 8.6 lb']
callpacking_slip({'order_id': 'O100043', 'items': []})returns['ORDER O100043', 'TOTAL WEIGHT 0.0 lb']