slaaphistogram/hist.py

60 lines
1.2 KiB
Python
Raw Normal View History

2021-01-26 12:40:07 +01:00
#!/usr/bin/env python3
import sys
import re
unknown = 0
skipped = 0
hours = [0] * 24
for line in sys.stdin:
m = re.match(r'([0-9]*):.*,([0-9])', line.strip())
assert m
if m.group(2) == "0":
skipped += 1
elif m.group(2) == "2":
unknown += 1
else:
assert m.group(2) == "1"
hours[int(m.group(1))] += 1
BLOCKS = "▁▂▃▄▅▆▇█"
HEIGHT = 20
max_val = max([*hours, unknown, skipped])
hours_norm = [h / max_val for h in hours]
unknown_norm = unknown / max_val
skipped_norm = skipped / max_val
def print_block(value, row):
height_value = value * HEIGHT - (HEIGHT - row)
if height_value >= 0.99:
print(BLOCKS[-1], end=" ")
elif height_value < 0.01:
if HEIGHT - 1 == row and value > 0:
print("", end=" ")
else:
print(" ", end=" ")
else:
print(BLOCKS[int(height_value * len(BLOCKS))], end=" ")
cutoff = 12
hour_indices = [*range(cutoff, 24), *range(0, cutoff)]
for row in range(HEIGHT):
print("", end=" ")
for h in hour_indices:
print_block(hours_norm[h], row)
print("", end=" ")
print_block(skipped_norm, row)
print("", end=" ")
print_block(unknown_norm, row)
print()
for h in hour_indices[::2]:
print(f"{h: >2}", end=" ")
print(" skp unk")