Logging
print is for your program's output. logging is for messages about what the program is doing, with levels that can be filtered without editing the code.
import logging
log = logging.getLogger("orders")
def charge(amount):
log.info("charging %s", amount)
if amount > 1000:
log.warning("large: %s", amount)- Levels rise from
DEBUGthroughINFO,WARNINGandERRORtoCRITICAL. - A logger has a dotted name, and messages travel up to its parents.
- Handlers decide where messages go. Applications configure them, libraries do not.
Format lazily
Pass values as arguments rather than building an f-string. The text is only assembled if a handler will actually use the record.
Set up once at program start:
logging.basicConfig(level=logging.INFO)Inside an except block, log.exception("failed") records the message along with the traceback.
Your task
Write parse_quantities(rows) converting strings to ints and returning the list of good values. Using a logger named "orders", log a WARNING containing the text of each bad row, then one INFO at the end containing the count of good rows. Print nothing.
The explanation opens here once every check passes.