""" Calculate percentage level of index for regions inside the Verenigde Nederlanden. """ import sys import math import re from datetime import timedelta, datetime import dateutil.parser # Config PLAYER = False DATE = True TOTAL = False def calculate_buildings(filename='department.txt'): """Count working in departments""" total = 0 players = {} days = {} with open(filename, 'r') as file: for line in file: try: date_str = re.search(r'\s(\d\d).*', line).group(0) date_str = re.sub(r'\s(\d\d|\d\d\d)\s', '', date_str) date = dateutil.parser.parse(date_str) if date.hour >= 20: date += timedelta(days=1) line = re.sub(r'\s\d\d.*', '', line) line = re.sub(r'\[.*\]', '', line) line = line.strip() count = re.search(r'\+\d+', line).group(0) count = count.replace('+', '') count = int(count) total += count if PLAYER: player = re.sub(r'\s\(.*', '', line) if player in players: players[player] += count else: players[player] = count if DATE or TOTAL: date_format = date.strftime("%Y-%m-%d") if date_format in days: days[date_format] += count else: days[date_format] = count except Exception as exception: print('%s %s' % (line, exception)) last_date = datetime.now() for date in sorted(days, reverse=True): date = dateutil.parser.parse(date) difference = last_date - date if difference.days > 1: for i in range(1, difference.days): new_date = date + timedelta(days=i) new_date_formatted = new_date.strftime("%Y-%m-%d") days[new_date_formatted] = 0 last_date = date if PLAYER: print_players(players) if DATE: print_days(days) if TOTAL: print_total(days) def print_players(players): """Print players and reward""" total_reward = 0 print('punten,beloning,naam') for player in sorted(players, key=players.get, reverse=True): worked_times = math.floor(players[player] / 10) reward = worked_times * 2000000000 if worked_times >= 6: reward += 10000000000 total_reward += reward print('%3s,$ %5s,%s' % (players[player], bucks(reward), player)) print(total_reward) def print_days(days): """Print date and points""" for date in sorted(days, reverse=True): print('%s,%3s' % (date, days[date])) def print_total(days): """Print total points in department""" total = 0 for date in sorted(days): total += days[date] last_day = dateutil.parser.parse(date) - timedelta(weeks=2) last_day_format = last_day.strftime("%Y-%m-%d") if last_day_format in days: total -= days[last_day_format] print('%s,%3s' % (date, total)) def bucks(money): """Format money""" str_format = '{:,}'.format(money) new_str = '' for i in range(len(str_format), 0, -4): if str_format[i-4:i] == ',000': new_str = 'k' + new_str else: new_str = str_format[:i] + new_str break new_str = new_str.replace('kkkk', 't') new_str = new_str.replace(',', '.') return new_str if __name__ == '__main__': if len(sys.argv) < 2: calculate_buildings() else: calculate_buildings(sys.argv[1])