From 2125db97b02973dc88c80a8b33a0525f38471449 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Wed, 13 Jul 2022 14:52:09 +0200 Subject: [PATCH] Auto: Make a bit more pythonic --- auto/burrow-auto | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/auto/burrow-auto b/auto/burrow-auto index 4770718..1774b8e 100755 --- a/auto/burrow-auto +++ b/auto/burrow-auto @@ -1,5 +1,6 @@ #!/usr/bin/python3 +from collections import deque import getopt import paho.mqtt.client as mqtt import sys @@ -67,10 +68,7 @@ class State: return True def hysteresis(self, key, value, low, high): - if key in self.hyst_state: - old_state = self.hyst_state[key] - else: - old_state = 0 + old_state = self.hyst_state.get(key, 0) if value is None: new_state = 0 elif old_state <= 0: @@ -88,14 +86,14 @@ class State: def update_average(self, key, window_seconds): if key not in self.running_averages: - self.running_averages[key] = ([], 0, 0, None) + self.running_averages[key] = (deque(), 0, 0, None) (history, sum, count, avg) = self.running_averages[key] while len(history) > 0 and history[0][0] <= self.now - window_seconds: if history[0][1] is not None: sum -= history[0][1] count -= 1 - history.pop(0) + history.popleft() curr = self.get_sensor(key) history.append((self.now, curr)) -- 2.39.2