Update table.py for new format

This commit is contained in:
Midgard 2020-12-09 15:07:31 +01:00
parent 228e44838a
commit 3d1ef9309f
Signed by: midgard
GPG key ID: 511C112F1331BBB4

View file

@ -4,51 +4,60 @@ import sys
import re
from datetime import datetime
from collections import defaultdict
from functools import partial
from typing import Mapping, Set
if sys.stdin.isatty():
print("Hint: stdin is a terminal, you may want to do `./make_table.py < verifications.log` instead.", file=sys.stderr)
users = {}
# users[awardee][post_id][verifier]: score
users: Mapping[str, Mapping[str, Mapping[str, int]]] = \
defaultdict(lambda: defaultdict(lambda: defaultdict(lambda: 0)))
for line_nr, line in enumerate(sys.stdin, start=1):
line = line.rstrip()
m = re.fullmatch(r"([^ ]+) ([^ ]+) at ([^ ]+) verified by ([^ ]+) at ([^ ]+)", line)
# {awardee} {post['id']} at {post_time}: {awarder} verified with score {score}
m = re.fullmatch(r"([^ ]+) ([^ ]+) at ([^ ]+): ([^ ]+) verified with score ([^ ]+)", line)
if m:
awardee = m.group(1)
post_id = m.group(2)
post_time = datetime.fromisoformat(m.group(3))
verifier = m.group(4)
verification_time = datetime.fromisoformat(m.group(5))
score = int(m.group(5))
if awardee not in users:
users[awardee] = {}
if post_id not in users[awardee]:
users[awardee][post_id] = defaultdict(dict)
users[awardee][post_id][verifier] = verification_time
users[awardee][post_id][verifier] = score
continue
m = re.fullmatch(r"([^ ]+) ([^ ]+) verification removed by ([^ ]+)", line)
# {awardee} {post['id']} at {post_time}: {awarder} updated their verification's score from {prev_score} to {score}
m = re.fullmatch(r"([^ ]+) ([^ ]+) at ([^ ]+): ([^ ]+) updated their verification's score from ([^ ]+) to ([^ ]+)", line)
if m:
awardee = m.group(1)
post_id = m.group(2)
verifier = m.group(3)
post_time = datetime.fromisoformat(m.group(3))
verifier = m.group(4)
prev_score = int(m.group(5))
score = int(m.group(6))
try:
del users[awardee][post_id][verifier]
except KeyError:
print(f"Line {line_nr}: trying to remove non-existing verification by {verifier}, looks like the file is corrupt!", file=sys.stderr)
users[awardee][post_id][verifier] = score
continue
# If no more verifiers left, clean up the dicts, this makes it easier to count
if not users[awardee][post_id]:
del users[awardee][post_id]
if not users[awardee]:
del users[awardee]
# {awardee} {post['id']} at {post_time}: {awarder} retracted their verification
m = re.fullmatch(r"([^ ]+) ([^ ]+) at ([^ ]+): ([^ ]+) retracted their verification", line)
if m:
awardee = m.group(1)
post_id = m.group(2)
post_time = datetime.fromisoformat(m.group(3))
verifier = m.group(4)
users[awardee][post_id][verifier] = 0
continue
print(f"Couldn't parse line {line_nr}, looks like the file is corrupt: {line}", file=sys.stderr)
for username, user_posts in users.items():
print(f"{username}: {len(user_posts)}")
score = sum(
max(post_verifications.values(), default=0)
for post_verifications in user_posts.values()
)
if score > 0:
print(f"{username}: {score}")