All the things
This commit is contained in:
46
nporadio2/top2000.py
Normal file
46
nporadio2/top2000.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from csv import DictReader
|
||||
from uuid import UUID
|
||||
|
||||
|
||||
class Top200Song:
|
||||
def __init__(self, songdict) -> None:
|
||||
try:
|
||||
self.id = UUID(songdict["id"])
|
||||
except ValueError:
|
||||
self.id = None
|
||||
self.artist = songdict["artist"]
|
||||
self.title = songdict["title"]
|
||||
self.position = int(songdict["position"])
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"{self.position:4d}: {self.artist} - {self.title}"
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return str(self)
|
||||
|
||||
|
||||
def load_top2000(filename="2021.csv") -> dict[UUID, Top200Song]:
|
||||
|
||||
songs = {}
|
||||
with open(filename) as f:
|
||||
reader = DictReader(f)
|
||||
|
||||
for line in reader:
|
||||
song = Top200Song(line)
|
||||
if song.id:
|
||||
songs[song.id] = song
|
||||
return songs
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
from csv import DictReader
|
||||
|
||||
songs = []
|
||||
with open("2021.csv") as f:
|
||||
reader = DictReader(f)
|
||||
for line in reader:
|
||||
print(line)
|
||||
songs.append(Top200Song(line))
|
||||
|
||||
for song in songs:
|
||||
print(song)
|
||||
Reference in New Issue
Block a user