Về cơ bản, đây là một ứng dụng Console đơn giản để quản lý thông tin
về video và playlist, cho phép người dùng nhập và lưu thông tin một
cách dễ dàng.
class Video:
def __init__(self, title, link):
self.title = title
self.link = link
class Playlist:
def __init__(self, name, description, rating, videos):
self.name = name
self.description = description
self.rating = rating
self.videos = videos
def read_video():
title = input("Enter title: ")
link = input("Enter link: ")
video = Video(title, link)
return video
def print_video(video):
print("Video title: ", video.title, end="")
print("Video link: ", video.link, end="")
def read_videos():
videos = []
total_video = int(input("Enter how many videos: "))
for i in range(total_video):
print("Enter video ", i+1)
vid = read_video()
videos.append(vid)
return videos
def print_videos(videos):
for i in range(len(videos)):
print_video(videos[i])
def write_video_txt(video, file):
file.write(video.title + "\n")
file.write(video.link + "\n")
def write_videos_txt(videos, file):
total = len(videos)
file.write(str(total) + "\n")
for i in range(total):
write_video_txt(videos[i], file)
def read_video_from_txt(file):
title = file.readline()
link = file.readline()
video = Video(title, link)
return video
def read_videos_from_txt(file):
videos = []
total = file.readline()
for i in range(int(total)):
video = read_video_from_txt(file)
videos.append(video)
return videos
def read_playlist():
playlist_name = input("Enter playlist name: ")
playlist_description = input("Enter playlist description: ")
playlist_rating = input("Enter rating (1-5): ")
playlist_videos = read_videos()
playlist = Playlist(playlist_name, playlist_description, playlist_rating, playlist_videos)
return playlist
def write_playlist_txt(playlist):
with open("data.txt", "w") as file:
file.write(playlist.name + "\n")
file.write(playlist.description + "\n")
file.write(playlist.rating + "\n")
write_videos_txt(playlist.videos, file)
print("Successfully write playlist to txt")
def read_playlist_from_txt():
with open("data.txt", "r") as file:
playlist_name = file.readline()
playlist_description = file.readline()
playlist_rating = file.readline()
playlist_videos = read_videos_from_txt(file)
playlist = Playlist(playlist_name, playlist_description, playlist_rating, playlist_videos)
return playlist
def print_playlist(playlist):
print("-------")
print("Playlist name: " + playlist.name, end="")
print("Playlist description: " + playlist.description, end="")
print("Playlist rating: " + playlist.rating, end="")
print_videos(playlist.videos)
def main():
playlist = read_playlist()
write_playlist_txt(playlist)
playlist = read_playlist_from_txt()
print_playlist(playlist)
main()
Giải thích
Đây là một đoạn mã Python để đọc và viết thông tin về video và playlist vào một tệp văn bản.
Đầu tiên, chúng ta định nghĩa hai lớp Video và Playlist:
class Video:
def __init__(self, title, link):
self.title = title
self.link = link
class Playlist:
def __init__(self, name, description, rating, videos):
self.name = name
self.description = description
self.rating = rating
self.videos = videos
Lớp Video có các thuộc tính title và link, đại diện cho tiêu đề và liên kết của video. Lớp Playlist bao
gồm thuộc tính name, description, rating và một danh sách các video liên quan đến đó.
Sau đó, chúng ta khai báo các hàm để đọc và viết thông tin vào tệp văn bản:
def read_video():
title = input("Enter title: ")
link = input("Enter link: ")
video = Video(title, link)
return video
def print_video(video):
print("Video title: ", video.title, end="")
print("Video link: ", video.link, end="")
def read_videos():
videos = []
total_video = int(input("Enter how many videos: "))
for i in range(total_video):
print("Enter video ", i+1)
vid = read_video()
videos.append(vid)
return videos
def print_videos(videos):
for i in range(len(videos)):
print_video(videos[i])
def write_video_txt(video, file):
file.write(video.title + "\n")
file.write(video.link + "\n")
def write_videos_txt(videos, file):
total = len(videos)
file.write(str(total) + "\n")
for i in range(total):
write_video_txt(videos[i], file)
def read_video_from_txt(file):
title = file.readline()
link = file.readline()
video = Video(title, link)
return video
def read_videos_from_txt(file):
videos = []
total = file.readline()
for i in range(int(total)):
video = read_video_from_txt(file)
videos.append(video)
return videos
def read_playlist():
playlist_name = input("Enter playlist name: ")
playlist_description = input("Enter playlist description: ")
playlist_rating = input("Enter rating (1-5): ")
playlist_videos = read_videos()
playlist = Playlist(playlist_name, playlist_description, playlist_rating, playlist_videos)
return playlist
def write_playlist_txt(playlist):
with open("data.txt", "w") as file:
file.write(playlist.name + "\n")
file.write(playlist.description + "\n")
file.write(playlist.rating + "\n")
write_videos_txt(playlist.videos, file)
print("Successfully write playlist to txt")
def read_playlist_from_txt():
with open("data.txt", "r") as file:
playlist_name = file.readline()
playlist_description = file.readline()
playlist_rating = file.readline()
playlist_videos = read_videos_from_txt(file)
playlist = Playlist(playlist_name, playlist_description, playlist_rating, playlist_videos)
return playlist
Hàm read_video() đọc thông tin về một video từ người dùng, hàm write_video_txt() ghi thông tin về
một video vào tệp văn bản. Tương tự, chúng ta có các hàm đọc và ghi thông tin cho nhiều video và
playlist (read_videos(), write_videos_txt(), read_videos_from_txt()).
Hàm read_playlist() yêu cầu người dùng nhập thông tin về một playlist và sau đó tạo một đối tượng
Playlist tương ứng, trong đó cũng gọi hàm read_videos() để đọc danh sách các video trong playlist.
Hàm write_playlist_txt() ghi thông tin về playlist vào tệp văn bản.
Cuối cùng, khi chạy hàm main(), chúng ta sẽ đọc và viết các thông tin vào tệp văn bản và in ra thông
tin về playlist:
def main():
playlist = read_playlist()
write_playlist_txt(playlist)
playlist = read_playlist_from_txt()
print_playlist(playlist)
main()