Home > database >  Running a function inside of a loop only once
Running a function inside of a loop only once

Time:02-18

This kind of follows up on a question I asked prior, but I have a function, we'll call it get_temp(), that needs to be looped in order to gather the data it needs to call for another function, we'll call it notification(), to run if the if statement is satisfied. This all works great, but once it does satisfy the if statement, I need it to only send auxFunction once.

This section of the code works by getting temperature from a thermocouple, and then I'm using an if statement to call for a specific function when the temp == a specific value. The temperature data is looked at every 2 seconds in the loop, which is necessary to see the changes in the data to satisfy the if statement eventually, so I can't just run it outside of the loop.

This is the part of the code that grabs the thermocouple value:

def grab_temp(self):
    temp = self.board.temp_sensor
    print("Temperature is "   str(round(temp))   " degrees")
    temp=int(temp)
    if temp == 200:
        self.notification()

This is the concept of the code, but not the exact code. It's written a little differently, but performs as it's written above and the notification function does more than just print. just for the sake of example, I'm just having it print something

And then:

def notification(self):
    print("Testing")

Again, this all works, but since the grab_temp function is inside of the loop of the rest of the code, it loops every two seconds, which in turn, prints every two seconds when the value is 200, where I need it to just perform the notification function once, when temp == 200. This is problematic because the code that will be in the notification function will be notifying me of the temperature, so I can't have it notifying me every two seconds while the temperature is 200 degrees.

What would be the best way to go about achieving this, in this instance? I've tried a few other things recommended on similar questions, but none of them worked. All that I tried returned some kind of error, which prevented the rest of the code from functioning properly.

I didn't write the entirety of this of this whole project, I'm just modifying it to do some things I want it to do, and I don't have a ton of experience in coding, but I'm learning more and more as I work through this project, and have enjoyed making these modifications, and am still learning some basics on the side.

CodePudding user response:

I sadly do not have the ability to comment currently due to my lack of reputation on this website. However, I think a solution you could consider is adding a global boolean variable called 'uncalled' that defaults to true and adjust it once you've called the self.notification() portion of your code. Beginning of Script:

global uncalled
uncalled = True

Printing Warning

global uncalled
if temp == 200 and uncalled:
   self.notification()
   uncalled = False

Using the keyword 'global' here should change it throughout your entire code and not just within the function. If this doesn't answer your question, would you mind clarifying for me in my comments? Thanks!

Extra Example:

global uncalled
uncalled = True
def function(x):
    global uncalled
    if x and uncalled:
        print('hit')
        uncalled = False
for i in range(5):
    x = True
    function(x)

Returns

hit

CodePudding user response:

Possible solution without explicitly using global.

Add a boolean parameter to grab_temp, we'll call it notify. Set an initial value for notify=True. At the end of grab_temp, return the appropriate boolean to turn on/off the notification. Assign grab_temp to notify within your loop.

This version will disable notifications for consecutive calls, but re-enable notifications when the condition is no longer met. You can modify what's returned from grab_temp if you only want a notification for the very first time the condition is met.

temp_values = [1,200,200,1,200,200,200,1,200]

def notification():
    print('notification')

def grab_temp(i, notify=True):
    temp = temp_values[i]
    print("Temp is", temp)
    check_temp = temp == 200
    if notify and check_temp:
        notification()
    return not check_temp

notify = True
for i in range(len(temp_values)):
    notify = grab_temp(i, notify)

Example result

Temp is 1
Temp is 200
notification
Temp is 200
Temp is 1
Temp is 200
notification
Temp is 200
Temp is 200
Temp is 1
Temp is 200
notification

Modified grab_temp to only notify for the very first time the condition is met.

def grab_temp(i, notify=True):
    temp = temp_values[i]
    print("Temp is", temp)
    check_temp = temp == 200
    if notify and check_temp:
        notification()
        return False
    else:
        return notify
Temp is 1
Temp is 200
notification
Temp is 200
Temp is 1
Temp is 200
Temp is 200
Temp is 200
Temp is 1
Temp is 200
  • Related