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.

202 lines
7.5 KiB

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### ![](https://ga-dash.s3.amazonaws.com/production/assets/logo-9f88ae6c9c3871690e33280fcf557f33.png)\n",
"\n",
"# Unit 3 Lab: Object-Oriented Programming\n",
"\n",
"## Overview\n",
"Welcome to the Unit 3 lab!\n",
"\n",
"We're making a slight jump with this lab to OOP structure, so please use the starter notebook as a starting point.\n",
"\n",
"Let's use object-oriented programming concepts to improve our code. Specifically, we'll be using dictionaries and classes."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Media Class\n",
"\n",
"Create a `Media` class. This will be the parent class of `Movie`, and it itself will inherit from the `object` base python class.\n",
"\n",
"- The `Media` class will take two arguments, `publisher` and `market`, both of which are strings. These strings will set class instance variables, `self.publisher` and `self.market`. Whenever this class is instantiated or called, assume values of `Universal Studios` for `publisher` and `USA` for `market`.\n",
"- The `Media` class will have one class method, `get_media_info()`. This method will print the `self.publisher` and `self.market` class instance variables to stdout and return `None`."
]
},
{
"cell_type": "code",
"execution_count": 1,
"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",
"Create a `Movie` class. This class will <i>inherit</i> from the base `Media` class. We're going to have several movies, each of which will have a title and a rating. We can use a `Movie` class as a scaffold to create many movie objects. Your `Movie` class should have three methods:\n",
" 1. `__init__`, which will take in `self`, `movie_data` (this will be a dictionary containing the title and rating of each movie), `publisher` and `market`. `__init__` will set a member variable, `movie_data`, to equal the `movie_data` dictionary passed in. `publisher` and `market` variables will be passed to the parent `Media` object, which must also have its `__init__()` method called using the `super()` builtin function.\n",
" 1. `get_movie_title()`, a getter function that returns the value of the `title` key in the `movie_data` dictionary.\n",
" 1. `get_movie_rating()`, a getter function that returns the value of the `rating` key in the `movie_data` dictionary."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"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": [
"#### Return Single Movie Object\n",
"\n",
"Now that we have a class, let's make a function that creates `Movie` objects.\n",
" - Create a function called `return_single_movie_object()` that takes four arguments:\n",
" - `movie_title`\n",
" - `movie_rating`\n",
" - `publisher`\n",
" - `market`\n",
" - Have it create and return a `Movie` object with those values."
]
},
{
"cell_type": "code",
"execution_count": 3,
"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)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Print All Ratings\n",
"Let's make a function, `print_all_ratings` that takes a list of movie titles, iterates through them, and prints a result to stdout (more on that below).\n",
"\n",
"- The `print_all_ratings` function will call the `return_single_object` function on each iteration to return a `Movie` object.\n",
"- Use the following hard-coded values of the following parameters when calling `return_single_object`:\n",
"\n",
"<table>\n",
" <tr>\n",
" <td>Variable</td>\n",
" <td>Value</td>\n",
" </tr>\n",
" <tr>\n",
" <td>movie_rating</td>\n",
" <td>4</td>\n",
" </tr>\n",
" <tr>\n",
" <td>publisher</td>\n",
" <td>\"Universal Studios\"</td>\n",
" </tr>\n",
" <tr>\n",
" <td>market</td>\n",
" <td>\"USA\"</td>\n",
" </tr> \n",
"</table>\n",
"\n",
"- After returning a `Movie` object, use the `get_movie_title` and `get_movie_ratings` class methods to retrieve the title and ratings of the Movie, respectively.\n",
"- The printed result to stdout will be of the format: `The movie <movie title> has a rating of <movie rating>`."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
"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())"
]
}
],
"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.1"
}
},
"nbformat": 4,
"nbformat_minor": 2
}