proffreda

abstractions.py

Sep 25th, 2016
702
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.26 KB | None | 0 0
  1. """Data Abstractions"""
  2.  
  3. from utils import mean
  4.  
  5. #############################
  6. # Phase 1: Data Abstraction #
  7. #############################
  8.  
  9.  
  10. # Reviews
  11.  
  12. def make_review(restaurant_name, rating):
  13. """Return a review data abstraction."""
  14. return [restaurant_name, rating]
  15.  
  16. def review_restaurant_name(review):
  17. """Return the restaurant name of the review, which is a string."""
  18. return review[0]
  19.  
  20. def review_rating(review):
  21. """Return the number of stars given by the review, which is a
  22. floating point number between 1 and 5."""
  23. return review[1]
  24.  
  25.  
  26. # Users
  27.  
  28. def make_user(name, reviews):
  29. """Return a user data abstraction."""
  30. return [name, {review_restaurant_name(r): r for r in reviews}]
  31.  
  32. def user_name(user):
  33. """Return the name of the user, which is a string."""
  34. return user[0]
  35.  
  36. def user_reviews(user):
  37. """Return a dictionary from restaurant names to reviews by the user."""
  38. return user[1]
  39.  
  40.  
  41. ### === +++ USER ABSTRACTION BARRIER +++ === ###
  42.  
  43. def user_reviewed_restaurants(user, restaurants):
  44. """Return the subset of restaurants reviewed by user.
  45.  
  46. Arguments:
  47. user -- a user
  48. restaurants -- a list of restaurant data abstractions
  49. """
  50. names = list(user_reviews(user))
  51. return [r for r in restaurants if restaurant_name(r) in names]
  52.  
  53. def user_rating(user, restaurant_name):
  54. """Return the rating given for restaurant_name by user."""
  55. reviewed_by_user = user_reviews(user)
  56. user_review = reviewed_by_user[restaurant_name]
  57. return review_rating(user_review)
  58.  
  59.  
  60. # Restaurants
  61.  
  62. def make_restaurant(name, location, categories, price, reviews):
  63. """Return a restaurant data abstraction."""
  64. # You may change this starter implementation however you wish, including
  65. # adding more items to the dictionary below.
  66. # BEGIN Question 1
  67. "*** REPLACE THIS LINE ***"
  68. # END Question 1
  69. return {
  70. 'name': name,
  71. 'location': location,
  72. 'categories': categories,
  73. 'price': price,
  74. }
  75.  
  76. def restaurant_name(restaurant):
  77. """Return the name of the restaurant, which is a string."""
  78. return restaurant['name']
  79.  
  80. def restaurant_location(restaurant):
  81. """Return the location of the restaurant, which is a list containing
  82. latitude and longitude."""
  83. return restaurant['location']
  84.  
  85. def restaurant_categories(restaurant):
  86. """Return the categories of the restaurant, which is a list of strings."""
  87. return restaurant['categories']
  88.  
  89. def restaurant_price(restaurant):
  90. """Return the price of the restaurant, which is a number."""
  91. return restaurant['price']
  92.  
  93. def restaurant_ratings(restaurant):
  94. """Return a list of ratings, which are numbers from 1 to 5, of the
  95. restaurant based on the reviews of the restaurant."""
  96. # BEGIN Question 1
  97. "*** REPLACE THIS LINE ***"
  98. # END Question 1
  99.  
  100.  
  101. ### === +++ RESTAURANT ABSTRACTION BARRIER +++ === ###
  102.  
  103. def restaurant_num_ratings(restaurant):
  104. """Return the number of ratings for restaurant."""
  105. # BEGIN Question 2
  106. "*** REPLACE THIS LINE ***"
  107. # END Question 2
  108.  
  109. def restaurant_mean_rating(restaurant):
  110. """Return the average rating for restaurant."""
  111. # BEGIN Question 2
  112. "*** REPLACE THIS LINE ***"
  113. # END Question 2
Advertisement
Add Comment
Please, Sign In to add comment