2023-09-21 14:46:23 +00:00
|
|
|
import re
|
|
|
|
|
2023-09-19 21:05:39 +00:00
|
|
|
# Task 1
|
|
|
|
class Party:
|
|
|
|
def __init__(self, fylke, code, name, voters, votes):
|
|
|
|
self.fylke = fylke
|
|
|
|
self.code = code
|
|
|
|
self.name = name
|
|
|
|
self.voters = voters
|
|
|
|
self.votes = votes
|
|
|
|
|
|
|
|
def percent(self):
|
|
|
|
return self.votes*100/self.voters
|
|
|
|
|
|
|
|
def table_from_votes(file_path, num=None):
|
|
|
|
with open(file_path) as f:
|
|
|
|
lines = f.readlines()
|
|
|
|
f.close()
|
|
|
|
|
|
|
|
# Read all the entries into a list of parties
|
|
|
|
local_parties = []
|
|
|
|
for l in lines[1:]:
|
|
|
|
l = l.split(';')
|
|
|
|
party = Party(fylke=l[1], code=l[6], name=l[7], voters=int(l[9]), votes=int(l[12]))
|
|
|
|
local_parties.append(party)
|
|
|
|
|
|
|
|
# Combine all the local results into a list of national party results
|
|
|
|
p_codes = []
|
|
|
|
parties = []
|
2023-09-24 20:33:25 +00:00
|
|
|
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
|
2023-09-19 21:05:39 +00:00
|
|
|
for p in local_parties:
|
|
|
|
if p.code not in p_codes:
|
2023-09-24 20:33:25 +00:00
|
|
|
party = Party('Alle', p.code, p.name, total_votes, p.votes)
|
2023-09-19 21:05:39 +00:00
|
|
|
parties.append(party)
|
|
|
|
p_codes.append(p.code)
|
|
|
|
else:
|
|
|
|
party = next(x for x in parties if x.code == p.code)
|
|
|
|
party.voters += p.voters
|
|
|
|
party.votes += p.votes
|
|
|
|
parties.sort(key=lambda x: x.percent(), reverse=True)
|
|
|
|
|
|
|
|
# Create the table
|
2023-09-24 20:33:25 +00:00
|
|
|
t = '{:^8}|{:^8}|{:^10}\n'.format('Parti', 'Prosent', 'Stemmer')
|
2023-09-19 21:05:39 +00:00
|
|
|
t += ("-"*len(t)+"\n")
|
|
|
|
for n, p in enumerate(parties):
|
2023-09-24 20:33:25 +00:00
|
|
|
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')
|
2023-09-19 21:05:39 +00:00
|
|
|
if n == num:
|
|
|
|
break;
|
|
|
|
return(t)
|
|
|
|
|
2023-09-21 14:46:23 +00:00
|
|
|
# Task 2
|
2024-09-20 19:08:35 +00:00
|
|
|
|
|
|
|
def print_encoding_info(char):
|
|
|
|
char_int = int.from_bytes(bytes(char, 'utf-8'))
|
|
|
|
print(f"Character: '{char}'")
|
|
|
|
if char_int < 129:
|
|
|
|
print(f"- ASCII representation: {format(char_int, 'b')}")
|
|
|
|
else:
|
|
|
|
print("- Not in ASCII range")
|
|
|
|
print(f"- UTF-8: {' '.join(format(x, 'b') for x in bytearray(char, 'utf-8'))}", end='')
|
|
|
|
print(f' ({len(bytearray(char, "utf-8"))} bytes)')
|
|
|
|
print('\n')
|
|
|
|
|
|
|
|
def print_encoding_info_list(char_list):
|
|
|
|
for char in char_list:
|
|
|
|
print_encoding_info(char)
|
2023-09-21 14:46:23 +00:00
|
|
|
|
2023-09-19 21:05:39 +00:00
|
|
|
def main():
|
|
|
|
print(table_from_votes('2021-09-14_party distribution_1_st_2021.csv'))
|
2023-09-24 20:33:25 +00:00
|
|
|
print(table_from_votes('2021-09-14_party distribution_1_st_2021.csv', 3))
|
2023-09-19 21:05:39 +00:00
|
|
|
|
2024-09-20 19:08:35 +00:00
|
|
|
print_encoding_info_list(["2", "$", "å"])
|
2023-09-21 14:46:23 +00:00
|
|
|
|
2023-09-19 21:05:39 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|