6 from __future__
import print_function
12 from optparse
import OptionParser
13 from optparse
import Option, OptionValueError
19 parser = argparse.ArgumentParser(
20 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')
25 help=
"file name to process")
30 help=
"Specify all fields to be printed. Multiple flags will append a list of fields to be printed")
33 dest=
"single_column_field",
34 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.")
40 help=
"print the fields contained in the file")
46 help=
"print the fields contained in the file (only stat2)")
50 dest=
"print_raw_number",
51 help=
"print the selected raw")
57 help=
"Soft match. Closest matching field will be printed, e.g. S will give Step_Number, En will give energy, etc. ")
61 help=
"Search a line from the file. Specify the field to be searched for. ")
65 help=
"Search a line from the file. Specify the value to be searched for. ")
71 help=
"Print the frame number as initial column")
73 result = parser.parse_args()
76 class MultipleOption(Option):
77 ACTIONS = Option.ACTIONS + (
"extend",)
78 STORE_ACTIONS = Option.STORE_ACTIONS + (
"extend",)
79 TYPED_ACTIONS = Option.TYPED_ACTIONS + (
"extend",)
80 ALWAYS_TYPED_ACTIONS = Option.ALWAYS_TYPED_ACTIONS + (
"extend",)
82 def take_action(self, action, dest, opt, value, values, parser):
83 if action ==
"extend":
84 values.ensure_value(dest, []).append(value)
96 option_class=MultipleOption,
97 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')
102 help=
"file name to process")
108 help=
"Specify all fields to be printed. Multiple flags will append a list of fields to be printed")
111 dest=
"single_column_field",
112 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.")
118 help=
"print the fields contained in the file")
124 help=
"print the fields contained in the file (only stat2)")
128 dest=
"print_raw_number",
129 help=
"print the selected raw")
135 help=
"Soft match. Closest matching field will be printed, e.g. S will give Step_Number, En will give energy, etc. ")
139 help=
"Search a line from the file. Specify the field to be searched for. ")
143 help=
"Search a line from the file. Specify the value to be searched for. ")
149 help=
"Print the frame number as initial column")
151 (result, args) = parser.parse_args()
158 if not result.filename
is None:
159 f = open(result.filename,
"r")
161 raise ValueError(
"No file name provided. Use -h for help")
164 for line
in f.readlines():
166 klist = list(d.keys())
168 if "STAT2HEADER" in klist:
172 if "STAT2HEADER" in str(k):
173 if result.print_header:
179 for k
in sorted(stat2_dict.items(), key=operator.itemgetter(1))]
181 for k
in sorted(stat2_dict.items(), key=operator.itemgetter(1))]
184 invstat2_dict.update({stat2_dict[k]: k})
193 if result.print_fields:
198 print(key[0:100],
"... omitting the rest of the string (>100 characters)")
203 match_strictness = 1.0
204 if result.soft_match:
205 match_strictness = 0.1
208 if not result.fields
is None:
212 for field
in result.fields:
213 found_entries = difflib.get_close_matches(
218 if len(found_entries) == 0:
219 raise ValueError(
"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 raise ValueError(
"field " + results.search_field +
" non found")
288 corrected_field = found_entries[0]
290 f = open(result.filename,
"r")
292 for line
in f.readlines():
297 print(
"# Warning: skipped line number " + str(line_number) +
" not a valid line")
301 if (str(d[corrected_field]) == result.search_value):
307 if (str(d[invstat2_dict[corrected_field]]) == result.search_value):
309 print(key, d[invstat2_dict[key]])
312 if not result.print_raw_number
is None:
315 f = open(result.filename,
"r")
317 for line
in f.readlines():
320 if (line_number == int(result.print_raw_number)):
324 print(
"# Warning: skipped line number " + str(line_number) +
" not a valid line")
330 if (line_number == int(result.print_raw_number) + 1):
334 print(
"# Warning: skipped line number " + str(line_number) +
" not a valid line")
337 print(key, d[invstat2_dict[key]])
IMP::kernel::OptionParser OptionParser