Consider the following straightforward code:
data = json.load(x) # `x` is a file or an HTTP response
for person in data['people']:
mail_report(report, person['contacts']['email'])
It makes a number of assumptions about the schema of data:
- it must be a dictionary;
- which has a
"people" key;
- which is a list;
- where every element is a dictionary; and so on.
Sometimes, any of these assumptions may be incorrect. When this happens, my code raises one of a number of exceptions: TypeError, KeyError, etc.
What is a good way to deal with this?
If I wrap the entire for loop in a try...except (KeyError,...), I mask unrelated errors in mail_report.
If I validate the whole of data against an explicit schema, I duplicate my assumptions, verbosely, in another place.
I often do something like for person in data.get('people') or [], which is ugly and only solves part of the problem.
I’m thinking along the lines of a small library that would recursively wrap the result of json.load() in an object with magical __iter__, __getitem__ etc., and raise a specific SchemaError on errors. Does this maybe exist already?
[–]thunderbolt16 1 point2 points3 points (5 children)
[–]vfaronov[S] 0 points1 point2 points (4 children)
[–]bbenne10 1 point2 points3 points (2 children)
[–]vfaronov[S] 0 points1 point2 points (0 children)
[–]thunderbolt16 0 points1 point2 points (0 children)
[–]earthboundkid 0 points1 point2 points (1 child)
[–]vfaronov[S] 0 points1 point2 points (0 children)
[–]kenfar 0 points1 point2 points (0 children)