Skip to content

Instantly share code, notes, and snippets.

@PatWg
Last active November 6, 2023 18:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PatWg/3bd79224c3a9f0d986716920f7375412 to your computer and use it in GitHub Desktop.
Save PatWg/3bd79224c3a9f0d986716920f7375412 to your computer and use it in GitHub Desktop.
ICC 23-24 Série 8
from dataclasses import dataclass
from typing import List
@dataclass
class Song:
title: str
duration: int
def pretty_print(self):
minutes:int = self.duration // 60
seconds: int = self.duration % 60
# On note l'utilisation d'un opérateur dit "ternaire"
# Voici comment il se lit :
# variable = {valeur si condition vrai} if condition else {valeur si condition fausse}
str_min: str = str(minutes) if minutes > 0 else "0" + str(minutes)
str_sec: str = str(seconds) if seconds > 9 else "0" + str(seconds)
print(f"{self.title} ({str_min}:{str_sec})")
@dataclass
class Album:
title: str
artist: str
year_released: int
tracks: List[Song]
def pretty_print(self):
print(f"Titre de l'album: {self.title}")
print(f"Artiste: {self.artist}")
print(f"Année de sortie: {self.year_released}")
for track_number, track in enumerate(self.tracks):
print(f"{track_number+1}. ", end="")
track.pretty_print()
songs: List[Song] = []
with open('songs.csv', 'r',encoding='utf-8') as file:
tracks = file.readlines()
for track in tracks:
title, duration = track.split(",")
songs.append(Song(title=title, duration=int(duration)))
for song in songs:
song.pretty_print()
wish_you_were_here: Album = Album(title="Wish You Were Here", artist="Pink Floyd", year_released=1975, tracks=songs)
wish_you_were_here.pretty_print()
Shine On You Crazy Diamond (Parts I-V) 820
Welcome to the Machine 458
Have a Cigar 308
Wish You Were Here 334
Shine On You Crazy Diamond (Parts VI-IX) 751
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment