question 218 Modules & Packages · medium
A summary tool with an exit status
Write this file as order_summary, used two ways. Other code imports summarize(rows). The scheduler runs the file and reads its exit status: 0 means success, anything else means "look at this". main() reads orders.csv, prints a one-line summary and returns 0, or prints no orders in orders.csv and returns 1 when the file has no rows. When run, the file ends with raise SystemExit(main()), which ends the program with that status, behind the __main__ guard.
Library
summarize(rows) → {"orders": n, "delivered": d, "revenue": "<delivered total, 2 decimals>"} for rows with status and text totalScript
main() prints "<orders> orders, <delivered> delivered, <revenue> revenue" and returns 0; with no rows prints no orders in orders.csv and returns 1Importprints nothing and reads no file
Examples
callsummarize([{'status': 'delivered', 'total': '12.93'}, {'status': 'cancelled', 'total': '57.34'}, {'status': 'delivered', 'total': '71.14'}])returns{'orders': 3, 'delivered': 2, 'revenue': '84.07'}
callsummarize([])returns{'orders': 0, 'delivered': 0, 'revenue': '0.00'}
callmain()returns0
Expected output1 line
- 13 orders, 2 delivered, 84.07 revenue