#!/usr/bin/env python # coding: utf-8 # # Programming Practice using The Blue Alliance API # I don't have spell check at the moment so bear with me. # # ## Getting an api key # Make an account on thebluealliance.com. Go to your "account dashboard". Scroll down to "Read API Keys". Type something into the description box like "programming-training" and click "Add new key". Copy the big long string you get and keep this secret. # # ## Setting up your computer # Instructions for installing python will depend on your operating system. Follow the section that matches yours. If you run into any trouble, ask for help. # # ### Windows # Go to python.org, download the executable installer for python3.6, and install it on your computer. *Make sure to check the box that says "install python to your path"*. # # Install the tba package for python: `python.exe -m pip install tbapy` # # ### Mac OS # If you haven't already, install homebrew: https://brew.sh/ # # Use brew to install Python3: `brew install python3` # # Tell MacOS that you installed this cool new program: `brew linkapps python3` # # Use python3 to install the blue alliance package: `python3 -m pip install tbapy` # # ### Debian-derived linux (debian, ubuntu, elementalOS, etc) # Install python3 and idle: `sudo apt-get install python3` # # Use python3 to install the blue allinace package: `python3 -m pip install tbapy` # # ## Test if it works # Run the program called idle # # Type in `import tbapy` # # If you get no output, this is good news! (no news is good news). If you get an error, ask someone for help. # # To start a new project, click File > new file. The window that pops up is a nice editor for python files. To run the files, type F5 or click Run > Run Module. # In[2]: """ Have you seen the tripple-quotes before? This is a multi-line string. It's pretty common to use them for comments. Copy the following lines of code into your text editor, then check that it runs. """ import tbapy api_key = "YOUR API KEY HERE" tba = tbapy.TBA(api_key) # In[24]: # Don't copy this cell get_ipython().run_line_magic('run', 'tba_solutions.py') # In[4]: """ Let's make our first call into the tba module! """ print(tba.status()) # In[5]: """ If you got output that looks kinda like that, then it's probably working. It's a little ugly, though. Here's a cool truck for making it easier to read: """ import pprint pp = pprint.PrettyPrinter() pp.pprint(tba.status()) pp.pprint("Much better!") # In[5]: """ Take a look at the output of tba.status(). What type is it? What type are each of the fields? What type are all the fields of all the fields? Let's try a different function call. Check out the output of the following command: """ pp.pprint(tba.team("frc973")) # In[6]: """ That's a huge dictionary! How many keys are there? On your own machines, try looking for your favorite teams. Don't have a favorite team yet? Take a look at some of my favorites: 1678, 254, 492, 2046, 971. """ pp.pprint(tba.team("frc666")) # In[7]: """ Write a function called `where_is` that takes in a team name (for example "frc360", "frc399") and returns the name of the city that the team is from. Here's some examples of how I want it to work; you can use these examples to test your function. """ print(where_is("frc973")) print(where_is("frc492")) print(where_is("frc1678")) # In[8]: """ Got your function written? Awesome! That was a warm-up. Let's do something more useful. Check out the output of this nifty function here: """ pp.pprint(tba.team_events("frc973", 2017)) # In[9]: """ Geeze that's a huge object! What type is it? What type is each element? What order are they in? Are they in any order? I'm pretty sure they're random but I haven't really checked. Let's cut out the noise and get to what matters. Write a function called `which_events` that takes in 2 parameters: the team name and the year, and returns a list the names of each event that team went to that year. Use the "short_name" field for this. Try it out on your favorite frc teams! Here's some examples below: """ print("973's events:") pp.pprint(which_events("frc973", 2011)) print("1678's events:") pp.pprint(which_events("frc1678", 2012)) print("972's events:") pp.pprint(which_events("frc972", 2016)) "Don't worry about what order these show up in, just that the contents match" # In[10]: """ By the way, if you call tba.team_events(team) and leave out the year, it'll tell you all the events over all the years. This will come in handy soon. """ pp.pprint(tba.team_events("frc973")) # In[11]: """ Still with me? This one's gonna be more difficult. I'll present the problem here, then I'll write a few hints in case you get stuck. Write a function that takes in a team name and returns a count of how many times that team has been at each event. Return a dictionary that has a key for every event the team has ever been to and a value of the number of times the team has been to that event. Call this function attendance_count. Use the "name" field. Some events have changed names slightly over the years, but there's there's not much we can do about that today. Here's an example for 973: """ pp.pprint(attendance_count("frc973")) # In[12]: """ Need help? Here's a hint cell. """ # Pretend I have a list of fruits fruits = ["apples", "bananas", "apples", "oranges"] # How many of each fruit do I have? Let's write a program to count them # At some point we should probably have a loop for fruit in fruits: "but what do we do inside the loop?" """ As we loop through this list of fruits, we need some way to keep track of how many of each fruit we've seen so far. What should we use for this? We want to keep track of some piece of data for every type of fruit. Each type of fruit is a string. Each of these strings has to have its own variable. Let's use a dictionary. """ # create an empty dictionary counts = {} # loop through the fruits for fruit in fruits: counts[fruit] = counts[fruit] + 1 print(counts) # Let's see what happens when I run this? # In[15]: """ What happened? We had a KeyError. What's a KeyError? We tried to access a key in the dictionary that didn't exist. Why didn't it exist? We created an empty dictionary and then tried to access stuff from it. How do we fix this? Let's put the keys in. """ # There's several ways to populate the dictionary. Here's a simple way: counts = {} for fruit in fruits: if fruit not in counts: counts[fruit] = 0 print(counts) # In[17]: """ It worked! Well we aren't done yet, but at least it didn't crash. Now add in the code to count the fruits. Is that working for you? Use this pattern in your attendance_count function. Still stumped? Ask for help! We're probably just sitting on our laptops or something... """ pp.pprint(attendance_count("frc254")) # In[13]: """ Done writing attendance_count? Great work! Check out this awesome new function: """ pp.pprint(tba.team_awards("frc5554")) # In[15]: """ What type is this? What type are all the items? By the way, if you want to limit the awards to a particular year or event, you can pass the year or event code as the second parameter. Let's take a look at 254's 2016 season: """ pp.pprint(tba.team_awards("frc254", 2016)) # In[23]: """ 2 Regional wins and a subdivision finalist. Not bad. Write a function called winning_alliances that takes a team name and returns a list of all the winning alliances they've been on. If you run into trouble, the next few cells will contain hints. Here's some example output: """ pp.pprint(winning_alliances("frc254")) # In[ ]: """ Alright alright this one is kinda hard so let's break it down into smaller chunks. The next few cells contain hints for the problem above. First off, there's tons of awards in these lists! How do we tell which ones are event wins, which ones are chairmens, which ones are the safety award, etc? Look through the list and see if any of the "winning" awards have something in common. Now write a function that takes an award and returns true if it's winning or false if it's not winning. """ def is_winning_award(single_award): "Is the award winning? How will you check?" """ Coolio. That tells if if *one* award is winning, let's write a function to pull out all the winning awards. """ def find_all_winning_awards(list_of_awards): for award in list_of_awards: if is_winning_award(award): return award """ There's a bug in the function above. Can you find it? """ # In[ ]: """ Hint #2. We've got our list of winning awards, how do we get a list of teams out of that? Pretend you've got one award and you already know it's a winning award. Write a function to turn that award into a list of winning teams. """ def get_winning_alliance_from_award(award): "How are you going to build that list of teams?" # In[27]: """ Next problem. Write a function called co_win_count that takes in a team and returns a dictionary that shows how many times each team has been on a winning alliance with the team. Here's an example: """ pp.pprint(winning_alliances("frc973")) pp.pprint(co_win_count("frc973"))