annotate py/utils.py @ 586:87c20b8c5472 default master

port to python3
author Matt Johnston <matt@ucc.asn.au>
date Mon, 09 Sep 2019 22:24:10 +0800
parents 02aff9ff8d24
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
1 import os
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
2 import sys
265
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
3 import ctypes
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
4 import time
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
5 import select
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
6 import logging
253
0a1b642e3086 long polling config updates
Matt Johnston <matt@ucc.asn.au>
parents: 230
diff changeset
7 import binascii
259
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
8 import json
265
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
9 import datetime
272
af924d27140f scale integrate by delay time
Matt Johnston <matt@ucc.asn.au>
parents: 271
diff changeset
10 import collections
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
11
149
d686b111dab4 working better. logging works properly, cleanup fridge.off() happens.
Matt Johnston <matt@ucc.asn.au>
parents: 145
diff changeset
12 D = logging.debug
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
13 L = logging.info
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
14 W = logging.warning
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
15 E = logging.error
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
16
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
17 DEFAULT_TRIES = 3
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
18 READLINE_SELECT_TIMEOUT = 1
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
19
144
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
20 def EX(msg, *args, **kwargs):
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
21 kwargs['exc_info'] = True
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
22 logging.error(msg, *args, **kwargs)
482d7852b511 a bit more, and some tests
Matt Johnston <matt@ucc.asn.au>
parents: 141
diff changeset
23
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
24 # decorator, tries a number of times, returns None on failure, sleeps between
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
25 # Must be used as "@retry()" if arguments are defaulted
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
26 def retry(retries=DEFAULT_TRIES, try_time = 1):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
27 def inner(func):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
28 def new_f(*args, **kwargs):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
29 for i in range(retries):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
30 d = func(*args, **kwargs)
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
31 if d is not None:
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
32 return d
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
33 time.sleep(try_time)
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
34 return None
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
35
230
185621f47040 run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 149
diff changeset
36 new_f.__name__ = func.__name__
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
37 return new_f
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
38 return inner
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
39
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
40 def readline(sock):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
41 timeout = READLINE_SELECT_TIMEOUT
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
42 buf = ''
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
43 while True:
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
44 (rlist, wlist, xlist) = select.select([sock], [], [], timeout)
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
45 if sock not in rlist:
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
46 # hit timeout
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
47 return None
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
48
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
49 c = sock.recv(1)
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
50 if c == '':
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
51 # lightblue timeout
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
52 return None
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
53 if c == '\r':
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
54 continue
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
55
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
56 buf += c
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
57 if c == '\n':
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
58 return buf
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
59
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
60 # from http://blog.stalkr.net/2011/04/pctf-2011-32-thats-no-bluetooth.html
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
61 def crc16(buff, crc = 0, poly = 0x8408):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
62 l = len(buff)
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
63 i = 0
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
64 while i < l:
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
65 ch = ord(buff[i])
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
66 uc = 0
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
67 while uc < 8:
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
68 if (crc & 1) ^ (ch & 1):
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
69 crc = (crc >> 1) ^ poly
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
70 else:
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
71 crc >>= 1
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
72 ch >>= 1
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
73 uc += 1
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
74 i += 1
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
75 return crc
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
76
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
77 def cheap_daemon():
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
78 L("Daemonising.")
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
79 sys.stdout.flush()
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
80 sys.stderr.flush()
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
81 out = file('/dev/null', 'a+')
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
82 os.dup2(out.fileno(), sys.stdout.fileno())
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
83 os.dup2(out.fileno(), sys.stderr.fileno())
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
84
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
85 try:
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
86 pid = os.fork()
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
87 if pid > 0:
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
88 sys.exit(0)
230
185621f47040 run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 149
diff changeset
89 except OSError as e:
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
90 E("Bad fork()")
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
91 sys.exit(1)
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
92
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
93 os.setsid()
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
94
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
95 try:
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
96 pid = os.fork()
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
97 if pid > 0:
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
98 sys.exit(0)
230
185621f47040 run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 149
diff changeset
99 except OSError as e:
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
100 E("Bad fork()")
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
101 sys.exit(1)
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
102
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
103 def uptime():
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
104 try:
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
105 return float(open('/proc/uptime', 'r').read().split(' ', 1)[0])
230
185621f47040 run 2to3
Matt Johnston <matt@ucc.asn.au>
parents: 149
diff changeset
106 except Exception as e:
145
6517ddee3187 few more bits
Matt Johnston <matt@ucc.asn.au>
parents: 144
diff changeset
107 return -1
141
4755e6f9a5b8 python raspberry pi rewrite
Matt Johnston <matt@ucc.asn.au>
parents:
diff changeset
108
259
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
109
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
110 def json_load_round_float(s, **args):
26eee8591f61 long polling works
Matt Johnston <matt@ucc.asn.au>
parents: 253
diff changeset
111 return json.loads(s,parse_float = lambda f: round(float(f), 2), **args)
265
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
112
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
113 class NotTooOften(object):
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
114 """ prevents things happening more than once per limit.
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
115 Isn't monotonic, good enough for logging. eg
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
116 self.logfailure = NotTooOften(180) # 3 minutes
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
117 ...
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
118 if self.logfailure():
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
119 L("blah")
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
120 """
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
121 def __init__(self, limit):
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
122 """ limit is a delay in seconds or TimeDelta """
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
123 if type(limit) is datetime.timedelta:
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
124 self.limit = limit
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
125 else:
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
126 self.limit = datetime.timedelta(seconds=limit)
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
127
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
128 # must be positive
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
129 assert self.limit > datetime.timedelta(0)
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
130 self.last = datetime.datetime(10, 1, 1)
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
131
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
132 def __call__(self):
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
133 if datetime.datetime.now() - self.last > self.limit:
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
134 self.last = datetime.datetime.now()
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
135 return True
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
136
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
137 def log(self, msg):
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
138 """ calls L(msg) if it isn't too often, otherwise D(msg)
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
139 """
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
140 if self():
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
141 L(msg + " (log interval %s)" % str(self.limit))
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
142 else:
78c542f03030 Limit log
Matt Johnston <matt@ucc.asn.au>
parents: 259
diff changeset
143 D(msg)
270
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
144
272
af924d27140f scale integrate by delay time
Matt Johnston <matt@ucc.asn.au>
parents: 271
diff changeset
145 Period = collections.namedtuple('Period', 'start end')
270
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
146 class StepIntegrator(object):
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
147 """
271
11cebd6f0bfb untested fridge.integrator
Matt Johnston <matt@ucc.asn.au>
parents: 270
diff changeset
148 Takes on/off events and a monotonically increasing timefn. Returns the integral
11cebd6f0bfb untested fridge.integrator
Matt Johnston <matt@ucc.asn.au>
parents: 270
diff changeset
149 of (now-limittime, now) over those events.
11cebd6f0bfb untested fridge.integrator
Matt Johnston <matt@ucc.asn.au>
parents: 270
diff changeset
150
270
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
151 >>> s = StepIntegrator(lambda: t, 40)
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
152 >>> t = 1
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
153 >>> s.turn(1)
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
154 >>> t = 10
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
155 >>> s.turn(0)
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
156 >>> t = 20
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
157 >>> s.turn(1)
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
158 >>> t = 30
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
159 >>> print(s.integrate())
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
160 19
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
161 >>> s.turn(0)
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
162 >>> print(s.integrate())
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
163 19
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
164 >>> t = 35
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
165 >>> print(s.integrate())
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
166 19
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
167 >>> t = 42
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
168 >>> print(s.integrate())
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
169 18
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
170 >>> t = 52
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
171 >>> print(s.integrate())
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
172 10
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
173 >>> t = 69
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
174 >>> print(s.integrate())
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
175 1
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
176 >>> t = 70
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
177 >>> print(s.integrate())
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
178 0
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
179 >>> t = 170
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
180 >>> print(s.integrate())
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
181 0
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
182 """
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
183 def __init__(self, timefn, limittime):
272
af924d27140f scale integrate by delay time
Matt Johnston <matt@ucc.asn.au>
parents: 271
diff changeset
184 # _on_periods is a list of [period]. End is None if still on
270
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
185 self._on_periods = []
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
186 self._timefn = timefn
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
187 self._limittime = limittime
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
188
271
11cebd6f0bfb untested fridge.integrator
Matt Johnston <matt@ucc.asn.au>
parents: 270
diff changeset
189 def set_limit(self, limittime):
11cebd6f0bfb untested fridge.integrator
Matt Johnston <matt@ucc.asn.au>
parents: 270
diff changeset
190 if self._limittime == limittime:
11cebd6f0bfb untested fridge.integrator
Matt Johnston <matt@ucc.asn.au>
parents: 270
diff changeset
191 return
11cebd6f0bfb untested fridge.integrator
Matt Johnston <matt@ucc.asn.au>
parents: 270
diff changeset
192 self._limittime = limittime
272
af924d27140f scale integrate by delay time
Matt Johnston <matt@ucc.asn.au>
parents: 271
diff changeset
193 self._trim()
271
11cebd6f0bfb untested fridge.integrator
Matt Johnston <matt@ucc.asn.au>
parents: 270
diff changeset
194
270
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
195 def turn(self, value):
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
196 if not self._on_periods:
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
197 if value:
272
af924d27140f scale integrate by delay time
Matt Johnston <matt@ucc.asn.au>
parents: 271
diff changeset
198 self._on_periods.append(Period(self._timefn(), None))
270
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
199 return
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
200
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
201 # state hasn't changed
272
af924d27140f scale integrate by delay time
Matt Johnston <matt@ucc.asn.au>
parents: 271
diff changeset
202 on_now = (self._on_periods[-1].end is None)
270
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
203 if value == on_now:
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
204 return
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
205
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
206 if value:
272
af924d27140f scale integrate by delay time
Matt Johnston <matt@ucc.asn.au>
parents: 271
diff changeset
207 self._on_periods.append(Period(self._timefn(), None))
270
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
208 else:
272
af924d27140f scale integrate by delay time
Matt Johnston <matt@ucc.asn.au>
parents: 271
diff changeset
209 self._on_periods[-1] = self._on_periods[-1]._replace(end = self._timefn())
270
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
210
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
211 def _trim(self):
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
212 begin = self._timefn() - self._limittime
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
213 # shortcut, first start is after begin
272
af924d27140f scale integrate by delay time
Matt Johnston <matt@ucc.asn.au>
parents: 271
diff changeset
214 if not self._on_periods or self._on_periods[0].start >= begin:
270
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
215 return
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
216
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
217 new_periods = []
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
218 for s, e in self._on_periods:
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
219 if s == e:
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
220 continue
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
221 elif s >= begin:
272
af924d27140f scale integrate by delay time
Matt Johnston <matt@ucc.asn.au>
parents: 271
diff changeset
222 new_periods.append(Period(s,e))
270
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
223 elif e is not None and e < begin:
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
224 continue
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
225 else:
272
af924d27140f scale integrate by delay time
Matt Johnston <matt@ucc.asn.au>
parents: 271
diff changeset
226 new_periods.append(Period(begin, e))
270
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
227 self._on_periods = new_periods
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
228
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
229 def integrate(self):
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
230 self._trim()
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
231 tot = 0
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
232 for s, e in self._on_periods:
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
233 if e is None:
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
234 e = self._timefn()
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
235 tot += (e-s)
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
236 return tot
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
237
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
238
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
239
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
240
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
241
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
242
9d0313b685ae StepIntegrator
Matt Johnston <matt@ucc.asn.au>
parents: 265
diff changeset
243