Submitting
If you’re in class
Please follow the directions on the projector screen.
If you’re not in attendance
Submit a screen shot of the above exercises working in your GUI window RATHER than the Interpreter window (like in the demo vids for each project).
Intro
In this week’s tutorial, we’ll be working on Project 2. This includes:
- Setting our computers up to work on P2.
- Practicing using some of the modules that have been provided for you inside of the apis directory.
NOTE: While you may complete the Project submissions as a team, this tutorial should be completed solo (i.e. everyone needs to setup their computer so it’s capable of working on the project).
Inside of project 2, we have multiple libraries available which makes importing and using these libraries a little funky. We import things in a familiar way…
from apis import audio
But because there are multiple libraries available in our apis
folder (e.g. gui
, yelp
, twilio
, and audio
), when we call the functions in these apis we still have to specify which library they come from when we call them. So for example if I want to use the get_genres
function of the audio
api, then we have to call it by saying audio.get_genres...
. If I want to use print
from the gui
library, then I have to call it by saying gui.print(...
, etc.
For all of the API functions in P2, you must use the syntax library_name.function_name(...)
.
Please complete the following steps:
Activity 0. Getting Setup
Complete the “Setup” section of Project 2.
When you’re done, complete one of the two options (either / or) listed below:
Activity 1a (Yelp)
Create a brand new file called tutorial7.py
directly inside of your project2
folder (the location matters). Your directory structure should look like this:
├── apis
├── restaurant_finder.py
├── tests
└── tutorial7.py
Next, paste the following code into your tutorial7.py
file and run it.
from apis import yelp
businesses = yelp.get_businesses("Evanston, IL")
print(businesses)
You should see some Evanston restaurants print to the interpreter window (as a list of dictionaries).
Practice Outputting Dictionary Values
- Output just the
name
of each business to the interpreter window. - Output the
name
,rating
, andreview_count
to the interpreter window.
Using Helper Functions
There is also a helper function inside of the apis.yelp module that can help you output businesses to the interpreter window, which you are welcome to modify:
table_text = yelp.generate_businesses_table(businesses)
print(table_text)
Practice Querying
When you’re done outputting the data up the Yelp documentation and navigate down to the get_businesses
function. Note all of the keyword (optional) parameters that the function accepts.
Next, go back to your tutorial7.py
file and modify the get_businesses(…) function call by:
- Use the various keyword arguments (
price
,category
, and/orlocation
) to change which results get displayed. - Use the
sort_by
keyword argument to change the sort order.
Activity 1b (Audio)
Create a brand new file called tutorial7.py
directly inside of your project2
folder (the location matters). Your directory structure should look like this:
├── apis
├── mixtape_maker.py
├── tests
└── tutorial7.py
Next, paste the following code into your tutorial7.py
file and run it.
from apis import audio
artists = audio.search_for_artists('Beyonce')
print(artists)
You should see some search results relating to Beyonce print to the interpreter window (as a list of dictionaries).
Practice Outputting Dictionary Values
- Output just the
name
of each artist to the interpreter window - Output the
name
,genres
, andshare_url
to the interpreter window
Practice Querying
When you’re done outputting the data, open the Spotify documentation and navigate down to the generate_mixtape
function. Note all of the keyword (optional) parameters that the function accepts.
While each parameter is technically optional, this function needs some seed data in order to give you song recommendations. Go back to your tutorial7.py
file and try invoking the audio.generate_mixtape
function as follows:
track_recommendations = audio.generate_mixtape(artist_ids=['6vWDO969PvNqNYHIOW5v0m'])
print(track_recommendations)
- Try passing in different Artist IDs
- Try passing in a list of genres (see the
audio.get_genres
function to get some valid genre categories).
There is also a helper function inside of the apis.audio
module that can help you output the tracks to the interpreter window:
table_text = audio.generate_tracks_table(track_recommendations)
print(table_text)
If you still have time, please experiment with some of the other built-in functions:
search_for_tracks
search_for_playlists
get_top_tracks_by_artist
Activity 2. Using the GUI Library
For the real project, we don’t want to print stuff to the interpreter window. Instead, we want to use the GUI library to help us make a real app! Once you get comfortable with the above, open up either restaurant_finder.py
or mixtape_generator.py
and take a look at the template.
Adapt some of your work from the above exercises to fit the template – moving your code into one of the “button” functions and replacing calls to the print
function with calls to gui.print
(which works exactly like print…but shows it on the app’s GUI rather than in the interpreter window).
Next, try out the gui.input
function (which works exactly like input…but uses a popup window to ask the user to type something in).
Finally, try out the gui.popup
function which shows a popup window with a message in-case you want to give the user a warning or message outside of the window.
Note: if you’re completing this remotely, you can just submit a screenshot of the above exercises working in your GUI window RATHER than the Interpreter window (like in the demo vids for each project).
Other Practice
Some other things that might be useful during project 2!
# Printing all of the different things in a list one on each line:
counter = 0
my_list = ["a", "b", "c", "d"]
for item in my_list:
print(counter, item)
counter = counter + 1
# Printing all of the different key value pairs in a dictionary one by one:
my_dict = {"a": 1, "b":2, "c": 3}
for key in my_dict:
print(key, my_dict[key])
# Splitting a string of numbers separated by commas into a list of strings
test = "1,2,3,4,5"
my_list = test.split(",")
# Making a list of numbers or things into a string separated by commas.
my_list = [1, 2, 3, 4, 5]
my_string = ",".join(my_list)
# Converting strings to an integer
a_string = "1"
try:
an_int = int(a_string)
except:
print("PANICCCCCC!")
# Saving something someone entered into a dictionary
test = {"keyboard_entry": ""}
some_input = input("Please type in something.")
test['keyboard_entry'] = some_input
# Short circuiting a function...
def example_function(a_number):
if a_number == 2:
return None
print("I'll only print if the number isn't 2")
return "hello"