check_numberdealers/numberdealers/numbers_per_user.py

39 lines
873 B
Python
Raw Normal View History

#!/usr/bin/env python3
from collections import defaultdict
from typing import Dict, Optional, Iterable
from . import parse_numberdealers
def analyze_users(numbers: Iterable[parse_numberdealers.Message]) -> Dict[Optional[str], int]:
stats = defaultdict(lambda: 0)
for msg in numbers:
stats[msg.username] += 1
return dict(stats)
def report_users(numbers):
stats = analyze_users(numbers)
for username, count in sorted(stats.items(), key=lambda x: x[1]):
print(f"{count:5d} {username}")
def main():
import sys
numbers, errors = parse_numberdealers.parse(sys.stdin)
if numbers == [] and errors == []:
print("No input data")
else:
if numbers == []:
print("No valid number messages!")
else:
print(f"Checked from {numbers[0].recognized_number} up to {numbers[-1].recognized_number}")
report_users(numbers)
if __name__ == '__main__':
main()