Github

FYI: This set of notes goes over what we did in class, but Soma covers some of these topics in greater detail in his ‘Git and Github’ section, found on the sidebar. Check it out – it’s really detailed and thorough.

Go to Github and make an account. Git is a command line tool (there are non-command line/desktop versions of Git… but Soma doesn’t want you to use that!).

  • Git is a version control system (“VCS”).
  • Git and Github are NOT the same. Git is the thing that runs on your computer, github runs magically on the cloud…
    • bitbucket is another service like Github.
    • gitlab, not open-source version of Github.
  • Sample project on Github: Soma’s Tabletop.js.
  • The code on Github is open source or free/open source software (FOSS) – you can download and run the programs you see!
  • People can make their own derivative versions of your code, OR they can fix errors in your code and ask if you want to incorporate the changes into your code. Essentially, lots of people can contribute to make a project better.

Open up Terminal, type git --version to make sure you have git on your computer.

We’re going to setup your Github!

  • git config --global user.name "YOUR NAME"
  • git config --global user.email "YOUR EMAIL ADDRESS"
  • Next Steps:
    • git credential-osxkeychain
    • git config --global credential.helper osxkeychain
  • Go to Github; we’re going to make a repository.
    • Make sure you’re logged in
    • Click the ‘+’ and then click ‘New Repository’
    • Create a new repository (give it a name), make it private.
    • A repository is a home for your code!
  • We’re going to make a directory somewhere on our computer.
    • mkdir [directory_name]: create a folder that will be your sample repository
    • cd [directory_name]
    • echo "# sample-repo" >> README.md
    • git init: this basically says “hey i’m about to work on a project in here!”
    • git add README.md
    • git commit -m "first commit": committing requires you to give a little summary of what you’re adding.
    • git remove add origin git@github.com:username/repo_name.git
    • git push -u origin master
  • Windows people: do this and then this.

Homework

We’re going to create a repository for homework.

Creating a Github repository

  • Create a new repository, name your repo, make it public.
  • Click ‘Initialize this repository with a README’ option. (You don’t really need to add a license, but you can)
  • On your README.md file, click the little pencil icon (“Edit this File”).
  • Using markdown on your README:
    • the # allows your to add headers to your page. ## and ### get you sub-headers of smaller and smaller sizes!
    • *italics*
    • **bold**
    • [add a link](http://www.google.com)
    • Make a list:
      • * List Item
      • * Another list item
    • Make an ordered list
      1. 1. List Item
      2. 2. Another List Item

In your new Foundations Homework repo, use ‘Create new file’ link repo page to start a new file.

Cloning a repository

We want to connect this repo to our computer! To copy a repo from github to our computer, we will need to clone the repo. Use the green “Clone or Download” button and the “Copy to Clipboard” link to copy the HTTPS or SSH code.

Then in Terminal, navigate to where you want to save your repo. Then, git clone [HTTPS/SSH code]. You should have a new directory (that’s your homework repo in your folder).

You can use Finder to add folders and file into that directory! Or the following commands on Terminal/Babun:

  • cp [filepath to file you want] .: to copy a particular file into the current directory
  • mv [filepath to the file you want] . to move a particular file into the current directory

Check what’s going on in your directory: git status (when you are in the correct folder)

  1. Add new files: git add .
  2. Commit your changes: git commit -m "updates" – you mayyyy have to use -am instead of -m
  3. Push to Github: git push

Add a .gitignore file and in that file, add the line .DS_Store – this prevents files with that name from being add to github; this will make pushing and pulling much more seamless.

Add a README.md to each of your HW directories.

  • atom .: opens a new window that has the current directory in it

When you make a change on Github, you make to make sure your local directory also has the new changes (use git pull to keep your local directory updated with what’s up on Github).

Add READMEs in your 02 and 03 directories.

  • Save them as README.md in their respective directories
  • Atom knows about git and knows when we’re in a git repo. It changes those file names to be green to reflect that we’ve added or changed those files but have not add or committed the changes.
  • git add .
  • git commit -m "adding new README files to hw assignments"
  • Now green highlighting in Atom will go away. But we still need to git push to see the changes on Github
  • We get an error – our push was rejected. Suggests that you integrate the remote changes.
  • git pull to solve this issue! And then git push

For your homework: Create a new folder, put your homework in there, git add/git commit/git push.

Spotify Data

Let’s take a peak at the data you’ll be looking at for homework. cd into the directory where you downloaded Soma’s Spotify data. Reminder: Soma got the data from Spotify’s API and saved it in a variable called “data” – for the purpose of class notes, I won’t copy the contents of data here (but remember that it is held in the same python script that you see below).

print(data)
# Let's try to figure out what our data type is
print(type(data))

#  try to get the keys
print(data.keys)
# that didn't work because it's a method! we need to use paratheses.
# you'll see this error: <built-in method keys of dict object at 0x10e87e288>
# this will fix that error:
print(data.keys())

If you know that you’re looking at a dictionary, the best way to learn more about what it looks like is to use the .keys() method. Once you have the names of the keys, you can use dictionary_name['key'] to access the values/data stored in specific keys.

print(data['artists'])
# it looks like a dictionary inside a dictionary (because our above print showed curly braces on the outside)

print(type(data['artists']))
# confirmed! dictionary!

# let's get keys again
print((data['artists']).keys())

Keep using the type() function and the .keys() method to investigate your dictionary further. If you’re dealing with data that has nested dictionaries within dictionaries, it may be a good idea to use variables to make your code more readable (as Some does below).

# if this seems confusing, you can also make a new variable out of the above and then use .keys()
artist_data = data['artists']
print(artist_data.keys())

Because we don’t know what’s in each key, Soma prints the contents of each of the keys one by one:

print(data['artists']['next'])
print(data['artists']['href'])
print(data['artists']['limit'])

And here he finds what he’s looking for:

# here's the real deal
print(data['artists']['items'])
print(type(data['artists']['items']))

print(data['artists']['items'][0])

Notice that in the last print statement, he used [0] to pull out data. This sort of indexing is a clue that the data type you’re looking at is a list! In the code below, he uses a variable called artists to “save” that list/simplify how he calls it.

artists = data['artists']['items']
print(artists)

print("Here is the first Drake result")
print(artists[0])

print(type(artists[0]))
# it's a dictionary!

Because we don’t know what each item in our artists list is, he uses type(artists[0]) to check the data type of the first item and discovers that artists is actually a list of dictionaries!

He knows he can call and print each item in the list by using its index (but maybe there’s a more programmatic way of doing the same thing…).

print((artists[0]).keys())
print(artists[0]['name'], "has a popularity of", artists[0]['popularity'])
print(artists[1]['name'], "has a popularity of", artists[1]['popularity'])
print(artists[2]['name'], "has a popularity of", artists[2]['popularity'])

# how many artists do we have?

print(len(artists))

And here is the more programmatic way of printing every item in a list! For loops!

# Don't Repeat Yourself.... but we still want to go through every single one of those artists
for artist in artists:
    print(artist['name'], "has a popularity of", artist['popularity'])

Soma only cares about the real Drake and by investigating the names and popularities scores above, he knows that artists[0] is the real Drake. Let’s look further into “real Drake”:

real_drake = artists[0]

print(real_drake.keys())
print(real_drake['uri'])
print(real_drake['href'])
print(real_drake['images'])

print(real_drake['images'][0])
print(type(real_drake['images'][0]))

print(real_drake['images'][0].keys())

# let's get the URL of the first image of Drake
print(real_drake['images'][0]['url'])

We can use a for loop again to see each of the images stores associated with Drake on Spotify.

# What's your favorite image of Drake?
for image in real_drake['images']:
    # print type(image)
    # print(image.keys())
    print(image['url'])

Note: The code above is a great demo of how to investigate data you aren’t familiar with. It shows you how to poke through lists and dictionaries – make sure you know what functions and methods make sense for each data type and what clues you have for understanding the structure of your data better. You aren’t printing for the sake of printing! Look at what you have printed – what clues do you see? (HINT: look to see whether your prints end in ] or }).