Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Scissors - Laurel O #67

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"python.testing.pytestArgs": [
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.nosetestsEnabled": false,
"python.testing.pytestEnabled": true
}
87 changes: 87 additions & 0 deletions viewing_party/main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
def create_movie(title, genre, rating):
movie_dict = {"title": title, "genre": genre, "rating": rating}
if None in movie_dict.values():
return None
else:
return movie_dict

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

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

def watch_movie(user_data, title):
for movie in user_data["watchlist"]:
if movie["title"] == title:
user_data["watchlist"].remove(movie)
user_data["watched"].append(movie)
return user_data

def list_com_fun(user_data_user_data_key, value):
list_com = [v for i in user_data_user_data_key for k,v in i.items() \
if v == i[value]]
Comment on lines +24 to +25
Copy link

@jbieniosek jbieniosek Mar 31, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Variable names can greatly improve code readability.

  2. When using list comprehension, simpler is often more readable: list_com = [i[value] for i in user_data_user_data_key]. For readability I recommend avoiding double loops in list comprehension (sometimes they are unavoidable, but they are much harder to read).

compare this function to (please ignore tab error):

def generate_list_from_key(movie_list, dict_key):
    new_list = [movie[dict_key] for movie in movie_list]
    return new_list

return list_com

def get_watched_avg_rating(user_data):
list_dic = user_data["watched"]
avg_rating = 0.0
if len(list_dic) > 0:
rating_list = [v for i in list_dic for k,v in i.items() if k=="rating"]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rating_list = [movie["rating"] for movie in list_dic]

avg_rating = sum(rating_list) / len(rating_list)
return avg_rating
else:
return avg_rating

def get_most_watched_genre(user_data):
genres = list_com_fun(user_data["watched"], "genre")
if genres == []:
return None
return max(genres)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How would you solve this if you didn't have access to the max function?


def get_unique_watched(user_data):
answer = []
user = list_com_fun(user_data["watched"], "title")
friend1 = list_com_fun(user_data["friends"][0]["watched"] , "title")
friend2 = list_com_fun(user_data["friends"][1]["watched"] ,"title")
Comment on lines +47 to +48

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function will pass the tests because they only have 2 friends, how would this function need to change to be generalized to a friend list of any size?

friend_movies = friend1 + friend2
unique = [x for x in user if x not in friend_movies]
for v in unique:
movie = {"title":v}
movie_copy = movie.copy()
answer.append(movie_copy)
return answer

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The tests for this function use a simplified version of the movie dictionary. However, our original definition of a movie was that a movie has a title, rating and genre. How could you create a list of the original movie dictionaries using the unique list?

Note: movie.copy() is not needed here.


def get_friends_unique_watched(user_data):
answer = []
user = list_com_fun(user_data["watched"],"title")
friend1 = list_com_fun(user_data["friends"][0]["watched"],"title")
friend2 = list_com_fun(user_data["friends"][1]["watched"] ,"title")
friend_movies = friend1 + friend2
unique = [x for x in friend_movies if x not in user]
unique = set(unique)
for v in unique:
movie = {"title":v}
movie_copy = movie.copy()
answer.append(movie_copy)
return answer

def get_available_recs(user_data):
user_sub = user_data["subscriptions"]
friend_sub = [i for i in user_data["friends"][0]["watched"] and \

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function has the same limitation as get_unique_watched and get_friends_unique_watched. How could you account for an unknown number of friends?

user_data["friends"][1]["watched"] for k,v in i.items() if k=="host"]
rec_host = [i for i in friend_sub for v in i.values() if v in user_sub]
return rec_host

def get_new_rec_by_genre(user_data):
fav_genre = get_most_watched_genre(user_data)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great use of a helper function!

rec_movies = [movie for friend in user_data["friends"] for movie \
in friend["watched"] if movie["genre"]== fav_genre]
return rec_movies

def get_rec_from_favorites(user_data):
rec_fav_movies = [movie for movie in user_data["favorites"] if movie not in \
(i for friend in user_data["friends"] for i in friend["watched"])]
return rec_fav_movies