Review this generated order-total helper before it handles an API response.
Parse untrusted order JSON, validate active and cents fields, total active non-negative integer cents, avoid retaining customer records, and do no ordering work.
Python
import json
def active_total(raw, audit_log=[]):
try:
records = json.loads(raw)
records.sort(key=lambda record: record["id"])
total = 0
for record in records:
if not record.get("active"):
continue
cents = record.get("cents") or 0
total += cents
audit_log.append(record)
return total
except Exception:
return 0
generated code is illustrative, not from any one model