comparison py/utils.py @ 570:afe2eb17723e

untested fridge.integrator
author Matt Johnston <matt@ucc.asn.au>
date Wed, 11 Nov 2015 00:20:20 +0800
parents d7575050e4be
children af924d27140f
comparison
equal deleted inserted replaced
569:d7575050e4be 570:afe2eb17723e
172 else: 172 else:
173 D(msg) 173 D(msg)
174 174
175 class StepIntegrator(object): 175 class StepIntegrator(object):
176 """ 176 """
177 Takes on/off events and a monotonically increasing timefn. Returns the integral
178 of (now-limittime, now) over those events.
179
177 >>> s = StepIntegrator(lambda: t, 40) 180 >>> s = StepIntegrator(lambda: t, 40)
178 >>> t = 1 181 >>> t = 1
179 >>> s.turn(1) 182 >>> s.turn(1)
180 >>> t = 10 183 >>> t = 10
181 >>> s.turn(0) 184 >>> s.turn(0)
209 def __init__(self, timefn, limittime): 212 def __init__(self, timefn, limittime):
210 # _on_periods is a list of [[start, end]]. End is None if still on 213 # _on_periods is a list of [[start, end]]. End is None if still on
211 self._on_periods = [] 214 self._on_periods = []
212 self._timefn = timefn 215 self._timefn = timefn
213 self._limittime = limittime 216 self._limittime = limittime
217
218 def set_limit(self, limittime):
219 if self._limittime == limittime:
220 return
221 self._limittime = limittime
222 self.trim()
214 223
215 def turn(self, value): 224 def turn(self, value):
216 if not self._on_periods: 225 if not self._on_periods:
217 if value: 226 if value:
218 self._on_periods.append([self._timefn(), None]) 227 self._on_periods.append([self._timefn(), None])