Assigning variables and sending to database once all values assigned python
Question: This is my first python script. I am trying to get data from an Arduino, read
it on a Raspberry Pi and save it to the database. The code works separately (I
can assign the variable correctly and send the data to the database but can't
seem to get them both to work. I'm not sure my logic works (setting variables
to null and then saving once they all have values). Thanks for the input.

    
    
        import re
        import serial
        import MySQLdb
        import time
    
    db =MySQLdb.connect(host = "localhost",user = "root",passwd = "example", db = "arduino")
    ser =serial.Serial('/dev/ttyACM0',9600) 
    
    humidityPattern = "Humidity\:(\d\d\.\d\d)"
    tempDhPattern = "TemperatureDH\:(\d\d\.\d\d)"
    barometerPattern = "PressureBMP\:(\d\d\.\d\d)"
    tempBmpPattern = "TemperatureBMP\:(\d\d\.\d\d)"
    tempTmpPattern = "TemperatureTMP\:(\d\d\.\d\d)"
    blLightPattern = "BLLight\:(\d+)"
    brLightPattern = "BRLight\:(\d+)"
    frLightPattern = "FRLight\:(\d+)"
    flLightPattern = "FLLight\:(\d+)"
    
    
    while 1:
        line = ser.readline()
        humidity = None
        tempDh = None
        pressure = None
        tempBmp = None
        tempTmp = None
        blLight = None
        brLight = None
        frLight = None
        flLight = None
    
        #Humidity Sensor
        m = re.match(humidityPattern, line)
        if m is not None:      
            humidity = m.group(1)
            print "Humidity is "+humidity
    
        m = re.match(tempDhPattern, line)
        if m is not None:
            tempDh= m.group(1)
            print "Humidity Temp is "+tempDh
    
        #Pressure Sensor
        m = re.match(barometerPattern, line)
        if m is not None:
            pressure = m.group(1)
            print "Pressure is "+tempDh
    
        m = re.match(tempBmpPattern, line)
        if m is not None:
            tempBmp= m.group(1)
            print "Pressure Temp is "+tempBmp
    
        #Temp Sensor
        m = re.match(tempTmpPattern, line)
        if m is not None:
            tempTmp= m.group(1)
            print "Temp is "+tempTmp
    
        #Light Sensors
        m = re.match(blLightPattern, line)
        if m is not None:
            blLight= m.group(1)
            print "BL Light is "+ blLight
    
        m = re.match(brLightPattern, line)
        if m is not None:
            brLight= m.group(1)
            print "BR Light is "+ brLight
    
        m = re.match(frLightPattern, line)
        if m is not None:
            frLight = m.group(1)
            print "FR Light is "+ frLight
    
        m = re.match(flLightPattern, line)
        if m is not None:
            flLight = m.group(1)
            print "FL Light is "+ flLight
    
        if humidity and tempDh and pressure and tempBmp and tempTmp and blLight and brLight and frLight and flLight is not None:
            with db:
                cur = db.cursor()
                cur.execute('insert into weather(humidity, temp_dh, pressure,temp_bmp, temp_tmp, bl_light, br_light, fr_light, fl_light) values("%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s", "%s")'%(humidity, tempDh, pressure, tempBmp, tempTmp, blLight, brLight, frLight, flLight ))
                time.sleep(5)
                print 'upload'
    


Answer: One problem is in the test:

    
    
    if humidity and tempDh and pressure and tempBmp and tempTmp and blLight and brLight and frLight and flLight is not None:
    

which only tests for `None` the very last variable, `flLight`. However if, as
it seems, all the others are either `None` or a non-empty string, this
**should** accidentally work because `None` is falsy and every non-empty
string is truthy.

So a bigger problem is that every time through the loop you're throwing away
every value you've previously read, whether you've saved them or not.

To fix **that** , add a boolean flag `must_init` and change the logic to
something like, at the start of your loop:

must_init = True while True: line = ser.readline() if must_init: humidity =
None tempDh = None pressure = None tempBmp = None tempTmp = None blLight =
None brLight = None frLight = None flLight = None must_init = False

and set `must_init = True` again only at the very end within the `with`
statement right after the now-final `print 'upload'`.

This way, you will null out all variables only (A) the first time or (B) right
after saving their previous values to the DB, which seems to be more correct
logic.

Other simplifying improvements are possible (e.g keep the variables as items
in a dict, so you don't have to enumerate them in the `any` check; have the RE
expression also in a dict keyed by the same name you use in the variables dict
to enormously compact and simplify your code) but the key point is that the
code with the addition of the `must_init` boolean flag as I suggest before
should **work** \-- you can improve it after!-)

