Search code examples
pythondictionaryparameters

Get parameter to dictionary function


Hi i am writing program for separating values of current and voltage from serial messages from some device. I eliminate a lot of if|elif using dictionaries.I am calling function getValues from main while function and giving this function parameter msg (stored message from serial port). I have error in function def V1 that variable doesn't exist. I am new in python and still learning. Is there some option to pass msg parameter in dictionaris fuction or something similar?

def getValues(msg,typeVal,corI3,corI2,corI1):
    busValues = {'V1':V1,
                 'V2':V2,
                 'V3':V3,
                 'I1':I1,
                 'I2':I2,
                 'I3':I3,
                 'Vb':Vb,
                 'Vl':Vl,
                 'a':a,
                 'Va':Va,
                 'Ia':Ia,
                 'Ib':Ib}
    busValues.get(typeVal)(typeVal)

def V1(value):
    global index
    global strEnd1
    global strEnd2
    global strEnd3
    index = msg.find('V1:')
    strEnd1 = 30+corI3+corI2+corI1
    strEnd2 = 31+corI3+corI2+corI1
    strEnd3 = 32+corI3+corI2+corI1

Solution

  • looks like you only need to add msg to your functions arguments. Same applies for cor parameters you seem to need. That would give you:

    def V1(value, msg, corI3, corI2, corI1):
    

    From there, you should be able to call it as:

    busValues.get(typeVal)(typeVal, msg, corI3, corI2, corI1)