Eu to tentando fazer um projeto em que as musicas e podcasts(filmes e series) entrem em um arquivo .csv segundo cada user.
estou tendo problemas em como escrever o "file". quando faco a class para tal, ela cria um arquivo com o nome de "temp" e nao o que eu gostaria que fosse("playlist", "user")
eu gostaria de alguma ajuda em relacao de como posso dar continuacao a esse projeto. mesmo alguma indicacao de algum tutorial para tal(tenho procurado mas nao encontrei)
MODELS:
class Playlist:
def __init__(self, author):
self._author = author.title()
# I use only one underline because 2 maybe can do mistakes if I work
# with subclass.
self._likes = 0
# With property don't need with the property to change
# all the self.likes to self.__likes on the main
@property
def likes(self):
return self._likes
def give_like(self):
self._likes += 1
# if someone wants to change the name of the author
@property
def author(self):
return self._author
@author.setter
def author(self, author):
self._author = author.title()
# If I repeat at the subclasses codes this def, I can print them with different proprieties
def __str__(self):
return f'{self.author}: Likes:{self.likes}'
class Music(Playlist):
def __init__(self, song, author, rhythm):
super().__init__(author)
self._song = song.title()
self._rhythm = rhythm.title()
def __str__(self):
return f'{self.author} - {self._song} - {self._rhythm}: Likes:{self.likes}'
class Podcast(Playlist):
def __init__(self, episode, author, subject):
super().__init__(author)
self._episode = episode.title()
self.subject = subject.title()
self._likes = 0
def __str__(self):
return f'{self.author} - {self._episode} - {self.subject}: Likes:{self.likes}'
# I called the class "list"
# I saw about __getitem__ and duck-typing, but I didn't understand very well, so I will not use that
# def __getitem__(self, item):
# return self.files[item]
# I saw that if we take all the "list" like a Class, we maybe will be in bugs on the future because this class
# has many codes and maybe crash with another features in the future
class MyPlaylist(list):
def __init__(self, name, files):
# files is a list
super().__init__(files)
self.name = name
# self.files = files
#with duck typing
# def __len__(self)
# def length(self):
# return len(self.files)
READ_FILES:
from models import Music
class ReadMusicFiles(Music):
def __init__(self, playlist, user, song, author, rhythm):
super().__init__(song, author, rhythm)
self.user = user.title()
self.playlist = playlist.title()
def write_list(self):
temp_list = [self.author, self._song, self._rhythm]
with open("temp.csv", 'a') as self.playlist:
self.playlist.write(', '.join(temp_list))
playlist1 = ReadMusicFiles("Rock_playlist", "Daniel", "beautiful day", "u2", "rock")
ReadMusicFiles.write_list(playlist1)
MAIN:
from models import Music, Podcast, MyPlaylist
# 1. username
# 2. playlist name
# 3. sound/podcast
# 4. insert file
# 5. view playlist
music1 = Music("beautiful day", "u2", "rock")
music1.give_like()
music1.give_like()
podcast1 = Podcast('the future of A.I', 'alura', 'technology')
podcast1.give_like()
podcast1.give_like()
podcast1.give_like()
podcast1.give_like()
podcast1.give_like()
# polymorphisms: Music and Podcast are "brothers". They are with the same SuperClass
my_playlist = [music1, podcast1]
my_weekend_list = MyPlaylist('Weekend Playlist', my_playlist)
for file in my_weekend_list:
# details1 = file.song if hasattr(file, "song") else file.episode
# details2 = file.rhythm if hasattr(file, "rhythm") else file.subject
# print(f'{file.author} - {details1} - {details2}: Likes:{file.likes}')
print(file)