Last active
April 22, 2020 16:31
-
-
Save Ze1598/723d111f90b3e6e993d814e0645ca3b0 to your computer and use it in GitHub Desktop.
Spotify - count artist frequencies with map reduce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from functools import reduce | |
| # Refactor of this function -> https://gist.github.com/Ze1598/cd32d9867dac11a029937539bac91721#file-get_spotify_playlist_data-py-L58-L73 | |
| def get_artist_counts(artists_info: List[List[Dict]]) -> Dict[str, int]: | |
| """ | |
| Find the frequency of each artist featured in the playlist and return | |
| a dictionary of the type Artist:Frequency. | |
| """ | |
| def map_fn(track_artists: List[Dict]) -> List[str]: | |
| """ | |
| Helper function for `map`. | |
| Goes through the list of artists for one song and returns a list of | |
| strings, i.e., a list with the names of the featured artists. | |
| """ | |
| return [artist["name"] for artist in track_artists] | |
| def inner_reduce_fn(all_artists: List[str], song_artists: List[str]) -> List[str]: | |
| """ | |
| Helper function for the inner `reduce`. | |
| Take the individual lists of artists featured in each song and reduce | |
| them to a single list (i.e., the result is a single list with every | |
| instance of the artist being featured in a song). | |
| """ | |
| return all_artists + song_artists | |
| def outer_reduce_fn(result: Dict[str, int], name: str) -> Dict[str, int]: | |
| """ | |
| Helper function for the outer `reduce`. | |
| Takes in the current dictionary of artist frequencies and updates it | |
| given the next artist name. | |
| """ | |
| if name in result: | |
| result[name] += 1 | |
| return result | |
| else: | |
| result[name] = 1 | |
| return result | |
| # `map` gets a list where each element is a list with all the featured\ | |
| # artists for one song | |
| # the inner `reduce` makes a single list of all artist instances | |
| # the outer `reduce` counts the artist frequency | |
| artist_counts = reduce( | |
| outer_reduce_fn, | |
| reduce( | |
| inner_reduce_fn, | |
| map(map_fn, artists_info), | |
| [] | |
| ), | |
| {} | |
| ) | |
| return artist_counts |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment