You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

207 lines
6.1 KiB

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### ![](https://ga-dash.s3.amazonaws.com/production/assets/logo-9f88ae6c9c3871690e33280fcf557f33.png)\n",
"\n",
"# Unit 4 Lab: Carefully Adding a Bit More Logic\n",
"\n",
"## Overview\n",
"Welcome to the Unit 4 lab!\n",
"\n",
"Our goal is that, at the end of the next lab (you're almost there!), your app makes it possible for users to search for a movie and print out either the Rotten Tomatoes rating or the search results.\n",
"\n",
"Let's get a little closer. We still have hard-coded values, but in the next lab we'll actually get the real ratings. For now, let's incorporate some intermediate variables and error-catching.\n",
"\n",
"Solutions from the previous lab have been provided below _as a starting point_ for this lab."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Media Class\n",
"\n",
"For this exercise, this class will remain untouched."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class Media(object):\n",
" \"\"\"Base class for Movie and Book objects\"\"\"\n",
"\n",
" def __init__(self, publisher, market):\n",
" self.publisher = publisher\n",
" self.market = market\n",
"\n",
" def get_media_info(self):\n",
" \"\"\"Prints media info to stdout\"\"\"\n",
" print(f'The Media object\\'s publisher is {self.publisher} \\\n",
" and the market is {market}.')\n",
" return None"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Movie Class\n",
"\n",
"Augment the functionality of the `Movie` class as follows:\n",
"\n",
"- Modify the `get_movie_title` and `get_movie_rating` methods to catch a KeyError if a key in the input dictionary is not found\n",
"- Upon such a condition, print the following to stdout\n",
"```python\n",
"'Key <input key> was not found in dictionary.'\n",
"```"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import sys\n",
"\n",
"class Movie(Media):\n",
" \"\"\"\n",
" Movie objects contain all information about a particular movie,\n",
" including the title and rating.\n",
" \"\"\"\n",
"\n",
" def __init__(self, movie_data, publisher, market):\n",
" \"\"\"\n",
" Initialize Movie class, note that the init of the superclass,\n",
" Media, is called with the super().__init__() call\n",
" \"\"\"\n",
" # Call the constructor (init) method of Media\n",
" super().__init__(publisher, market)\n",
" # Store the raw data in this object so that we can use the\n",
" # data in the getter functions.\n",
" self.movie_data = movie_data\n",
"\n",
" def get_movie_title(self):\n",
" \"\"\"\n",
" get_movie_title is a getter function that returns the movie title.\n",
" \"\"\"\n",
" # Return the title from the movie data.\n",
" return self.movie_data[\"title\"]\n",
"\n",
" def get_movie_rating(self):\n",
" \"\"\"\n",
" get_movie_rating is a getter function that returns the rating.\n",
" \"\"\"\n",
" # Return the rating from the movie data.\n",
" return self.movie_data[\"rating\"]"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Functions\n",
"\n",
"Functions below will remain untouched for this exercise"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def return_single_movie_object(movie_title, movie_rating, publisher, market):\n",
" \"\"\"\n",
" Take in the movie title and rating, and return the movie object.\n",
" \"\"\"\n",
"\n",
" return Movie({'title': movie_title, 'rating': movie_rating}, publisher, market)\n",
"\n",
"def print_all_ratings(movie_list):\n",
" \"\"\"\n",
" Take in a list of movies, create a movie object for each, and print the rating.\n",
" \"\"\"\n",
"\n",
" for movie in movie_list:\n",
" movie_object = return_single_movie_object(movie, 4, \"Universal Studios\", \"USA\")\n",
" print(\"The movie\", movie_object.get_movie_title(), \"has a rating of\", movie_object.get_movie_rating())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Testing Pt. 1/2\n",
"\n",
"Write code that will:\n",
"\n",
"- Create a new `Movie` object with the following properties:\n",
" - A `movie_data` dictionary with key value pairs (respectively) of:\n",
" - `title` and `The Matrix`\n",
" - `rating` and `10`\n",
" - A `publisher` of `Roadshow Entertainment`\n",
" - A `market` of `Australia`\n",
" \n",
" - Call the `get_movie_title` and `get_movie_rating` methods"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# A:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Testing Pt. 2/2\n",
"\n",
"- Repeat the instantiation of the `Movie` object with an empty dictionary for the `movie_data` argument\n",
" - Call the `get_movie_title` and `get_movie_rating` methods again\n",
" - Does your error handling catch your KeyError event?"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# A:"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 2
}