| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 | """Calculate donations"""import sysimport reimport mathdef sum_donations(filename='donations.txt'):    """Count total donations"""    resources = {        'bbl': {},        'kg': {},        'pcs': {},        'G': {},        'g': {},        '$': {},    }    with open(filename, 'r') as file:        for line in file:            try:                donation = re.search(r'\d.*?(bbl|kg|pcs|G|g|\$)', line).group(0)                player = re.search(r'.*?\s\t', line).group(0).strip()                amount, resource = donation.split(' ')                amount = int(amount.replace('.', ''))                if player not in resources[resource]:                    resources[resource][player] = 0                resources[resource][player] += amount                if 'total' not in resources[resource]:                    resources[resource]['total'] = 0                resources[resource]['total'] += amount            except Exception as exception:                print('exception')                print('%s %s' % (line, exception))    return resourcesdef print_resources(resources):    """Print donations per resource"""    for resource, players in resources.items():        print(resource)        count = 1        for player in sorted(players, key=players.get, reverse=True):            print('%20s %s' % (bucks(players[player]), player))            if count >= 10:                break            count += 1def calc_reward(resources):    """Calculate reward"""    reward = {        'bbl':    130,        'kg':     120,        'g':     1120,        'pcs': 840000,    }    resource_koef = {        'bbl': 1000000000,        'kg':  1105000000,        'g':    100000000,        'pcs':     150000,    }    player_reward = {}    for resource, players in resources.items():        if resource not in reward:            continue        for player, amount in players.items():            if player == 'total':                continue            if player not in player_reward:                player_reward[player] = 0            tmp_reward = reward[resource] * amount            koef = math.floor(amount / resource_koef[resource])            player_reward[player] += tmp_reward + round(tmp_reward / 100 * koef * 2)    return player_rewarddef print_reward(players):    """print player rewards"""    total = 0    for player, reward in sorted(players.items(), key=lambda x: x[1], reverse=True):        total += reward        print('%20s %s' % (bucks(reward), player))    print('total: %s' % bucks(total))def bucks(integer):    """Format number"""    return '{:,}'.format(integer).replace(',', '.')if __name__ == '__main__':    RESOURCES = None    if len(sys.argv) < 2:        RESOURCES = sum_donations()    else:        RESOURCES = sum_donations(sys.argv[1])    PLAYERS = calc_reward(RESOURCES)    print_reward(PLAYERS)    print_resources(RESOURCES)
 |