How to execute a function once when boolean value changes in python? -
i monitoring external device outputs boolean value of false when condition not met, , boolean value of true when condition met. problem is, boolean value output more once after condition met.
this output looks like:
false false false false true true false false false
so able monitor value , execute simple function, once each time boolean changes false true. have seen other languages have simple "on change" function, know possible , lot easier i'm making out be.
currently have this. sake of explaining this, variable "ext_data" used external data python script monitoring.
while true: if ext_data == true: print("true") pass
this prints "true" every time boolean equal true, , when resets boolean still set true, multiple readings when want one.
thanks helps out!
edit:
updated code using edgedetector class written holdenweb.
ext_data = true/false # returns true or false depending on state # of variable. monitored , # change @ moment, why i'm # running function in while true loop def printer(): print(u'something%s' % serialport.cts) post_to_api('cycle_count') def myfunction(): return ext_data test_subject = edgedetector(myfunction, printer) while true: test_subject.test()
this still returns occasional duplicate posts. mentioned data needs in array edgedetector class work properly, how suggest putting data array without creating infinitely long array?
the way question framed doesn't make clear how external value acquired. must involve storing state information - specifically, last value read.
while possible store in global variable, not practice - while may satisfy immediate needs, if try create re-usable software component based on it, break in event other code in same module decides use same global variable other purposes. class might more satisfactory.
the following class written receive function that, when called, returns value of external variable. passed class's __init__
parameter read_signal
. second parameter, action
, function object should call when detects false
true
transition.
casting in form means can integrate inot program, , should fund having deal many external variables can create edgedetector
each one.
class edgedetector: """detects false true transitions on external signal.""" def __init__(self, reader, action): self.reader = reader self.action = action self.last_value = reader() # initialise value def test(self): new_value = self.reader() if new_value , not self.last_value: self.action() self.last_value = new_value if __name__ == '__main__': # simple self-test vlist = [false, false, false, true, true, false, true, true, false, false, false, true] vgen = (v v in vlist) # generator value sequence def test_reader(): # generate test sequence value = next(vgen) print("read:", value) return value def printer(): print("edge transition detected") test_subject = edgedetector(test_reader, printer) in range(len(vlist)-1): test_subject.test()
normally import module program, making easier re-use , keeping implementation details away main code. when run program self-test code shows sequence of input values , when transitions detected, allowing have little more confidence in code before deploying it. output of self-test is
read: false read: false read: false read: true edge transition detected read: true read: false read: true edge transition detected read: true read: false read: false read: false read: true edge transition detected
Comments
Post a Comment