-
-
Save objarni/3720182 to your computer and use it in GitHub Desktop.
fuse variation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 TirePressureAlarm(): | |
def __init__(self): | |
self.fuse = Fuse( | |
condition=lambda value : not 17 <= value <= 21, | |
iterator=iter(Sensor()) | |
) | |
def check(self): | |
self.fuse.check() | |
def is_alarm_on(self): | |
return self.fuse.on | |
Alarm = TirePressureAlarm | |
class Fuse(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.on or self.__conditon(value) | |
@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
@emilybache i do agree that
_
is enough though I think that the "you can still get to it" is the wrong reason. Unless you think privat in Java is not private, since you can get to that with reflections...Any mays, i have ssen that you changed it in your code, but you made the alarm_on method only a public variable, letting the interface the user change the value and not only read it. And also i though that the Java-nes of the code was quite fun to pythonify.