writing to locals

James_Althoff at i2.com James_Althoff at i2.com
Mon Apr 9 17:30:58 EDT 2001


Got it.   (Your suggestion has the added benefit that  the localsDict has
_only_ the c1, c2, ... variables defined and not the other vars in the
method!)

Thanks, Fredrik!


James_Althoff at i2.com wrote:
> ... my class ...
>
>     def setFormula(self,formulaString):
>      self.formula = formulaString
>
>     def getCalculatedValueAt(self,rowIndex):
>         count = self.getColumnCount()  # excludes the calculated column
>         localsDict = locals()
>         for columnIndex in xrange(count):
>             columnID = 'c' + str(columnIndex+1)
>             columnValue = self.getValueAt(rowIndex,columnIndex)
>             localsDict[columnID] = columnValue
>         try:
>             value = eval(self.formula)
>         except:
>             value = 'Invalid formula'
>         return value

         localsDict = {}
         for columnIndex in xrange(count):
             columnID = 'c' + str(columnIndex+1)
             columnValue = self.getValueAt(rowIndex,columnIndex)
             localsDict[columnID] = columnValue
         try:
             value = eval(self.formula, localsDict)
         except:
             value = 'Invalid formula'
         return value

Cheers /F






More information about the Python-list mailing list