Skip to content

Commit

Permalink
Refactor based on PR feedback from Ansel
Browse files Browse the repository at this point in the history
  • Loading branch information
Msambere committed Oct 2, 2024
1 parent a73723c commit f9b007a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 47 deletions.
10 changes: 9 additions & 1 deletion tests/test_wave_01.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,11 @@ def test_moves_movie_from_watchlist_to_empty_watched():
assert len(updated_data["watchlist"]) == 0
assert len(updated_data["watched"]) == 1
assert updated_data["watched"][-1]["title"] == MOVIE_TITLE_1
assert updated_data["watched"][-1]["genre"] == GENRE_1
assert updated_data["watched"][-1]["rating"] == RATING_1





# @pytest.mark.skip()
Expand All @@ -179,8 +184,11 @@ def test_moves_movie_from_watchlist_to_watched():

# Assert
assert len(updated_data["watchlist"]) == 1
assert FANTASY_1 in updated_data["watchlist"]
assert len(updated_data["watched"]) == 2
assert updated_data["watched"][-1]["title"] == movie_to_watch["title"]
assert FANTASY_2 in updated_data["watched"]
# assert updated_data["watched"][-1]["title"] == movie_to_watch["title"]
assert movie_to_watch in updated_data["watched"]


# @pytest.mark.skip()
Expand Down
1 change: 0 additions & 1 deletion tests/test_wave_03.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ def test_friends_unique_movies_not_duplicated():

# Assert
assert len(friends_unique_movies) == 3
#assert desired movies on in list
assert FANTASY_4 in friends_unique_movies
assert HORROR_1 in friends_unique_movies
assert INTRIGUE_3 in friends_unique_movies
Expand Down
85 changes: 40 additions & 45 deletions viewing_party/party.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,32 @@
# ------------- WAVE 1 -------------------- #

TITLE = "title"
GENRE = "genre"
RATING = "rating"
WATCHED = "watched"
WATCHLIST = "watchlist"
def create_movie(title, genre, rating):
if not title or not genre or not rating:
return None

new_movie = {}

new_movie["title"] = title
new_movie["genre"] = genre
new_movie["rating"] = rating

return new_movie
return {
TITLE : title,
GENRE :genre,
RATING : rating
}

def add_to_watched(user_data, movie):
user_data["watched"].append(movie)
user_data[WATCHED].append(movie)
return user_data

def add_to_watchlist(user_data, movie):
user_data["watchlist"].append(movie)
user_data[WATCHLIST].append(movie)
return user_data

def watch_movie(user_data, title):
for i in range(len(user_data["watchlist"])):
if user_data["watchlist"][i]["title"] == title:
user_data["watched"].append(user_data["watchlist"][i])
user_data["watchlist"].pop(i)
return user_data
for movie in user_data[WATCHLIST]:
if movie[TITLE] == title:
add_to_watched(user_data, movie)
user_data[WATCHLIST].remove(movie)
break
return user_data

# ------------- WAVE 2 -------------------- #
Expand All @@ -43,32 +44,31 @@ def get_watched_avg_rating(user_data):
return avg_rating

def get_most_watched_genre(user_data):
if not user_data["watched"]:
return None
genre_count={}

for movie in user_data["watched"]:
current_genre = movie["genre"]
current_genre_count = genre_count.get(current_genre, 0)
genre_count[current_genre] = current_genre_count + 1
most_popular =[]

popular_genre = None
popular_count = 0

for genre, count in genre_count.items():
if not most_popular or most_popular[1] < count:
most_popular = [genre, count]
return most_popular[0]
if not popular_genre or popular_count < count:
popular_genre = genre
popular_count = count
return popular_genre

# ------------- WAVE 3 -------------------- #

def get_unique_watched(user_data):
unique_movie_list = user_data["watched"].copy()
removal_list = []
for friend in user_data["friends"]:
friend_movie_list = friend["watched"]
for user_movie in unique_movie_list:
if user_movie in friend_movie_list:
removal_list.append(user_movie)
for movie in removal_list:
if movie in unique_movie_list:
unique_movie_list.remove(movie)
friend_movie_list = friend[WATCHED]
for movie in friend_movie_list:
if movie in unique_movie_list:
unique_movie_list.remove(movie)
return unique_movie_list

def get_friends_unique_watched(user_data):
Expand All @@ -84,41 +84,36 @@ def get_friends_unique_watched(user_data):
# ------------- WAVE 4 -------------------- #

def get_available_recs(user_data):
friends_unique = get_friends_unique_watched(user_data)
recommendations = []
for friend in user_data["friends"]:
for movie in friend["watched"]:
if movie["host"] in user_data["subscriptions"] and movie not in user_data["watched"] and movie not in recommendations:
recommendations.append(movie)
for movie in friends_unique:
if movie["host"] in user_data["subscriptions"] and movie not in recommendations:
recommendations.append(movie)
return recommendations

# ------------- WAVE 5 -------------------- #

def get_new_rec_by_genre(user_data):
recommendations = []
if not user_data["watched"] or not user_data["friends"]:
return recommendations


most_watched_genre = get_most_watched_genre(user_data)
friend_unique_watched = get_friends_unique_watched(user_data)

for movie in friend_unique_watched:
if movie["genre"] == most_watched_genre:
recommendations.append(movie)

recommendations = [movie for movie in friend_unique_watched if movie["genre"] == most_watched_genre]

return recommendations

def get_rec_from_favorites(user_data):
recommendations = []
user_unique = get_unique_watched(user_data)
if not user_data["favorites"]:
return recommendations
elif not user_data["friends"]:
recommendations = user_data["favorites"]
return recommendations
else:
for movie in user_data["favorites"]:
for friend in user_data["friends"]:
if movie in friend["watched"] and movie in recommendations:
recommendations.remove(movie)
elif movie not in friend["watched"] and movie not in recommendations:
recommendations.append(movie)
if movie in user_unique:
recommendations.append(movie)
return recommendations

0 comments on commit f9b007a

Please sign in to comment.