Skip to content

Commit

Permalink
Refactors waves further, creating and refactoring helper functions in…
Browse files Browse the repository at this point in the history
… Wave 3.

Makes docstrings more concise and to the point.
Makes additional changes for clarity and standardization, including using comprehensions where appropriate, formatted for readability.
  • Loading branch information
svankoch committed Sep 27, 2024
1 parent 12a43db commit b9ff167
Showing 1 changed file with 55 additions and 186 deletions.
241 changes: 55 additions & 186 deletions viewing_party/party.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,6 @@ def create_movie(title, genre, rating):
'''
Creates a dictionary representing a movie with the given title, genre,
and rating.
Parameters:
title (str): Movie title
genre (str): Movie genre
rating (float): Movie rating
Returns:
user_data (dict): A dictionary with the movie's title, genre, and
rating, or None if any of the inputs are falsy.
'''
if not (title and genre and rating):
return None
Expand All @@ -20,54 +11,25 @@ def create_movie(title, genre, rating):
def add_to_watched(user_data, movie):
'''
Adds a movie to the user's watched list.
Parameters:
user_data (dict): A dictionary containing the user's data,
including a "watched" key.
movie (dict): A dictionary representing the movie to be added to
the user's "watched" list.
Returns:
user_data (dict): The updated user_data with the movie added to
the "watched" list.
'''
user_data["watched"].append(movie)
user_data['watched'].append(movie)
return user_data

def add_to_watchlist(user_data, movie):
'''
Adds a movie to the user's watchlist.
Parameters:
user_data (dict): A dictionary containing the user's data,
including a "watchlist" key.
movie (dict): A dictionary representing the movie to be added to
the user's watchlist.
Returns:
user_data (dict): The updated user_data with the movie added to
the "watchlist" list.
'''
user_data["watchlist"].append(movie)
user_data['watchlist'].append(movie)
return user_data

def watch_movie(user_data, title):
'''
Moves a movie from the user's watchlist to the watched list if the
movie title is found.
Parameters:
user_data (dict): A dictionary containing the user's data,
including "watchlist" and "watched" keys.
title (str): The title of the movie to move from the watchlist
to the watched list.
Returns:
user_data (dict): The updated user_data with the movie added to
the "watchlist" list.
'''

for movie in user_data['watchlist']:
if title == movie["title"]:
if title == movie['title']:
user_data['watched'].append(movie)
user_data['watchlist'].remove(movie)
break
Expand All @@ -78,52 +40,37 @@ def watch_movie(user_data, title):
# -----------------------------------------
def get_watched_avg_rating(user_data):
'''
Calculates the average rating of all movies watched by the user.
Parameter:
user_data (dict): A dictionary with a "watched" list of movie
dictionaries. Each movie dictionary contains information on the
genre, rating, and title of the movie.
Returns:
(float): The average rating of all movies in the watched list,
0.0 if the list is empty.
Calculates the average rating of all movies watched by the user. If
no movies are watched, returns 0.0.
'''
total_ratings = 0.0

if not user_data['watched']:
return 0.0
return total_ratings

total_ratings = 0.0
for movie in user_data['watched']:
total_ratings += movie['rating']

return total_ratings / len(user_data['watched'])

def get_most_watched_genre(user_data):
'''
Determines the genre most frequently watched by the user.
Parameter:
user_data (dict): A dictionary with a "watched" list of movie
dictionaries. Each movie dictionary contains information on the
genre, rating, and title of the movie.
Returns:
most_watched_genre (string): The genre most frequently watched by
the user, None if the list is empty.
Determines the genre most frequently watched by the user. Returns
None if no movies are watched.
'''
if not user_data['watched']:
return None

count = {}
most_watched_genre = None
max_count = 0

genre_count = {}
for movie in user_data['watched']:
genre = movie['genre']
count[genre] = count.get(genre, 0) + 1
genre_count[genre] = genre_count.get(genre, 0) + 1

if count[genre] > max_count:
max_count = count[genre]
most_watched_genre = None
max_count = 0
for genre, count in genre_count.items():
if count > max_count:
max_count = count
most_watched_genre = genre

return most_watched_genre
Expand All @@ -135,110 +82,56 @@ def get_unique_watched(user_data):
'''
Retrieves a list of movies that the user has watched, but their
friends have not.
Parameters:
user_data (dict): A dictionary containing the user's watched
movies and their friends' watched movies. The key "watched"
corresponds to a list of movies for the user, and the key
"friends" maps to a list of dictionaries representing individual
friends, each with "watched" keys
Returns:
unique_watched (list): A list of dictionaries representing movies
that have been watched by the user but not their friends.
'''
user_watched_list = list(user_data["watched"])

friends_watched_list = []
for friend in user_data["friends"]:
friends_watched_list += friend["watched"]

unique_watched = []
for movie in user_watched_list:
if movie not in friends_watched_list:
unique_watched.append(movie)

return unique_watched
friends_watched_list = get_friends_watched_list(user_data)
return get_unique_list(user_data['watched'], friends_watched_list)

def get_friends_unique_watched(user_data):
'''
Retrieves a list of movies that the user's friends have watched, but
the user has not.
Parameters:
user_data (dict): A dictionary containing the user's watched
movies and their friends' watched movies. The key "watched"
corresponds to a list of movies for the user, and the key
"friends" maps to a list of dictionaries representing individual
friends, each with "watched" keys
Returns:
unique_watched (list): A list of dictionaries representing movies
that have been watched by the user's friends but not them.
'''
user_watched_list = list(user_data["watched"])

friends_watched_list = []
for friend in user_data["friends"]:
friends_watched_list += friend["watched"]
friends_watched_list = get_friends_watched_list(user_data)
unique_watched_list = get_unique_list(friends_watched_list, user_data['watched'])

unique_movies_list = []
movies_seen = set()

unique_watched = []
for movie in friends_watched_list:
if movie not in user_watched_list:
unique_watched.append(movie)
for movie in unique_watched_list:
movie_tuple = tuple(movie.items())
if movie_tuple not in movies_seen:
movies_seen.add(movie_tuple)
unique_movies_list.append(movie)

unique_watched = remove_duplicates(unique_watched)
return unique_movies_list

return unique_watched

def remove_duplicates(list_of_dicts):
def get_friends_watched_list(user_data):
'''
Removes duplicate dictionaries from a list based on their contents.
Parameters:
list_of_dicts (list): A list of dictionaries from which
duplicates should be removed.
Returns:
unique_list (list): A list of unique dictionaries. Preserves
the order of occurrence.
Retrieves a list of all movies watched by the user's friends.
'''
unique_list = []
seen = set()
return ([movie for friend in user_data['friends']
for movie in friend['watched']])

for d in list_of_dicts:
dict_tuple = tuple(d.items())

if dict_tuple not in seen:
seen.add(dict_tuple)
unique_list.append(d)
def get_unique_list(source_list, exclusion_list):
'''
Returns a list of items found in the source list, excluding those
from the exclusion list.
'''
return [item for item in source_list if item not in exclusion_list]

return unique_list
# -----------------------------------------
# ------------- WAVE 4 --------------------
# -----------------------------------------
def get_available_recs(user_data):
'''
Retrieves a list of recommended movies that friends have watched and
are available on the streaming services the user has access to.
Parameters:
user_data (dict): A dictionary containing the user's data, which
includes a "subscriptions" key. This represents the names of the
streaming services that the user has access to. Also has a
"friends" key, containing the user's friends' watched movies.
Returns:
'''
friends_unique_watched = get_friends_unique_watched(user_data)
available_recs = []

for movie in friends_unique_watched:
if movie["host"] in user_data["subscriptions"]:
available_recs.append(movie)

return available_recs
return ([movie for movie in friends_unique_watched
if movie['host'] in user_data['subscriptions']])

# -----------------------------------------
# ------------- WAVE 5 --------------------
Expand All @@ -249,46 +142,23 @@ def get_new_rec_by_genre(user_data):
this list will match the user's most frequent genre, will have been
watched by at least one of the user's friends, and will not have been
watched yet by the user.
Parameters:
user_data (dict): A dictionary with a "watched" list of movie
dictionaries. Each movie dictionary contains information on the
genre, rating, and title of the movie.
Returns:
recs_by_genre (list): A list of dictionaries that represents a
list of movies recommended for the user.
'''
if (not user_data['watched'] or
not any(friend['watched']
for friend in user_data['friends'])):
return[]
if (not user_data['watched'] or not any(friend['watched']
for friend in user_data['friends'])):
return []

recs_by_genre = []
available_recs = get_available_recs(user_data)
most_watched_genre = get_most_watched_genre(user_data)

for rec in available_recs:
if (rec['genre'] == most_watched_genre and
not any(movie['title'] == rec['title']
for movie in user_data['watched'])):
recs_by_genre.append(rec)

return recs_by_genre
return ([rec for rec in available_recs
if rec['genre'] == most_watched_genre
and not any(movie['title'] == rec['title']
for movie in user_data['watched'])])

def get_rec_from_favorites(user_data):
'''
Compiles a list of recommended movies from the user's favorites that
no friends have watched.
Parameters:
user_data (dict): A dictionary with a "favorites" list of movie
dictionaries and a "friends" list.
Returns:
rec_from_favorites (list): A list of dictionaries representing
the user's favorite movies that have not been watched by any of
their friends.
'''

if not user_data['favorites']:
Expand All @@ -297,15 +167,14 @@ def get_rec_from_favorites(user_data):
if not user_data['friends']:
return user_data['favorites']

friends_watched_titles = {
movie['title']
for friend in user_data['friends']
friends_watched_titles = ({
movie['title'] for friend in user_data['friends']
for movie in friend['watched']
}
})

rec_from_favorites = [
rec_from_favorites = ([
favorite for favorite in user_data['favorites']
if favorite['title'] not in friends_watched_titles
]
])

return rec_from_favorites

0 comments on commit b9ff167

Please sign in to comment.