Untitled
#!/usr/bin/env python3 # -*- coding: utf-8 -*- import os import sys def read_database(filename='movie_dataset.txt'): """ Reading from a text file and creating a database using a dictionary This function reads a text file with movies, each entry on a new line with the following format : "_title_of_the_movie_" _rating_ _duration_in_minutes_ The title of the movie might be several words long. The rating is a float and the duration is an integer representing minutes The function creates a dictionary with keys the movie titles and value a tuple of the respective rating and diration. If two movie entries have the same title, the movie with the longer duration should be kept. If the filename is invalid (the file does not exist) the program returns None params : filename - string - the name of the file to be read, default = "movie_dataset.txt" returns : a dictionary with movie names as keys, and a tuple containing the movie rating and duration of each movie as a value. """ if not os.path.exists(filename): return None movies = {} with open(filename, 'r') as file: for line in file: parts = line.split() title = ' '.join(parts[:-2]).strip('"') rating = float(parts[-2]) duration = int(parts[-1]) if title not in movies or movies[title][1] < duration: movies[title] = (rating, duration) return movies # Example Usage movies_dict = read_database() if movies_dict: print("Database read successfully!") else: print("Invalid filename or file not found.") def movie_title_list(movies: dict): """ This function returns a list of keys in that case titles of movies : The title of the movie might be several words long. The function takes a dictionary of movies as an input params : movies - dict - the dictionary that has its keys returned, default = none returns : a list of keys for a dictionary with movie names as keys """ return list(movies.keys()) def avg_duration(movies: dict, rating: float): """ This function takes a dictionary of movies and a rating as inputs. It calculates average duration for movies that have a rating greater than specified(rating). params: movies - dict - a dictionary containing movie information rating - float - rating, only movies with a rating greater than this will be considered returns: average duration of movies meeting the criteria """ total_duration = 0 count = 0 for movie_info in movies.values(): movie_rating, movie_duration = movie_info if movie_rating > rating and movie_duration > 0: total_duration += movie_duration count += 1 # If no movies meet the criteria, return None if count == 0: return None # Calculate the average duration average = total_duration / count return average def best_rated_movie(movies: dict, min_dur: int, max_dur: int, choose_shortest: bool = False): """ This function takes a dictionary of movies, a minimum duration, and a maximum duration as inputs. Searches for the best-rated movie within the specified duration range. params: movies - dict - dictionary containing movie information min_dur - int - minimum duration for movies. max_dur - int - maximum duration for movies. choose_shortest - bool - prioritize the shortest movie among those with the highest rating, default = false returns: title of the best-rated movie within duration range, if no movie meets the criteria returns None. """ best_movie = None highest_rating = -1 best_duration = None for title, (rating, duration) in movies.items(): if min_dur <= duration <= max_dur: if rating > highest_rating or (rating == highest_rating and ((choose_shortest and duration < best_duration) or (not choose_shortest and (best_duration is None or duration > best_duration)))): highest_rating = rating best_movie = title best_duration = duration return best_movie def main(): """ This function is a main function of the programe displays the menu for choices to be made. params: returns: """ movie_dict = read_database("movie_dataset.txt") if len(sys.argv) > 1: option = sys.argv[1] if option == '1': titles = movie_title_list(movie_dict) for title in titles: print(title) elif option == '2' and len(sys.argv) > 2: rating = float(sys.argv[2]) average = avg_duration(movie_dict, rating) print(f"Average duration for movies with rating over {rating}: {average}") elif option == '3' and len(sys.argv) > 2: movie_name = ' '.join(sys.argv[2:]) movie_info = movie_dict.get(movie_name) if movie_info: print(f"Rating of '{movie_name}': {movie_info[0]}") else: print(f"Movie '{movie_name}' not found") elif option == '4' and len(sys.argv) > 4: min_dur = int(sys.argv[2]) max_dur = int(sys.argv[3]) choose_shortest = sys.argv[4].lower() == 'true' best_movie = best_rated_movie(movie_dict, min_dur, max_dur, choose_shortest) print(f"Best rated movie: {best_movie}") else: print("Invalid input or insufficient arguments.") else: print("No command-line arguments provided.") if __name__ == '__main__': main()
Leave a Comment