]> mj.ucw.cz Git - home-hw.git/commitdiff
Auto: Make a bit more pythonic
authorMartin Mares <mj@ucw.cz>
Wed, 13 Jul 2022 12:52:09 +0000 (14:52 +0200)
committerMartin Mares <mj@ucw.cz>
Wed, 13 Jul 2022 12:52:09 +0000 (14:52 +0200)
auto/burrow-auto

index 47707181ad1d0a2a85e42358c328599fac8f6870..1774b8ee70b98d9cbc463a69f2e9e7aa21341364 100755 (executable)
@@ -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))