Assignments > Tutorial 9: Getting Started on Project 2
Due on Wed, 12/01 @ 11:59PM. 3 Points.
Background
In this week’s tutorial, you will be getting a preview of Project 2. This includes:
- Installing some python dependencies using PIP.
- Downloading my_token.py from Canvas and save it in your
apis
folder. This is the course API master token. Your project won’t work without it. - Practicing using some of the modules that have been provided for you inside of the apis directory.
Please complete the following steps:
Step 1: Setup
Complete Steps A, B, C, and D in the “Setup” section of Project 2. Here are some tips for getting everything set up.
If you have set everything up correctly, running the tests/run_verification.py
python file will display the following output:
test_token (test_authentication.TestAuthentication) ... ok
test_get_key (test_authentication.TestAuthentication) ... ok
test__issue_get_request_only_one (test_spotify.TestSpotify) ...
ok
test_execute_business_queries_just_one_simplified (test_yelp.TestYelp) ...
ok
test_can_import_sendgrid (test_sendgrid.TestSendgrid) ... ok
test_can_import_sendgrid_api_module (test_sendgrid.TestSendgrid) ... ok
----------------------------------------------------------------------
Ran 6 tests in 5.935s (time will vary)
OK
Step 2
When you’re done, complete one of the two options (either / or) listed below:
Option 1: Yelp Option
Create a brand new file called tutorial09.py
directly inside of your project02
folder (the location matters). Your directory structure should look like this:
├── apis
├── music_finder.py
├── restaurant_finder.py
├── tests
└── tutorial09.py
Next, paste the following code into your tutorial09.py
file and run it.
from apis import yelp
businesses = yelp.get_businesses()
print(businesses)
You should see some Evanston restaurants print to the screen (as a list of dictionaries).
Practice Outputting Dictionary Values
- Output just the
name
of each business to the screen - Output the
name
,rating
, andreview_count
to the screen
TIP: You can use the string’s build-in format
function to create nice columns:
template = '{name:<30}{rating:>10}{review_count:>20}'
print('-' * 60)
print(template.format(name='NAME', rating='RATING', review_count='REVIEW COUNT'))
print('-' * 60)
print(template.format(name='Giordannos Pizza', rating=3.5, review_count=280))
print(template.format(name='Lou Malnati\'s Pizza', rating=3.7, review_count=190))
print('-' * 60)
The greater than / less than signs refer to whether the output is left or right justified, and the number refers to the column width.
There is also a helper function inside of the apis.yelp module that can help you output businesses to the screen, which you are welcome to modify:
table_text = yelp.get_formatted_business_list_table(businesses)
print(table_text)
Practice Querying
When you’re done outputting the data, open the apis/yelp.py
and navigate down to the get_businesses
function definition. Note all of the keyword (optional) parameters that the function accepts.
Next, go back to your tutorial09.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.
Learning more about the yelp module (apis/yelp.py)
You can also learn more about the yelp module by:
- Looking at the online documentation here: /project02/docs/yelp.html
- Running this command using python:
help(apis.yelp)
Option 2: Spotify Option
Create a brand new file called tutorial09.py
directly inside of your project02
folder (the location matters). Your directory structure should look like this:
├── apis
├── music_finder.py
├── restaurant_finder.py
├── tests
└── tutorial09.py
Next, paste the following code into your tutorial09.py
file and run it.
from apis import spotify
artists = spotify.get_artists('Beyonce')
print(artists)
You should see some search results relating to Beyonce print to the screen (as a list of dictionaries).
Practice Outputting Dictionary Values
- Output just the
name
of each artist to - Output the
name
,genres
, andshare_url
to the screen
TIP: You can use the string’s build-in format
function to create nice columns:
template = '{name:<30}{id:<25}{genres:<35}'
print('-' * 90)
print(template.format(name='NAME', id='ID', genres='GENRES'))
print('-' * 90)
print(template.format(name='Beyoncé', id='6vWDO969PvNqNYHIOW5v0m', genres='dance pop, pop, r&b'))
print(template.format(name='Beyonce as Shine', id='3mjguRNN96bC8zvaTwHoqE', genres=''))
print('-' * 90)
The greater than / less than signs refer to whether the output is left or right justified, and the number refers to the column width. The code above outputs like this:
------------------------------------------------------------------------------------------
NAME ID GENRES
------------------------------------------------------------------------------------------
Beyoncé 6vWDO969PvNqNYHIOW5v0m dance pop, pop, r&b
Beyonce as Shine 3mjguRNN96bC8zvaTwHoqE
------------------------------------------------------------------------------------------
Practice Querying
When you’re done outputting the data, open the apis/spotify.py
file and navigate down to the get_similar_tracks
function definition. 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. Up to 5 seed values may be provided in any combination of seed_artists, seed_tracks and seed_genres. In other words: len(artist_ids) + len(track_ids) + len(genres)
must be between 1 and 5.
Next, go back to your tutorial09.py
file and try invoking the spotify.get_similar_tracks
function as follows:
track_recommendations = spotify.get_similar_tracks(artist_ids=['6vWDO969PvNqNYHIOW5v0m'])
print(track_recommendations)
- Try passing in different Artist IDs
- Try passing in a list of genres (see the
apis.spotify.get_genres_abridged
function to get some valid genre categories).
There is also a helper function inside of the apis.spotify
module that can help you output the tracks to the screen, which you are welcome to modify:
table_text = spotify.get_formatted_tracklist_table(track_recommendations)
print(table_text)
If you still have time, please experiment with some of the other built-in functions:
- get_tracks
- get_top_tracks_by_artist
- get_related_artists
- get_playlists
Learning more about the spotify module (apis/spotify.py)
You can also learn more about the yelp module by:
- Looking at the online documentation here: /project02/docs/spotify.html
- Running this command using python:
help(apis.spotify)
What to Turn In
Please turn in your completed tutorial exercise(s) on Canvas. To do this:
- Zip your entire
project02
folder (with your edited files inside) - Rename your zip file to
tutorial09
- upload your zip file to Canvas.