本文整理汇总了Python中playlist.Playlist.save方法的典型用法代码示例。如果您正苦于以下问题:Python Playlist.save方法的具体用法?Python Playlist.save怎么用?Python Playlist.save使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类playlist.Playlist
的用法示例。
在下文中一共展示了Playlist.save方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
# 需要导入模块: from playlist import Playlist [as 别名]
# 或者: from playlist.Playlist import save [as 别名]
def main():
path = ''
while path == '':
path = askdirectory(title='Please choose path to crawl!')
crawler = MusicCrawler(path)
songs = crawler.generate_playlist()
pname = ''
print('\n')
while pname == '':
pname = input('Please insert playlist name: ')
playlist = Playlist(pname)
playlist.add_songs(songs)
print('\n')
# print(playlist.playlist1)
print('\n')
print(playlist.pprint_playlist())
print('\n')
print(playlist.artists())
print('\n')
playlist.save()
playlist.remove_all_songs(playlist.playlist1)
print(playlist.pprint_playlist())
print('\n')
playlist.load(askopenfilename(title='Please choose playlist file to load'))
print('\n')
print(playlist.pprint_playlist())
print('\n')
print(playlist.artists())
print('\n')
示例2: __init__
# 需要导入模块: from playlist import Playlist [as 别名]
# 或者: from playlist.Playlist import save [as 别名]
class MusicPlayer:
def __init__(self):
self.playlist = Playlist("Playlist")
def print_messages(self):
print("Hello, welcome to the music player interface!\n")
print("Load your playlist!")
def get_input(self):
command = input("Enter command> ")
command = command.split(" ")
return command
def interface(self):
command = self.get_input()
while command[0] != "exit":
if command[0] == "load_dir":
folder = MusicCrawler(command[1])
self.playlist = folder.generate_playlist()
command = self.get_input()
elif command[0] == "remove_song":
self.playlist.remove_song(command[1])
print("Songs removed.")
command = self.get_input()
elif command[0] == "total_length":
print(self.playlist.total_length())
command = self.get_input()
elif command[0] == "remove_disrated":
self.playlist.remove_disrated(command[1])
command = self.get_input()
elif command[0] == "remove_song":
self.playlist.remove_song(command[1])
command = self.get_input()
elif command[0] == "remove_bad_quality":
self.playlist.remove_bad_quality()
print("Songs removed.")
command = self.get_input()
elif command[0] == "show_artists":
print(self.playlist.show_artists())
command = self.get_input()
elif command[0] == "remove_song":
self.playlist.remove_song(command[1])
command = self.get_input()
elif command[0] == "save":
self.playlist.save(command[1])
command = self.get_input()
elif command[0] == "load":
self.playlist = self.playlist.load(command[1])
command = self.get_input()
elif command[0] == "print_playlist":
print(self.playlist)
command = self.get_input()
else:
print("Bad input!")
command = self.get_input()
else:
print("Goodbye!")
示例3: TestPlaylist
# 需要导入模块: from playlist import Playlist [as 别名]
# 或者: from playlist.Playlist import save [as 别名]
class TestPlaylist (unittest.TestCase):
def setUp(self):
self.playlist = Playlist("TestPlaylist")
self.song = Song("Thunderstruck", "ACDC", "The Razors Edge", 5, 271.8, 320)
self.song2 = Song("Nothing else matters", "Metallica", "IDK", 0, 271.8, 320)
self.playlist.add_song(self.song)
self.playlist.add_song(self.song2)
"""def test_playlist_init(self):
self.assertEqual(self.playlist.name, "TestPlaylist")
self.assertListEqual(self.playlist.songs,[])"""
def test_add_song(self):
self.playlist.add_song(self.song)
self.assertEqual(self.playlist.songs[0], self.song)
def test_remove_song(self):
self.playlist.add_song(self.song)
song_name = "Thunderstruck"
self.playlist.remove_song(song_name)
self.assertNotIn(song_name, self.playlist.songs)
#Check if removes more than 1 song if we have 3 eqqual songs!
def test_total_length(self):
playlist = Playlist("TestPlaylist")
song1 = Song("Thunderstruck", "ACDC", "The Razors Edge", 5, 271.8, 320)
song2 = Song("Thunderstruck", "ACDC", "The Razors Edge", 5, 200, 320)
playlist.add_song(song1)
playlist.add_song(song2)
self.assertEqual(playlist.total_length(), 471.8)
def test_remove_disrated(self):
song = Song("Thunderstruck", "ACDC", "The Razors Edge", 0, 271.8, 320)
self.playlist.add_song(song)
self.playlist.remove_disrated(2)
self.assertNotIn(song, self.playlist.songs)
def test_show_artists(self):
song1 = Song("Thunderstruck", "ACDC", "The Razors Edge", 0, 271.8, 320)
self.playlist.add_song(song1)
song2 = Song("Nothing else matters", "Metallica", "IDK", 0, 271.8, 320)
self.playlist.add_song(song2)
self.assertEqual(self.playlist.show_artists(), ["ACDC", "Metallica"])
def test_save(self):
playlist = Playlist("TestPlaylist")
song1 = Song("Thunderstruck", "ACDC", "The Razors Edge", 5, 271.8, 320)
song2 = Song("Thunderstruck", "ACDC", "The Razors Edge", 5, 200, 320)
playlist.add_song(song1)
playlist.add_song(song2)
self.playlist.save("json.txt")
示例4: PlaylistTest
# 需要导入模块: from playlist import Playlist [as 别名]
# 或者: from playlist.Playlist import save [as 别名]
class PlaylistTest(unittest.TestCase):
def setUp(self):
self.song1 = Song("Title1", "Artist1", "Album1", 2, 192, 128)
self.song2 = Song("Title2", "Artist2", "Album2", 2, 183, 1024)
self.playlist = Playlist("PlaylistTitle")
self.playlist.songs.append(self.song1)
self.playlist.songs.append(self.song2)
def test_init(self):
self.assertEqual(self.playlist.name, "PlaylistTitle")
self.assertEqual(self.playlist.songs, [self.song1, self.song2])
def test_add_song(self):
self.song3 = Song("Title3", "Artist3", "Album3", 2, 201, 128)
self.playlist.add_song(self.song3)
self.assertEqual(
self.playlist.songs, [self.song1, self.song2, self.song3])
def test_remove_song(self):
self.song3 = Song("Title3", "Artist3", "Album3", 4, 194, 1024)
self.playlist.add_song(self.song3)
self.playlist.remove_song("Title3")
self.assertEqual(self.playlist.songs, [self.song1, self.song2])
def test_total_length(self):
self.assertEqual(self.playlist.total_length(), 375)
def test_remove_disrated(self):
self.song3 = Song("Title3", "Artist3", "Album3", 4, 206, 1024)
self.playlist.add_song(self.song3)
self.playlist.remove_disrated(3)
self.assertEqual(self.playlist.songs, [self.song3])
def test_remove_bad_quality(self):
self.song3 = Song("Title3", "Artist3", "Album3", 4, 206, 1024)
self.playlist.add_song(self.song3)
self.playlist.remove_bad_quality(1000)
self.assertEqual(self.playlist.songs, [self.song2, self.song3])
def test_show_artists(self):
self.song3 = Song("Title3", "Artist2", "Album3", 4, 206, 1024)
self.playlist.add_song(self.song3)
self.assertEqual(self.playlist.show_artists(), ["Artist1", "Artist2"])
# def test_str(self):
# test = self.playlist.str()
# self.assertEqual(
# "Artist1 Title1 - 3:12\nArtist2 Title2 - 3:03\n", test)
def test_save(self):
self.playlist.save("pesho.txt")
def test_load(self):
self.playlist.load("pesho.txt")
示例5: generate_playlist
# 需要导入模块: from playlist import Playlist [as 别名]
# 或者: from playlist.Playlist import save [as 别名]
def generate_playlist(self, playlist_name):
self.collect_songs_names()
new_playlist = Playlist("Songs in {} ".format(self.retrun_folder_name()))
#print(os.getcwd())
#os.chdir(self.path)
#print(os.getcwd())
for song_name in self.folder_files:
file_name, file_path = MusicCrawler.split_path_and_name(song_name)
new_playlist.list_all_songs.append(self.import_songs_from_folder(file_name, file_path))
new_playlist.save(playlist_name)
return new_playlist
示例6: TestPlaylist
# 需要导入模块: from playlist import Playlist [as 别名]
# 或者: from playlist.Playlist import save [as 别名]
class TestPlaylist(unittest.TestCase):
def setUp(self):
self.playlist = Playlist('Pro Playlist')
self.song = Song("BeatBox", "PsychoFrog", "BB 2015", 4, 190, 320)
def tearDown(self):
os.remove('test_playlist.txt')
def test_add_song(self):
self.playlist.add_song(self.song)
self.assertIn(self.song, self.playlist.songs)
def test_remove_song(self):
self.playlist.add_song(self.song)
self.playlist.add_song(self.song)
self.playlist.add_song(self.song)
self.playlist.remove_song(self.song.title)
self.assertEqual(self.playlist.songs, [])
def test_total_length(self):
self.playlist.add_song(self.song)
self.playlist.add_song(self.song)
self.assertEqual(self.song.length + self.song.length, self.playlist.total_length())
def test_remove_disrated(self):
self.playlist.add_song(self.song)
self.playlist.remove_disrated(5)
self.assertEqual([], self.playlist.songs)
def test_remove_disrated_with_invalid_rating(self):
with self.assertRaises(ValueError):
self.playlist.remove_disrated(-1)
def test_remove_bad_quality(self):
self.playlist.add_song(self.song)
bad_bit_song = Song("d", "R", "A", 4, 40, 256)
self.playlist.add_song(bad_bit_song)
self.playlist.remove_bad_quality()
self.assertEqual([self.song], self.playlist.songs)
def test_show_artists(self):
self.playlist.add_song(self.song)
self.playlist.add_song(self.song)
self.playlist.add_song(Song("Other Song", "Other Artist", "Album", 5, 180, 320))
self.assertEqual(["PsychoFrog", "Other Artist"], self.playlist.show_artists())
def test_saving(self):
self.playlist.save('test_playlist.txt')
示例7: main
# 需要导入模块: from playlist import Playlist [as 别名]
# 或者: from playlist.Playlist import save [as 别名]
def main():
#new_songs_arr = []
print(start_message())
print(songs_to_add())
new_playlist = Playlist("my_playlist")
while True:
command = split_command_str(input("Enter command>"))
if the_command(command, "generate"):
new_crawler = Music_crawler(command[1])
new_playlist = new_crawler.generate_playlist()
print(new_playlist.str_func())
elif the_command(command, "remove_disrated"):
new_playlist.remove_disrated(int(command[1]))
print(new_playlist.str_func())
elif the_command(command, "remove_bad_quality"):
new_playlist.remove_bad_quality()
print(new_playlist.str_func())
elif the_command(command, "remove"):
new_playlist.remove_song(command[1])
print(new_playlist.str_func())
elif the_command(command, "add"):
for song in songs_arr:
if command[1] == song.title:
new_playlist.add_song(song)
break
print(new_playlist.str_func())
elif the_command(command, "load"):
new_playlist.load(command[1])
print(new_playlist.str_func())
elif the_command(command, "save"):
new_playlist.save(command[1])
elif the_command(command, "finish"):
print("GoodBye. Thank you for using the player")
break
else:
print(unknown_command())
示例8: TestPlaylist
# 需要导入模块: from playlist import Playlist [as 别名]
# 或者: from playlist.Playlist import save [as 别名]
class TestPlaylist(unittest.TestCase):
def setUp(self):
self.playlist = Playlist("test_playlist")
self.test_song = Song("Highway to Hell", "AC/DC", "Highway to Hell", 5, 208, 128, "sda")
self.test_song2 = Song("Back in Black", "ACDC", "Back in Black", 4, 200, 256, "sdb")
self.playlist.add_song(self.test_song)
self.playlist.add_song(self.test_song2)
self.playlist.save("sdadada.json")
#self.playlist.load("sdadada.json")
def test_init(self):
self.assertEqual("test_playlist", self.playlist.name)
def test_playlist_add_song(self):
self.assertListEqual(self.playlist.songs, [self.test_song, self.test_song2])
self.assertEqual(self.playlist.totalLength, 408)
def test_playlist_remove_song(self):
self.playlist.remove_song(self.test_song.title)
self.assertListEqual(self.playlist.songs, [self.test_song2])
self.assertEqual(self.playlist.totalLength, 200)
def test_playlist_total_length(self):
self.playlist.remove_song(self.test_song.title)
self.assertEqual(self.playlist.total_length(), 200)
def test_playlist_remove_disrated(self):
self.playlist.remove_disrated(5)
self.assertListEqual(self.playlist.songs, [self.test_song])
self.assertEqual(self.playlist.total_length(), 208)
def test_playlist_remove_bad_quality(self):
self.playlist.remove_bad_quality()
self.assertListEqual(self.playlist.songs, [self.test_song2])
self.assertEqual(self.playlist.total_length(), 200)
def test_playlist_list_artists(self):
self.playlist.add_song(self.test_song2)
artists = self.playlist.list_artists()
self.assertListEqual(["AC/DC", "ACDC"], artists)
示例9: PlaylistTest
# 需要导入模块: from playlist import Playlist [as 别名]
# 或者: from playlist.Playlist import save [as 别名]
class PlaylistTest(unittest.TestCase):
def setUp(self):
self.playlist = Playlist('DnB')
self.come_alive = Song(
'Come Alive',
'Netsky',
'Netsky-2',
2,
204,
320
)
self.puppy = Song(
'Puppy',
'Netsky',
'Netsky-2',
4,
280,
96
)
self.horrorcane = Song(
'Horrorcane',
'High Rankin',
'Unknown',
5,
300,
192
)
self.playlist.songs = [self.come_alive, self.puppy]
def test_init(self):
self.assertEqual(self.playlist.name, 'DnB')
self.playlist.songs = []
self.assertEqual(self.playlist.songs, [])
def test_add_song(self):
self.playlist.songs = []
self.playlist.add_song(self.come_alive)
self.assertEqual(self.playlist.songs, [self.come_alive])
def test_remove_song(self):
self.playlist.remove_song('Come Alive')
self.assertEqual(self.playlist.songs, [self.puppy])
def test_total_length(self):
self.assertEqual(self.playlist.total_length(), 484)
def test_remove_disrated(self):
self.playlist.remove_disrated(3)
self.assertEqual(self.playlist.songs, [self.puppy])
def test_remove_bad_quality(self):
self.playlist.remove_bad_quality()
self.assertEqual(self.playlist.songs, [self.come_alive])
def test_show_artists(self):
self.playlist.songs.append(self.horrorcane)
self.assertEqual(self.playlist.show_artists(), ['Netsky', 'High Rankin'])
def test_save_playlist(self):
self.playlist.songs = [self.puppy, self.horrorcane, self.come_alive]
self.playlist.save('DnB')
os.remove('DnB.json')
def test_load_playlist(self):
self.plist = load('haha.json')
self.assertIsInstance(self.plist.songs[0], Song)
self.assertEqual(self.puppy.title, self.plist.songs[0].title)
示例10: PlaylistTests
# 需要导入模块: from playlist import Playlist [as 别名]
# 或者: from playlist.Playlist import save [as 别名]
class PlaylistTests(unittest.TestCase):
def setUp(self):
self.playlist = Playlist("The Named Playlist")
self.song1 = Song("Loosing My Insanity", "DIO", "Magica", 5, 333, 256)
self.song2 = Song("Last In Line", "DIO", "Last In Line", 4, 222, 191)
file = open("test_file", "w")
file.close()
def test_init(self):
self.assertEqual("The Named Playlist", self.playlist.name)
self.assertEqual([], self.playlist.collection)
def test_add_song(self):
self.assertTrue(self.playlist.add_song(self.song1))
def test_add_song_value_error(self):
with self.assertRaises(ValueError):
self.playlist.add_song("tomatoe")
def test_remove_song(self):
self.playlist.add_song(self.song1)
self.playlist.add_song(self.song2)
self.playlist.remove_song("Last In Line")
lst = deepcopy(self.playlist.collection)
lst = list(map(lambda x: x.title, lst))
self.assertEqual(0, lst.count("Last In Line"))
def test_total_length(self):
self.playlist.add_song(self.song1)
self.playlist.add_song(self.song2)
self.assertEqual(555, self.playlist.total_length())
def test_total_length_empty_collection(self):
self.assertEqual(0, self.playlist.total_length())
def test_remove_disrated(self):
self.playlist.add_song(self.song1)
self.playlist.add_song(self.song2)
self.playlist.remove_disrated(5)
lst = deepcopy(self.playlist.collection)
lst = list(map(lambda x: x.rating, lst))
self.assertEqual(0, lst.count(4))
def test_remove_disrated_value_error(self):
with self.assertRaises(ValueError):
self.playlist.remove_disrated(8)
def test_remove_bad_quality(self):
self.playlist.add_song(self.song1)
self.playlist.add_song(self.song2)
self.playlist.remove_bad_quality()
lst = deepcopy(self.playlist.collection)
lst = list(map(lambda x: x.bitrate, lst))
self.assertEqual(0, lst.count(191))
def test_show_artists(self):
self.playlist.add_song(self.song1)
self.playlist.add_song(self.song2)
self.assertEqual(["DIO"], self.playlist.show_artists())
def test_show_artists_empty_collection(self):
self.assertEqual([], self.playlist.show_artists())
def test_str(self):
self.playlist.add_song(self.song1)
self.playlist.add_song(self.song2)
self.assertEqual("DIO Loosing My Insanity - 05:33\n" +
"DIO Last In Line - 03:42\n", self.playlist.str())
# I'm not sure how to test them separately
def test_save_and_load(self):
self.playlist.add_song(self.song1)
self.playlist.add_song(self.song2)
self.playlist.save("test_file")
self.assertEqual(self.playlist.str(), Playlist.load("test_file").str())
def tearDown(self):
remove("test_file")
示例11: TestPlaylist
# 需要导入模块: from playlist import Playlist [as 别名]
# 或者: from playlist.Playlist import save [as 别名]
class TestPlaylist(unittest.TestCase):
def setUp(self):
self.test_playlist = Playlist("Rock'n'roll")
self.test_song = Song(
"Hells Bells",
"ACDC",
"Hells Bells",
5,
309,
256
)
def test_playlist_init(self):
self.assertEqual(self.test_playlist.name, "Rock'n'roll")
def test_add_song(self):
self.test_playlist.add_song(self.test_song)
self.assertIn(self.test_song, self.test_playlist.songs)
def test_remove_song(self):
self.test_playlist.add_song(self.test_song)
self.test_playlist.add_song(self.test_song)
self.test_playlist.remove_song("Hells Bells")
self.assertNotIn(self.test_song, self.test_playlist.songs)
def test_total_length(self):
self.test_playlist.add_song(self.test_song)
self.test_playlist.add_song(self.test_song)
self.assertEqual(self.test_playlist.total_length(), 618)
def test_remove_disrated(self):
self.test_bad_song = Song(
"Highway to hell",
"ACDC",
"Hells Bells",
1,
309,
256
)
self.test_playlist.add_song(self.test_song)
self.test_playlist.add_song(self.test_song)
self.test_playlist.remove_disrated(2)
self.assertNotIn(self.test_bad_song, self.test_playlist.songs)
self.assertIn(self.test_song, self.test_playlist.songs)
def test_removev_bad_quality(self):
self.test_bad_song = Song(
"Highway to hell",
"ACDC",
"Hells Bells",
5,
309,
1
)
self.test_playlist.add_song(self.test_song)
self.test_playlist.add_song(self.test_bad_song)
self.test_playlist.remove_bad_quality()
self.assertNotIn(self.test_bad_song, self.test_playlist.songs)
self.assertIn(self.test_song, self.test_playlist.songs)
def test_show_artists(self):
self.test_new_song = Song(
"November rain",
"Guns'n'Roses",
"Hells Bells",
5,
240,
1
)
self.test_playlist.add_song(self.test_song)
self.test_playlist.add_song(self.test_new_song)
self.assertEqual(self.test_playlist.show_artists(), ["ACDC", "Guns'n'Roses"])
def test_str(self):
self.test_new_song = Song(
"November rain",
"Guns'n'Roses",
"Hells Bells",
5,
240,
1
)
self.test_playlist.add_song(self.test_song)
self.test_playlist.add_song(self.test_new_song)
self.assertEqual(str(self.test_playlist), "ACDC Hells Bells - 05:09'\n'Guns'n'Roses November rain - 04:00")
def test_save(self):
self.test_new_song = Song(
"November rain",
"Guns'n'Roses",
"Hells Bells",
5,
240,
1
)
self.test_playlist.add_song(self.test_song)
self.test_playlist.add_song(self.test_new_song)
self.test_playlist.save("json_playlist.json")
#.........这里部分代码省略.........
示例12: __init__
# 需要导入模块: from playlist import Playlist [as 别名]
# 或者: from playlist.Playlist import save [as 别名]
class MusicPlayer:
def __init__(self):
self.playlist = Playlist("myPlaylist")
self.choice = 0
def list_options(self):
options = ["craw a directory (<path_to_the_files>)",
"create empty playlist(<name_of_the_playlist>)",
"load a playlist(<name_of_the file_to_load_from>)",
"save a playlist(<name_of_the_file_to_save_in>)",
"add song to a playlist(<song>)",
"remove song from the playlist()(<song>)",
"remove disrated songs(<rating>)",
"remove songs with bad quality",
"show artists",
"print",
"list options",
"finish"]
for index, value in enumerate(options):
print (index + 1, value)
print("<song> is <title> <artist> <album> <rating> <length> <bitrate>")
def options(self, args):
if (self.choice == 1):
self.playlist = MusicCrawler(args[0]).generate_playlist()
elif(self.choice == 2):
self.playlist = Playlist(args[0])
elif(self.choice == 3):
self.playlist = Playlist.load(args[0])
elif(self.choice == 4):
self.playlist.save(args[0])
elif(self.choice == 5):
song = Song(args[0], args[1], args[2], int(
args[3]), int(args[4]), int(args[5]))
self.playlist.add_song(song)
elif(self.choice == 6):
song = Song(args[0], args[1], args[2], int(
args[3]), int(args[4]), int(args[5]))
self.playlist.remove_song(song)
elif(self.choice == 7):
self.playlist.remove_disrated(int(args[0]))
elif(self.choice == 8):
self.playlist.remove_bad_quality()
elif(self.choice == 9):
print(self.playlist.show_artist())
elif(self.choice == 10):
print(self.playlist)
elif(self.choice == 11):
self.list_options()
else:
print("Enter a valid command")
def user_decision(self):
self.list_options()
while True:
command = input("Enter command>")
command = command.split(" ")
self.choice = int(command[0])
arguments = command[1:]
if self.choice == 12:
break
else:
self.options(arguments)
示例13: TestPlaylist
# 需要导入模块: from playlist import Playlist [as 别名]
# 或者: from playlist.Playlist import save [as 别名]
class TestPlaylist(unittest.TestCase):
def setUp(self):
self.playlist = Playlist('my_playlist')
self.untitled = Playlist()
self.song_1 = Song(
'Hells Bells',
'AC/DC',
'rough and though',
4,
400,
256)
self.song_2 = Song(
'For Whom the Bell Tolls',
'Metallica',
'For Whom the Bell Tolls',
5,
500,
320)
def test_playlist_init(self):
self.assertEqual(self.playlist.name, 'my_playlist')
self.assertEqual(self.untitled.name, 'Untitled')
def test_add_song(self):
self.playlist.add_song(self.song_1)
self.playlist.add_song(self.song_2)
def test_remove_song(self):
self.playlist.add_song(self.song_1)
self.playlist.add_song(self.song_1)
self.playlist.add_song(self.song_1)
self.playlist.add_song(self.song_2)
self.playlist.remove_song('Hells Bells')
self.assertEqual(len(self.playlist.songs), 1)
def test_total_length(self):
self.playlist.add_song(self.song_1)
self.playlist.add_song(self.song_1)
self.playlist.add_song(self.song_1)
self.playlist.add_song(self.song_1)
self.playlist.add_song(self.song_2)
self.assertEqual(self.playlist.total_length(), 2100)
def test_remove_disrated(self):
self.song_1.set_rating(3)
self.playlist.add_song(self.song_1)
self.playlist.add_song(self.song_1)
self.playlist.add_song(self.song_1)
self.playlist.add_song(self.song_1)
self.playlist.add_song(self.song_2)
self.playlist.remove_disrated(4)
self.assertEqual(len(self.playlist.songs), 1)
def test_remove_bad_quality(self):
self.song_1.bitrate = 92
self.playlist.add_song(self.song_1)
self.playlist.add_song(self.song_1)
self.playlist.add_song(self.song_1)
self.playlist.add_song(self.song_1)
self.playlist.add_song(self.song_2)
self.playlist.remove_bad_quality()
self.assertEqual(len(self.playlist.songs), 1)
def test_show_artists(self):
self.playlist.add_song(self.song_1)
self.playlist.add_song(self.song_1)
self.playlist.add_song(self.song_1)
self.playlist.add_song(self.song_1)
self.playlist.add_song(self.song_2)
# print(self.playlist.show_artists())
def test_to_string(self):
self.playlist.add_song(self.song_1)
self.playlist.add_song(self.song_2)
self.playlist.add_song(self.song_1)
self.playlist.add_song(self.song_2)
self.playlist.add_song(self.song_1)
self.playlist.add_song(self.song_2)
print(self.playlist.to_string())
def test_save(self):
# pass
self.playlist.add_song(self.song_2)
self.playlist.add_song(self.song_1)
self.playlist.add_song(self.song_2)
self.playlist.save('playlist.json')
def test_load(self):
# pass
plst = Playlist.load('playlist.json')
print(plst.to_string)
示例14: TestPlaylist
# 需要导入模块: from playlist import Playlist [as 别名]
# 或者: from playlist.Playlist import save [as 别名]
class TestPlaylist(unittest.TestCase):
def setUp(self):
self.playlist = Playlist()
self.song = Song(
'Clarity',
"Zedd",
'Clarity',
456,
64)
self.playlist.add_song(self.song)
self.song2 = Song(
'Hearthbeat',
"Nneka",
'HB',
65,
256)
self.playlist.add_song(self.song2)
def test_add_song(self):
self.assertTrue(self.song in self.playlist.songs)
def test_remove_song(self):
self.playlist.add_song(self.song) # add it 2nd time
self.playlist.remove_song("Clarity")
self.assertTrue(self.song not in
[x for x in self.playlist.songs])
def test_total_length(self):
self.assertEqual(self.playlist.total_length(), "08:41")
def test_remove_disrated(self):
self.song.rate(2)
self.playlist.remove_disrated(3)
self.assertTrue(self.song not in
[x for x in self.playlist.songs])
def test_remove_bad_quality(self):
self.playlist.remove_bad_quality()
self.assertTrue(self.song not in
[x for x in self.playlist.songs])
def test_show_artists(self):
self.assertEqual({"Nneka", "Zedd"},
self.playlist.show_artists())
def test_string_representation(self):
self.assertEqual(str(self.playlist),
"Zedd Clarity - 07:36\nNneka Hearthbeat - 01:05")
def test_save(self):
self.playlist.save("test_playlist.txt")
file = open("test_playlist.txt")
file_content = file.read()
file.seek(0)
self.assertEqual(file_content,
dumps(self.playlist.songs,
default=lambda o: o.__dict__))
remove("test_playlist.txt")
@unittest.skip("I have no Idea why it does not work")
def test_load(self):
self.playlist.save("test_playlist.txt")
new_playlist = Playlist.load("test_playlist.txt")
self.assertListEqual(str(new_playlist),
str(self.playlist))
remove("test_playlist.txt")
示例15: PlaylistTests
# 需要导入模块: from playlist import Playlist [as 别名]
# 或者: from playlist.Playlist import save [as 别名]
class PlaylistTests(unittest.TestCase):
def setUp(self):
self.playlist = Playlist("Test Playlist")
self.song = Song("The Jack", "ACDC", "T.N.T.", 4, 256, 320)
self.song_2 = Song("The Mack", "ACDC", "B.N.B.", 2, 256, 96)
def test_init(self):
test_playlist = Playlist("ASDF")
self.assertEqual(test_playlist.name, "ASDF")
def test_add_song_success(self):
self.playlist.add_song(self.song)
self.assertEqual(self.playlist.songs[0], self.song)
def test_add_song_type_error(self):
with self.assertRaises(TypeError):
self.playlist.add_song("not a song")
def test_remove_song(self):
self.playlist.add_song(self.song)
self.playlist.add_song(self.song_2)
self.playlist.remove_song(self.song.title)
self.assertEqual(self.playlist.songs, [self.song_2])
def test_total_length(self):
self.playlist.add_song(self.song)
self.playlist.add_song(self.song_2)
self.assertEqual(self.playlist.total_length(), 512)
def test_remove_disrated_success(self):
self.playlist.add_song(self.song)
self.playlist.add_song(self.song_2)
self.playlist.remove_disrated(3)
self.assertEqual(self.playlist.songs, [self.song])
def test_remove_disrated_value_error(self):
with self.assertRaises(ValueError):
self.playlist.remove_disrated(8)
def test_remove_bad_quality(self):
self.playlist.add_song(self.song)
self.playlist.add_song(self.song_2)
self.playlist.remove_bad_quality()
self.assertEqual(self.playlist.songs, [self.song])
def test_show_artists(self):
self.playlist.add_song(self.song)
self.playlist.add_song(self.song_2)
self.assertEqual(self.playlist.show_artists(), ["ACDC"])
def test_load(self):
self.playlist.add_song(self.song)
self.playlist.add_song(self.song_2)
file_name = str(uuid4())
self.playlist.save(file_name)
loaded_playlist = Playlist.load(file_name)
remove(file_name)
self.assertEqual(str(self.playlist), str(loaded_playlist))