Skip to content

Instantly share code, notes, and snippets.

@Neppord
Created September 13, 2012 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Neppord/3717798 to your computer and use it in GitHub Desktop.
Save Neppord/3717798 to your computer and use it in GitHub Desktop.
sticky trigger
import random
class Sensor(object):
OFFSET = 16
def pop_next_pressure_psi_value(self):
pressure_telemetry_value = self.sample_pressure()
return Sensor.OFFSET + pressure_telemetry_value
def __iter__(self):
return iter(self.pop_next_pressure_psi_value, Ellipsis)
@staticmethod
def sample_pressure():
# placeholder implementation that simulate a real sensor in a real tire
pressure_telemetry_value = 6 * random.random() * random.random()
return pressure_telemetry_value
class Alarm():
def __init__(self):
self.trigger = StickyTrigger(
condition=lambda value : not 17 <= value <= 21,
iterator=iter(Sensor())
)
def check(self):
self.trigger.check()
def is_alarm_on(self):
return self.trigger.on
class StickyTrigger(object):
def __init__(self, condition, iterator):
self.__conditon = condition
self.__iterator = iterator
self.__on = False
def check(self):
value = self.__iterator.next()
self.__on = self.__conditon(value) or self.on
@property
def on(self):
return self.__on
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment