CS110: Fall 2021

Intro to Computer Programming with Python

CS110: Fall 2021

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:

  1. Installing some python dependencies using PIP.
  2. 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.
  3. 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

  1. Output just the name of each business to the screen
  2. Output the name, rating, and review_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:

  1. Use the various keyword arguments (price, category, and/or location) to change which results get displayed.
  2. 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:

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

  1. Output just the name of each artist to
  2. Output the name, genres, and share_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)

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:

Learning more about the spotify module (apis/spotify.py)

You can also learn more about the yelp module by:

What to Turn In

Please turn in your completed tutorial exercise(s) on Canvas. To do this:

  1. Zip your entire project02 folder (with your edited files inside)
  2. Rename your zip file to tutorial09
  3. upload your zip file to Canvas.