10 from optparse
import OptionParser
11 from optparse
import Option, OptionValueError
17 parser = argparse.ArgumentParser(
18 description=
'Process output data file saved as dictionaries. It has two modality: print selected fields for all lines or print a particular line where a filed has a given value. Example of usage: process_output.py --soft -s To E S -f log.3.native-2-no-red. process_output.py --soft --search_field EV0 --search_value 5.67750116023 -f log.3.native-2-no-red')
23 help=
"file name to process")
28 help=
"Specify all fields to be printed. Multiple flags will append a list of fields to be printed")
31 dest=
"single_column_field",
32 help=
"Specify a single column field to be printed. It will be printed as a column. If the field name is not complete, it will print all fields whose name contain the queried string.")
38 help=
"print the fields contained in the file")
44 help=
"print the fields contained in the file (only stat2)")
48 dest=
"print_raw_number",
49 help=
"print the selected raw")
55 help=
"Soft match. Closest matching field will be printed, e.g. S will give Step_Number, En will give energy, etc. ")
59 help=
"Search a line from the file. Specify the field to be searched for. ")
63 help=
"Search a line from the file. Specify the value to be searched for. ")
69 help=
"Print the frame number as initial column")
71 result = parser.parse_args()
74 class MultipleOption(Option):
75 ACTIONS = Option.ACTIONS + (
"extend",)
76 STORE_ACTIONS = Option.STORE_ACTIONS + (
"extend",)
77 TYPED_ACTIONS = Option.TYPED_ACTIONS + (
"extend",)
78 ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + (
"extend",)
80 def take_action(self, action, dest, opt, value, values, parser):
81 if action ==
"extend":
82 values.ensure_value(dest, []).append(value)
94 option_class=MultipleOption,
95 usage=
'Process output data file saved as dictionaries. It has two modality: print selected fields for all lines or print a particular line where a filed has a given value. Example of usage: process_output.py --soft -s To -s E -s S -f log.3.native-2-no-red. process_output.py --soft --search_field EV0 --search_value 5.67750116023 -f log.3.native-2-no-red')
100 help=
"file name to process")
106 help=
"Specify all fields to be printed. Multiple flags will append a list of fields to be printed")
109 dest=
"single_column_field",
110 help=
"Specify a single column field to be printed. It will be printed as a column. If the field name is not complete, it will print all fields whose name contain the queried string.")
116 help=
"print the fields contained in the file")
122 help=
"print the fields contained in the file (only stat2)")
126 dest=
"print_raw_number",
127 help=
"print the selected raw")
133 help=
"Soft match. Closest matching field will be printed, e.g. S will give Step_Number, En will give energy, etc. ")
137 help=
"Search a line from the file. Specify the field to be searched for. ")
141 help=
"Search a line from the file. Specify the value to be searched for. ")
147 help=
"Print the frame number as initial column")
149 (result, args) = parser.parse_args()
156 if not result.filename
is None:
157 f = open(result.filename,
"r")
159 print "Error: No file name provided. Use -h for help"
163 for line
in f.readlines():
167 if "STAT2HEADER" in klist:
171 if "STAT2HEADER" in str(k):
172 if result.print_header:
178 for k
in sorted(stat2_dict.iteritems(), key=operator.itemgetter(1))]
180 for k
in sorted(stat2_dict.iteritems(), key=operator.itemgetter(1))]
183 invstat2_dict.update({stat2_dict[k]: k})
192 if result.print_fields:
197 print key[0:100],
"... omitting the rest of the string (>100 characters)"
202 match_strictness = 1.0
203 if result.soft_match:
204 match_strictness = 0.1
207 if not result.fields
is None:
211 for field
in result.fields:
212 found_entries = difflib.get_close_matches(
217 if len(found_entries) == 0:
218 print "Error: field " + field +
" non found"
221 field_list.append(found_entries[0])
224 s0 =
' '.join([
"%20s" % (field)
for field
in field_list])
228 f = open(result.filename,
"r")
230 for line
in f.readlines():
235 print "# Warning: skipped line number " + str(line_number) +
" not a valid line"
238 s0 =
' '.join([
"%20s" % (str(d[field]))
for field
in field_list])
242 s0 =
' '.join([
"%20s" % (str(d[invstat2_dict[field]]))
243 for field
in field_list])
244 if not result.nframe:
247 print str(line_number)+
" > " + s0
251 if not result.single_column_field
is None:
254 if result.single_column_field
in k:
257 f = open(result.filename,
"r")
259 for line
in f.readlines():
264 print "# Warning: skipped line number " + str(line_number) +
" not a valid line"
267 for key
in field_list:
272 for key
in field_list:
273 print key, d[invstat2_dict[key]]
277 if (
not result.search_field
is None)
and (
not result.search_value
is None):
280 found_entries = difflib.get_close_matches(
285 if len(found_entries) == 0:
286 print "Error: field " + results.search_field +
" non found"
289 corrected_field = found_entries[0]
291 f = open(result.filename,
"r")
293 for line
in f.readlines():
298 print "# Warning: skipped line number " + str(line_number) +
" not a valid line"
302 if (str(d[corrected_field]) == result.search_value):
308 if (str(d[invstat2_dict[corrected_field]]) == result.search_value):
310 print key, d[invstat2_dict[key]]
313 if not result.print_raw_number
is None:
316 f = open(result.filename,
"r")
318 for line
in f.readlines():
321 if (line_number == int(result.print_raw_number)):
325 print "# Warning: skipped line number " + str(line_number) +
" not a valid line"
331 if (line_number == int(result.print_raw_number) + 1):
335 print "# Warning: skipped line number " + str(line_number) +
" not a valid line"
338 print key, d[invstat2_dict[key]]
IMP::kernel::OptionParser OptionParser