conditions can handle string data.

use "#s#label#" format to indicate that column headed by "label" contains strings (not floats)...
This commit is contained in:
Philip Eisenlohr 2012-12-03 14:29:21 +00:00
parent 7d96bb7ce8
commit 2d4cf7da74
1 changed files with 13 additions and 7 deletions

View File

@ -30,7 +30,9 @@ 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 = """
Filter rows according to condition and columns by either white or black listing. Filter rows according to condition and columns by either white or black listing.
Example: every odd row if x coordinate is positive -- " #ip.x# >= 0.0 and #_row_#%2 == 1 )" Examples:
Every odd row if x coordinate is positive -- " #ip.x# >= 0.0 and #_row_#%2 == 1 ).
All rows where label 'foo' equals 'bar' -- " #s#foo# == \"bar\" "
""" + string.replace('$Id$','\n','\\n') """ + string.replace('$Id$','\n','\\n')
) )
@ -82,18 +84,22 @@ for file in files:
positions.append(position) # ...and position positions.append(position) # ...and position
interpolator = [] interpolator = []
for position,operand in enumerate(set(re.findall(r'#(.+?)#',options.condition))): for position,operand in enumerate(set(re.findall(r'#(([s]#)?(.+?))#',options.condition))): # find three groups
options.condition = options.condition.replace('#'+operand+'#','{%i}'%position) options.condition = options.condition.replace('#'+operand[0]+'#',
if operand in specials: { '': '{%i}'%position,
interpolator += ['specials["%s"]'%operand] 's#':'"{%i}"'%position}[operand[1]])
if operand[2] in specials: # special label ?
interpolator += ['specials["%s"]'%operand[2]]
else: else:
try: try:
interpolator += ['float(table.data[%i])'%table.labels.index(operand)] interpolator += ['%s(table.data[%i])'%({ '':'float',
's#':'str'}[operand[1]],
table.labels.index(operand[2]))]
except: except:
parser.error('column %s not found...\n'%operand) parser.error('column %s not found...\n'%operand)
evaluator = "'" + options.condition + "'.format(" + ','.join(interpolator) + ")" evaluator = "'" + options.condition + "'.format(" + ','.join(interpolator) + ")"
# ------------------------------------------ assemble header --------------------------------------- # ------------------------------------------ assemble header ---------------------------------------
table.labels = labels # update with new label set table.labels = labels # update with new label set