Python Forum
Please review my logic! Something is off
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Please review my logic! Something is off
#1
Hi everybody,
I'm writing a module for a program and the purpose is to check a row in a db- if "new" run some function.
I was pretty sure this logic would work (and was pretty proud of myself), alas, it doesn't work. It's supposed to run every 2 seconds to compare the newest row as a db "listener", of sorts.

My logic:
connect to db, query for the newest row, compare that row with variable, if not equal, run function, store newest row to variable, else close.

Here's the code:

from Logger import Logger
from sendSMS import sendSMS
from Needles import dbUser, dbHost, dbPassword, pull_stmt
import pyodbc
import time

#set last_sent to something
last_sent = ''

def sendListener():
    #connect to db
    cnxn = pyodbc.connect('UID='+dbUser+';PWD='+dbPassword+';DSN='+dbHost)
    cursor = cnxn.cursor()
    #run query to pull newest row
    cursor.execute(pull_stmt)
    results = cursor.fetchone()

    #if query results different from results stored in last_sent, run function. 
    #then set last_sent object to the query results for next comparison.
    if results != last_sent:
        sendSMS()
        last_sent = results
    else:
        cnxn.close()

# a loop to run the check every 2 seconds- as to lessen cpu usage
def sleepLoop():
    while 0 == 0:
        sendListener()
        time.sleep(2.0)

sleepLoop()
The error I'm getting is that 'local variable last_sent' referenced before assignment.
This confuses me for 2 reasons.
A) I thought I set last_sent to '' as a global variable/object.
B) In order for my comparison logic to work, I can't set last_sent within the sendListener() function before the if/else!


Is there a better way of implementing this type of thing?
What am I doing wrong here?

Thank you!
Reply
#2
Generally, global variables are avoided. They tend to create software design problems and bugs.

To get around this, make a class with last_sent as a field and sendListener() as a method. Then, you can reference self.last_sent within sendListener() without an issue.
Reply
#3
Thanks for the tip... I'm still getting accustomed to python syntax so I'll have to look into implementing that. This program has about 6 or 7 modules so it might be a bit of work.

lol funny thing... i realized my issue was that last_sent was not declared as global within the function. I fixed that and it looks like my logic still isn't working. Instead of checking against the last sent message and not sending if same, it's just sending the same text 50 times a second or so...

so I inadvertantly created an sms bomber denial of service program :P


from Logger import Logger
from sendSMS import sendSMS
from Needles import dbUser, dbHost, dbPassword, pull_stmt
import pyodbc
import time

last_sent = ''

def sendListener():
    global last_sent
    cnxn = pyodbc.connect('UID='+dbUser+';PWD='+dbPassword+';DSN='+dbHost)
    cursor = cnxn.cursor()
    cursor.execute(pull_stmt)
    results = cursor.fetchone()
    if results != last_sent:
        sendSMS()
        last_sent = results
    else:
        cnxn.close()

def sleepLoop():
    while 0 == 0:
        sendListener()
        time.sleep(2.0)

sleepLoop()
Reply
#4
I believe my error is related to some other functions in other modules manipulating the db record along the line... so when my comparison runs, it's never the same, hence it keeps sending it.

Anyways, my initial issue has been resolved. I'm still a newbie and don't fully understand how to implement classes/methods... I'm just getting going with writing functions and designing a hierarchy of modules that can call the functions in a proper order to make a functional program.

Will have to look into that.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020