comparison tests/sort_by_time.py @ 83:729738462297 draft

"planemo upload commit c0ffc68aec5836d5b20b543106493056a87edf57"
author rhpvorderman
date Wed, 15 Sep 2021 12:24:06 +0000
parents
children
comparison
equal deleted inserted replaced
82:a103134ee6e0 83:729738462297
1 #!/usr/bin/env python3
2
3 """Small script to profile bash scripts that have been run with the following
4 code inside:
5
6 exec 5> debug_output.txt
7 BASH_XTRACEFD="5"
8 PS4='$(date +%s.%N) $LINENO: '
9 set -x
10
11
12 """
13 import calendar
14 import time
15 import sys
16
17 import re
18
19 SECONDS_FINDER = re.compile(r"^(\d+.\d+).*")
20
21
22 def file_to_timestamped_lines(input_file):
23 with open(input_file, "rt") as file_h:
24 for line in file_h:
25 time_since_epoch = float(SECONDS_FINDER.search(line).group(1))
26 yield time_since_epoch, line
27
28
29 def time_delta_lines(input_file):
30 timestamped_lines = file_to_timestamped_lines(input_file)
31 current_time, current_line = next(timestamped_lines)
32 for next_time, next_line in timestamped_lines:
33 time_since = next_time - current_time
34 yield time_since, current_line
35 current_time = next_time
36 current_line = next_line
37
38
39 if __name__ == "__main__":
40 input_file = sys.argv[1]
41 # Sort by time ascending order.
42 sorted_time = sorted(time_delta_lines(input_file), key=lambda tup: tup[0])
43 for time_since, line in sorted_time:
44 if time_since > 60*60*24*365:
45 # big times are probably nonsensical parsing errors.
46 continue
47 print(time_since, line.strip())