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 = """ 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). 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') """ + string.replace('$Id$','\n','\\n')
) )
@ -72,7 +73,7 @@ for file in files:
for label,formula in zip(options.labels,options.formulas): for label,formula in zip(options.labels,options.formulas):
table.labels_append(label) table.labels_append(label)
interpolator = [] interpolator = []
operands = re.findall(r'\$(.+?)\$',formula) operands = re.findall(r'#(.+?)#',formula)
for operand in operands: for operand in operands:
if not operand in column: if not operand in column:
try: try:
@ -81,7 +82,7 @@ for file in files:
parser.error('column %s not found...\n'%operand) 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: for operand in operands:
formula = formula.replace('$'+operand+'$','%e') formula = formula.replace('#'+operand+'#','%e')
evaluator[label] = "'" + formula + "'%(" + ','.join(interpolator) + ")" evaluator[label] = "'" + formula + "'%(" + ','.join(interpolator) + ")"
# ------------------------------------------ assemble header --------------------------------------- # ------------------------------------------ assemble header ---------------------------------------