-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathr1.py
47 lines (40 loc) · 1.21 KB
/
r1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# Read and process 1 byte at a time (unbuffered), stats list instead of dict
from dataclasses import dataclass
from line_profiler import profile
import sys
@dataclass
class Stat:
station: str
min: float
max: float
sum: float
count: float
@profile
def main():
stats = []
with open(sys.argv[1], 'rb', buffering=0) as f:
while True:
b = f.read(1)
if not b:
break
station = b''
while b and b != b';':
station += b
b = f.read(1)
b = f.read(1) # skip ';'
temp_str = b''
while b and b != b'\n':
temp_str += b
b = f.read(1)
temp = float(temp_str)
s = next((s for s in stats if s.station == station), None)
if s is None:
stats.append(Stat(station=station, min=temp, max=temp, sum=temp, count=1))
continue
s.min = min(s.min, temp)
s.max = max(s.max, temp)
s.sum += temp
s.count += 1
for s in sorted(stats, key=lambda s: s.station):
print(f'{s.station.decode()}={s.min:.1f}/{s.sum/s.count:.1f}/{s.max:.1f}')
main()