From 3cb8813d565901a8a6b51fd70f3a169d6713ad2e Mon Sep 17 00:00:00 2001 From: Craig Maloney Date: Thu, 23 Jul 2020 11:42:21 -0400 Subject: [PATCH] Refactor multiple thermostat code --- thermostat/multiple.py | 24 +++++++----------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/thermostat/multiple.py b/thermostat/multiple.py index e9916d90..2794b38a 100644 --- a/thermostat/multiple.py +++ b/thermostat/multiple.py @@ -44,27 +44,17 @@ def multiple_thermostat_calculate_epa_field_savings_metrics(thermostats): metrics_dict = {} for output in results: - try: + if len(output) > 0: thermostat_id = output[0]['ct_identifier'] - metrics_dict[thermostat_id] = [] - for individual_output in output: - metrics_dict[thermostat_id].append(individual_output) - except IndexError: - warnings.warn("Missing thermostat output") + metrics_dict[thermostat_id] = output # Get the order of the thermostats from the original input so the output # matches the order that was sent in - thermostat_ids = \ - [thermostat.thermostat_id for thermostat in thermostats_list] metrics = [] - for thermostat_id in thermostat_ids: - try: - for metric in metrics_dict[thermostat_id]: - metrics.append(metric) - # Prevent duplicate thermostat IDs from being double-counted - metrics_dict.pop(thermostat_id, None) - # Trap for missing keys - except KeyError: - pass + for thermostat in thermostats_list: + for metric in metrics_dict.get(thermostat.thermostat_id, []): + metrics.append(metric) + # Prevent duplicate thermostat IDs from being double-counted + metrics_dict.pop(thermostat_id, None) return metrics