Ferdig med uke 2

This commit is contained in:
Trygve 2023-09-24 22:33:25 +02:00
parent ec970373aa
commit d48d576891
1 changed files with 13 additions and 3 deletions

16
uke2.py
View File

@ -27,9 +27,15 @@ def table_from_votes(file_path, num=None):
# Combine all the local results into a list of national party results
p_codes = []
parties = []
total_votes = 0
# Not all parties are in all distrcts. Therofore i need to get the total amount of voters from a party that is everywhere
for p in local_parties:
if p.code == 'A':
total_votes += p.voters
# Put all the kommuneparties into fylker
for p in local_parties:
if p.code not in p_codes:
party = Party('Alle', p.code, p.name, p.voters, p.votes)
party = Party('Alle', p.code, p.name, total_votes, p.votes)
parties.append(party)
p_codes.append(p.code)
else:
@ -39,10 +45,13 @@ def table_from_votes(file_path, num=None):
parties.sort(key=lambda x: x.percent(), reverse=True)
# Create the table
t = '{:^25}|{:^10}|{:^10}\n'.format('Parti', 'Prosent', 'Stemmer')
t = '{:^8}|{:^8}|{:^10}\n'.format('Parti', 'Prosent', 'Stemmer')
t += ("-"*len(t)+"\n")
for n, p in enumerate(parties):
t += (f'{p.code:^25}|{p.percent():^10.2f}|{p.votes:^10}\n')
if p.percent() > 4.0:
t += (f'\033[1m{p.code:^8}|{p.percent():^8.2f}|{p.votes:^10}\033[0m\n')
else:
t += (f'{p.code:^8}|{p.percent():^8.2f}|{p.votes:^10}\n')
if n == num:
break;
return(t)
@ -64,6 +73,7 @@ def get_friends(text):
def main():
print(table_from_votes('2021-09-14_party distribution_1_st_2021.csv'))
print(table_from_votes('2021-09-14_party distribution_1_st_2021.csv', 3))
text = [
'Ali and Per and friends.',