Python for loop saving variables into global array

Thread Starter

zazas321

Joined Nov 29, 2015
936
Hey. Could someone help me figure out the problem I am having with saving variables to global array?
Code:
def update_current_operation(conn):
    #mqttc.publish("global/task_started",0)
    global Device_global
    global Quantity_global
    global Counter_global
    Device_global = []
    Quantity_global=[]
    
    counter=0;
    cur = conn.cursor()
    #Clear the current operation values for the last operation
    query = "UPDATE pack_to_light SET Current_operation=0, left_to_pick = 0"
    cur.execute(query)
    myConnection.commit()
    # update pack to light current operation based on komplektacija
    query1 = "UPDATE pack_to_light p, komplektacija k SET p.Current_operation = k.Quantity WHERE k.Serial = p.Serial and k.Item = p.Item"
    # figure out what items are missing in pack_to_light table
    query2 = "SELECT * FROM komplektacija WHERE komplektacija.Item NOT IN (SELECT Item from pack_to_light)"
    # Select device where current operation is not 0
    query3 = "SELECT Device,Current_operation FROM `pack_to_light` WHERE Current_operation!=0"
    cur.execute(query1)
    myConnection.commit()
    

    cur.execute(query2)
    for Item,Serial,Quantity in cur.fetchall() :
        print("ITEMS NOT FOUND IN PACK_TO_LIGHT")
        print (Item,Serial,Quantity)
    
  
    cur.execute(query3)
    print("ITEMS MATCHED")
    ran = False
    for Device,Quantity in cur.fetchall() :     
        Device_global[counter]= Device
        Quantity_global[counter]= Quantity
        counter=counter+1
        print (Device, Quantity)
        string = Device+"/"+"number_to_pick";
        mqttc.publish(string,Quantity)
        
    print ("number of individual items in set=",counter)
    Counter_global = counter
    mqttc.publish("global/counter",counter)
    mqttc.publish("global/task_started",1)
As you can see, I initialize Device_global and Quantity_global to global variables and set them to an array.
In my for loop, I read the MYSQL table data and want to assign device and quantity data into these global arrays something like that:
Device_global[0] = device1 data
Quantity_global[0] = quantity1 data

Device_global[1] = device2 data
Quantity_global[1] = quantity2 data

Device_global[2] = device3 data
Quantity_global[2] = quantity3 data

Device_global[3] = device3 data
Quantity_global[3] = quantity3 data


The size of array is variable.. My MYSQL data will change and I need to be able to fill an array the size of for loop. Currently, with this code it is returning me an error:
Code:
ITEMS MATCHED
Traceback (most recent call last):
  File "/home/pi/Desktop/Lukas_programming/database_test.py", line 193, in <module>
    update_current_operation(myConnection);
  File "/home/pi/Desktop/Lukas_programming/database_test.py", line 172, in update_current_operation
    Device_global[counter]= Device
IndexError: list assignment index out of range
Which might be because I havent set a size to an array?
 

strantor

Joined Oct 3, 2010
6,798
I do not think this is your problem, but a word of advice...

I have had lots of problems with defining global variables within functions as you are attempting. I found it much worth the effort to learn about utilizing Python's Class methods and declaring class variables in the _init_
 
Top