fixed bug when column was referenced multiple time in a formula.

changed column tagging character to '#' ('$' gave some trouble on the shell...)
This commit is contained in:
Philip Eisenlohr 2012-02-09 15:46:25 +00:00
parent f8013ce4bf
commit 3228cf563c
1 changed files with 5 additions and 4 deletions

View File

@ -30,6 +30,7 @@ class extendableOption(Option):
parser = OptionParser(option_class=extendableOption, usage='%prog options [file[s]]', description = """
Add column(s) with derived values according to user defined arithmetic operation between column(s).
Example: distance to IP coordinates -- "math.sqrt( #ip.x#**2 + #ip.y#**2 + #ip.z#**2 )"
""" + string.replace('$Id$','\n','\\n')
)
@ -72,17 +73,17 @@ for file in files:
for label,formula in zip(options.labels,options.formulas):
table.labels_append(label)
interpolator = []
operands = re.findall(r'\$(.+?)\$',formula)
operands = re.findall(r'#(.+?)#',formula)
for operand in operands:
if not operand in column:
try:
column[operand] = table.labels.index(operand)
except:
parser.error('column %s not found...\n'%operand)
interpolator += ['float(table.data[%i])'%column[operand]]
interpolator += ['float(table.data[%i])'%column[operand]]
for operand in operands:
formula = formula.replace('$'+operand+'$','%e')
evaluator[label] = "'" + formula + "'%(" + ','.join(interpolator) + ")"
formula = formula.replace('#'+operand+'#','%e')
evaluator[label] = "'" + formula + "'%(" + ','.join(interpolator) + ")"
# ------------------------------------------ assemble header ---------------------------------------