runtime
287 / 500
question 287 Advanced OOP & Design · medium

Line parsers that share a template

Partners send orders in two text formats: O100282,2,17.92 and order=O100282; qty=2; price=17.92. Write an abstract LineParser whose concrete parse(text) does the shared work (skip blank lines and lines starting with #, number the lines from 1, collect records, and turn a ValueError from one line into an error message) and whose abstract parse_line(line) turns one line into a record. Then write CsvLineParser and KeyValueParser.

Record{"order_id": str, "qty": int, "price": float}
`LineParser.parse(text)`→ (records, errors); errors are "line <n>: <message>"
`CsvLineParser.parse_line`order_id,qty,price; anything else → ValueError
`KeyValueParser.parse_line`key=value pairs separated by ; (spaces allowed), keys order, qty, price in any order; a missing key → ValueError

Loading your progress…

Line parsers that share a template — Python practice · Runtime