From 37a1eb22b07cd589ae8cb51f78b833a6196e2951 Mon Sep 17 00:00:00 2001 From: Chris Hill-Scott Date: Tue, 30 Aug 2016 14:54:33 +0100 Subject: [PATCH] Use default dict for default job stats MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This is just some refactoring. `defaultdict` is a data structure which won’t raise a `KeyError` if you try to access a key that doesn’t exist. By passing `int` as the first argument, trying to access the value of any key that doesn’t exists will return the value of `int()`, ie `0` --- app/notify_client/job_api_client.py | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/app/notify_client/job_api_client.py b/app/notify_client/job_api_client.py index dc3250ef4..bd9f06d57 100644 --- a/app/notify_client/job_api_client.py +++ b/app/notify_client/job_api_client.py @@ -1,3 +1,4 @@ +from collections import defaultdict from notifications_python_client.base import BaseAPIClient from app.notify_client import _attach_current_user @@ -16,12 +17,7 @@ class JobApiClient(BaseAPIClient): @staticmethod def __convert_statistics(job): - results = { - 'sending': 0, - 'delivered': 0, - 'failed': 0, - 'requested': 0 - } + results = defaultdict(int) if 'statistics' in job: for outcome in job['statistics']: if outcome['status'] in ['failed', 'technical-failure', 'temporary-failure', 'permanent-failure']: