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 raise ValueError(
"No file name provided. Use -h for help")
162 for line
in f.readlines():
166 if "STAT2HEADER" in klist:
170 if "STAT2HEADER" in str(k):
171 if result.print_header:
177 for k
in sorted(stat2_dict.iteritems(), key=operator.itemgetter(1))]
179 for k
in sorted(stat2_dict.iteritems(), key=operator.itemgetter(1))]
182 invstat2_dict.update({stat2_dict[k]: k})
191 if result.print_fields:
196 print key[0:100],
"... omitting the rest of the string (>100 characters)"
201 match_strictness = 1.0
202 if result.soft_match:
203 match_strictness = 0.1
206 if not result.fields
is None:
210 for field
in result.fields:
211 found_entries = difflib.get_close_matches(
216 if len(found_entries) == 0:
217 raise ValueError(
"field " + field +
" non found")
219 field_list.append(found_entries[0])
222 s0 =
' '.join([
"%20s" % (field)
for field
in field_list])
226 f = open(result.filename,
"r")
228 for line
in f.readlines():
233 print "# Warning: skipped line number " + str(line_number) +
" not a valid line"
236 s0 =
' '.join([
"%20s" % (str(d[field]))
for field
in field_list])
240 s0 =
' '.join([
"%20s" % (str(d[invstat2_dict[field]]))
241 for field
in field_list])
242 if not result.nframe:
245 print str(line_number)+
" > " + s0
249 if not result.single_column_field
is None:
252 if result.single_column_field
in k:
255 f = open(result.filename,
"r")
257 for line
in f.readlines():
262 print "# Warning: skipped line number " + str(line_number) +
" not a valid line"
265 for key
in field_list:
270 for key
in field_list:
271 print key, d[invstat2_dict[key]]
275 if (
not result.search_field
is None)
and (
not result.search_value
is None):
278 found_entries = difflib.get_close_matches(
283 if len(found_entries) == 0:
284 raise ValueError(
"field " + results.search_field +
" non found")
286 corrected_field = found_entries[0]
288 f = open(result.filename,
"r")
290 for line
in f.readlines():
295 print "# Warning: skipped line number " + str(line_number) +
" not a valid line"
299 if (str(d[corrected_field]) == result.search_value):
305 if (str(d[invstat2_dict[corrected_field]]) == result.search_value):
307 print key, d[invstat2_dict[key]]
310 if not result.print_raw_number
is None:
313 f = open(result.filename,
"r")
315 for line
in f.readlines():
318 if (line_number == int(result.print_raw_number)):
322 print "# Warning: skipped line number " + str(line_number) +
" not a valid line"
328 if (line_number == int(result.print_raw_number) + 1):
332 print "# Warning: skipped line number " + str(line_number) +
" not a valid line"
335 print key, d[invstat2_dict[key]]
IMP::kernel::OptionParser OptionParser