Skip to content

Instantly share code, notes, and snippets.

@xDHILEx
Created October 26, 2015 19:09
Show Gist options
  • Save xDHILEx/dbdc419b5ee27d9b3d45 to your computer and use it in GitHub Desktop.
Save xDHILEx/dbdc419b5ee27d9b3d45 to your computer and use it in GitHub Desktop.
Building a JSON object of Band and Album information.
import json
class Artist(object):
def __init__(self, artistname):
self.artistname = artistname
self.albums = []
class Album(object):
def __init__(self, name):
self.name = name
self.tracks = []
class Band(Artist, Album):
def __init__(self, artistname):
super(Band, self).__init__(artistname)
def addAlbum(self, album):
self.albums.append(Album(album))
def addTrack(self, album, number, trackname, length):
Track = (number, trackname, length)
self.albums[album].tracks.append(Track)
self.albums[album].length = self.secondsToHour(album)
def lengthInSeconds(self, album):
total = 0
for track in self.albums[album].tracks:
minute, seconds = track[2].split(":")
total += int(minute) * 60 + int(seconds)
return(total)
def secondsToHour(self, album):
total_in_seconds = self.lengthInSeconds(album)
total_minutes = total_in_seconds / 60 # minutes
total_seconds = total_in_seconds % 60 # seconds
if total_minutes % 60 != 0:
total_hours = total_minutes / 60
extra_minutes = total_minutes % 60
if extra_minutes < 10:
extra_minutes = str(0) + str(extra_minutes)
return("%s:%s:%s" % (total_hours, extra_minutes, total_seconds))
x = Band("Mogwai")
x.addAlbum("Come On Die Young")
x.addTrack(0, 1, "Punk Rock", "2:08")
x.addTrack(0, 2, "Cody", "6:33")
x.addTrack(0, 3, "Helps Both Ways", "4:53")
x.addTrack(0, 4, "Year 2000 Non-Compliant Cardia", "3:25")
x.addTrack(0, 5, "Kappa", "4:52")
x.addTrack(0, 6, "Waltz For Aidan", "3:44")
x.addTrack(0, 7, "May Nothing But Happiness Come Through Your Door", "8:29")
x.addTrack(0, 8, "Oh! How The Dogs Stack Up", "2:03")
x.addTrack(0, 9, "Ex-Cowboy", "9:09")
x.addTrack(0, 10, "Chocky", "9:23")
x.addTrack(0, 11, "Christmas Steps", "10:39")
x.addTrack(0, 12, "Punk Rock/Puff Daddy/Antichrist", "2:14")
x.addAlbum("Rock Action")
x.addTrack(1, 1, "Sine Wave", "4:55")
x.addTrack(1, 2, "Take Me Somewhere Nice", "6:57")
x.addTrack(1, 3, "O I Sleep", "0:55")
x.addTrack(1, 4, "Dial: Revenge", "3:28")
x.addTrack(1, 5, "You Don't Know Jesus", "8:02")
x.addTrack(1, 6, "Robot Chant", "1:03")
x.addTrack(1, 7, "2 Rights Make 1 Wrong", "9:31")
x.addTrack(1, 8, "Secret Pint", "3:37")
print(json.dumps(x, default=lambda o: vars(o), indent=4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment