diff --git a/rank_progress.py b/rank_progress.py --- a/rank_progress.py +++ b/rank_progress.py @@ -11,6 +11,33 @@ parser.add_argument("-c", "--country-cod args = parser.parse_args() +class Record: + def __init__(self, tokens, source): + self.pin = int(tokens[0]) + self.country_code = tokens[2] + self.rating_before = int(tokens[8]) + self.rounded_before = round_rating(self.rating_before) + self.rating_after = int(tokens[9]) + self.rounded_after = round_rating(self.rating_after) + + tournament_code = tokens[5] + self.date = datetime.strptime(tournament_code[1:7], "%y%m%d") + + self.source = source + + @classmethod + def parse(cls, line): + tokens = re.split(r" {2,}", line.strip()) + + if len(tokens) != 10: + return None + + return cls(tokens, line) + + def __str__(self): + return self.source + + def parse_record(record): types = [int, str, str, str, str, str, int, int, int, int] columns = [f(token) for (f, token) in zip(types, record)] + [datetime.strptime(record[5][1:7], "%y%m%d")] @@ -27,27 +54,27 @@ to = datetime.strptime(args.to, "%Y%m%d" with open("/tmp/all.hst") as f: s = f.read() -records = [re.split(r" {2,}", line.strip()) for line in s.splitlines()] -records = filter(lambda rec: len(rec) == 10, records) -records = map(parse_record, records) -national_records = filter(lambda rec: rec[2] == args.country_code, records) -players = groupby(national_records, lambda rec: rec[0]) +records = [Record.parse(line) for line in s.splitlines()] +records = filter(lambda rec: rec is not None, records) +national_records = filter(lambda rec: rec.country_code == args.country_code, records) +player_records = groupby(national_records, lambda rec: rec.pin) -for (pin, recs) in players: +for (pin, recs) in player_records: tourneys = list(recs) - rounded_rating = round_rating(tourneys[0][9]) + rounded_rating = tourneys[0].rounded_before steps = [] for r in tourneys: - if rounded_rating != round_rating(r[8]): - rounded_rating = round_rating(r[8]) + # omit reset ratings + if rounded_rating != r.rounded_before: + rounded_rating = r.rounded_before - if r[9]-rounded_rating >= 50 or rounded_rating-r[9] > 100: + if r.rating_after-rounded_rating >= 50 or rounded_rating-r.rating_after > 100: steps.append(r) - rounded_rating = round_rating(r[9]) + rounded_rating = r.rounded_after - steps = [r for r in steps if since <= r[10] <= to] + steps = [r for r in steps if since <= r.date <= to] if steps: - print(steps) - + print("\n".join(map(str, steps))) + print()