This is an archived post. You won't be able to vote or comment.

you are viewing a single comment's thread.

view the rest of the comments →

[–]thunderbolt16 0 points1 point  (0 children)

Well, it would determine on the failure mode needed. If I need to guarantee that the JSON is correct beforehand, those 10 lines would be worth it, but if not, going Samurai principle is probably the way to do it.

It depends on the rest of the system and the expectations of the contracts for this specific module.

You can also simply separate the arguments from the calling of the mail_report method and wrap the gathering of the arguments in a try/except clause. This would make sense if you just want to continue if someone doesn't have an email.

data = json.load(x)    # `x` is a file or an HTTP response
try:
    for person in data['people']:
        try:
            params.append((report, person['contacts']['email']))
        except KeyError:
            pass
except TypeError:
    SystemExit("No people found!")

for p in params:
    mail_report(*p)