Homework 2: More basics

Due Date: Sunday, September 10th at 11:59 PM

Be sure to push the final version of your notebook to your GitHub repo. Follow the instructions on the course website

Topics:

1. Command line basics

2. Text editors

3. Installing a Python distribution

4. Making changes to your local repo



Problem 1: Command line basics

In Lecture 1 I asked you to use git from the command line. Macs come with a command line app (terminal.app) and since Macs are loosely similar to Unix the terminal commands are pretty much the same as those in Linux distributions. Windows is a bit different, and because of this, I had you install git BASH if you are using a Windows machine. What this means is that, even if you are using a Windows OS, you can still use the nice Unix commands as long as you are in the git BASH terminal.

It is true that there are various options for using git from the Desktop (e.g. see GitHub Desktop). However, it is more useful for you to be able to do things from the command line. The reasons for this are that the command line is not going anywhere any time soon and that I can't predict what GUI you're future company/group will want you to use. You'll be much more versatile using the command line.

What this means, is that I have to teach you a little bit about how to navigate with the command line. There are a few essential commands that you absolutely must know. You'll pick up the rest as you go through you life. Here are the essential ones:

  • ls: list the contents of the current directory
  • cd: change to a new directory
  • mv: rename a directory or file OR move a directory or file to a new location
  • cp: copy a directory or file
  • pwd: print the working directory
  • mkdir: create a new directory
  • rm: remove a file (you can remove a directory with rm -r)

Let's try to work with these a bit. In the following steps, you should hit the return key after typing each command.

  1. Open up a terminal and type pwd. The output should be your home directory.
  2. Next type ls. You should see all the files and directories in your home directory. Notice that if you type ls -l you get a list with more information where the files and directories are ordered alphabetically. Typing ls -lt sorts the list by date/time created with the most recent file/directory at the top. ls -ltr just reverses the order of the sort; now the most recent file/directory is at the bottom.
  3. Now type mkdir mydir. Voila! You have just created a new directory. Try ls again. Do you see the new directory?
  4. To enter that directory type cd mydir. You just changed into your new directory. Type pwd to see where you are.
  5. Now that you're inside your new directory, let's create a file in there.
    • Type echo 'Hello world!' > newfile.txt
    • Type ls. You should see the next text file!
    • To see the contents of the file, type cat newfile.txt.
  6. Suppose you now want to rename newfile.txt to README.txt. Just execute mv newfile.txt README.txt. Type ls again if you wish.
  7. Maybe you really wanted to keep a copy of newfile.txt. Just do cp README.txt newfile.txt. As usual, ls will show you what you want to see.
  8. What if newfile.txt should actually be in a different directory? Let's do the following:
    • mkdir newdir
    • mv newfile.txt newdir\
    • Altneratively, you could give the entire path of the new directory (not necessary here). Here's how you would do that:
      • pwd (just to see the current path)
      • mv newfile.txt path_from_pwd/newdir/
  9. That was fun, but this directory is completely useless to use now. Let's delete it.
    • First, you need to get out of the current directory. You can't delete a directory from within it!
    • To go up one directory, just type cd ...
      • Unix Note: In Unix, . stands for the current directory and .. stands for the parent directory (that is, the directory containing the current directory. Hence, cd ../ changes to the parent directory of the current directory.
      • Unix Note: If you want to go up two directories, just do cd ../../. You can use the same pattern for n directories.
    • Now type rm mydir. You should see the message rm: mydir/: is a directory. rm cannot be used to removed directories as-is. It can only remove files.
    • To remove the directory type rm -rv mydir. The -r option says to recursively remove the directory and any contents. The -v option says to be verbose while removing files.

That was a whirlwind tour through a few of the more useful Unix commands. It won't take you long to use them effectively.

Note: If you want more information about any command, just type man command_name into the terminal. This will bring up the manual page for that command. You will see all sorts of information. To scroll down, just hit the space bar. To exit the man page just type q (for quit). Here's an example of a portion of the man page for the rm command:

Bonus Command

Suppose you want to copy a file from a different location to your current directory. Well, recall that the current directory is represented by the . object. Here are a few things you can do:

  • cp path_to_file/file .: Will copy file to your current location
  • cp path_to_file/file ./sub_dir: Will copy file to the directory sub_dir in your current location
  • cp ../file .: Will copy file from the parent directory to your current location There are other, similar patterns to accomplish roughly the same thing. You'll figure them out as you go through life.

Problem 2: Text Editors

In the last exercise, you learned out to create a simple .txt file. Of course, you really don't want to use the echo command to write programs. That would be insane. Many of you will write programs using Interactive Development Environments (IDEs) such as Spyder. Spyder is shipped with Anaconda which we will be using when we get to Python. However, I would strongly recommend that you learn how to use a text editor to write your programs and documents.

There has been a battle raging over whether or not programmers who use IDEs are true developers. The answer is that they are and so are people who use text editors. One major advantage of using a text editor over an IDE is that you tend to learn what's going on "under the hood" which in turn makes you a better developer. Many of you will go on to use IDEs in your careers so I want to make sure that you learn how to use a text editor.

There are a variety of text editors (nano, vi, vim, emacs, notepad++, atom, sublime, gedit...). My preferred text editor is vim but it really comes down to personal preference. vim is shipped with most operating systems (which is really nice) and it is also included with git bash.

There are a few vim commands with which you should become familiar. First, to open a file (or a new file) simply type vim filename. You will get a new file if that filename does not yet exist. Otherwise, you will see the contents contained in filename.

In this exercise, you will edit the README.md file in your private repository. The .md extension stands for markdown. You should probably learn Markdown. It's super easy and quite nice. GitHub also plays nicely with Markdown.

  1. Open your terminal and navigate to your private course repository. Type ls. You should see README.md.
  2. Open README.md using your favorite text editor. This exercise will use vim, but I don't care what you use as long as it is a text editor. To open, type vim README.md.
  3. The first line should say # cs207-F17.
  4. Use the arrow keys to move your cursor over the letter c.
  5. Press v and move your cursor to the end of the line using the arrow keys. The v means that you're entering virtual mode.
  6. Now type d. You have just deleted the highlighted text.
  7. That sequense was somewhat advanced (but efficient). Now you'll learn one of the most important commands: the insert command. Type i. You have just entered insert mode.
  8. Now type something informative in. When you're done typing press the escape key: esc. You have now exited insert mode and returned to normal mode.
  9. Now save the file. Simply type :w followed by the return key.
  10. Now exit the editor by typing :q followed by the return key.
    • Note: Steps 10 and 11 could have been combined by typing :wq.

Great! Now you know the basics of using the vim text editor to edit files. You'll pick up a lot more commands the more you use vim. Here is a vim cheatsheet (one of many) vim cheatsheet.

Now you should update your local repository.

  1. Type git status. This tells you which files you changed. You should see that README.md was modified.
  2. Type git commit -a -m "Updated README file with more informative heading.". This will register the changes in your local git repo (but not the remote repository yet!). The -m option means that you're specifying a commit message and the string in quotes is the informative commit message. All of this is followed by the file you're committing. We'll learn a lot more about this in the coming lectures.
  3. Finally, type git push. This will push your changes to your remote repository on GitHub.

Problem 3: A few minor updates

Please do the following:

  1. Some of you named your course repo cs207work. Please rename it cs207_firstname_lastname. If you have already done this, then you're done and you can skip to Step 2. If not, here's how you can rename your repo:
    1. Go to your GitHub repo and click on the Settings tab. Change the name under Repository name.
    2. Now go to the local copy of your repository (remember, we cloned this last time). It's probably named cs207work if you're doing these steps.
    3. Type git remote set-url origin url_to_your_repo where url_to_your_repo will be something like https://github.com/github_username/cs207_firstname_lastname.git. What you just did was change the url of the remote repository. Recall that origin is the short name of your remote repository.
    4. As a sanity check, type git remote -v. It should list the correct url now.
    5. Finally, your directory should be updated to avoid any future confusion.
      • To navigate up one directory, type cd ...
      • Type mv cs207work cs207_firstname_lastname. You just renamed the directory!
  2. Give the teaching staff access to your private repository:
    1. Go to your private repository again on GitHub.
    2. Go to Settings again.
    3. Click on Collaborators on the left side.
    4. Add the TFs and me. Our GitHub usernames are:
      • dsondak
      • therealchuckliu
      • ericwu09
      • kevinwu23

Problem 4: Install SSH keys

You may have noticed that you are always promted for your GitHub username and password any time you try to interact with GitHub. That can be a bit annoying, especially if you're doing a lot of git work. To overcome this annoyance, you can set up SSH keys. Rather than re-write everything for you, I'll refer you to the GitHub website which has detailed instructions for Linux, Mac, and Windows users: Connecting to GitHub with SSH. Follow these steps:

  1. Checking for existing SSH keys: Be sure to select Mac or Windows as is appropriate for your system:
  2. Generating a new SSH key and adding it to the ssh-agent
  3. Adding a new SSH key to your GitHub account

Now you can clone your GitHub repos using https or ssh. GitHub recommends using https (like we've been doing), but it's really up to you. Here's a discussion: Why does GitHub recommend HTTPS over SSH?.

Of course, if you really do want to use ssh instead of https then you'll have to change your remote url. Remember, we set up our remotes using https. For example here is what my current remotes look like:

To change the remote to ssh just do

git remote set-url upstream git@github.com:IACS-CS-207/cs207-F17.git

where upstream is the short name for the remote repo that you're referring to and git@github.com:IACS-CS-207/cs207-F17.git is the ssh address of the remote repo.

Now here is what my repos look like:

You can change all of your remotes to be through ssh if you wish or keep them as https.


Problem 4: Install Anaconda and Open a Jupyter notebook

All of our coding work will be done in Python this semester. We will work with the Python3 distribution provided by Anaconda. Anaconda works with Windows, Mac, and Linux operating systems. You should download and install it now: Anaconda Download. Install the Python 3.6 version!

Once you have successfully installed Anaconda, you can start coding in Python. But first, for this course you need to be able to work with Jupyter notebooks. In fact, all of the lecture exercises will be done in Jupyter notebooks and some of the homeworks will be too.

There are two ways to open a Jupyter notebook. One is to open the Anaconda Navigator and Launch a Jupyter notebook. The other way is to launch a notebook from the command line. We'll continue working from the command line. Please navigate to the homeworks/HW2 directory now using the commands from Problem 1. To open a Jupyter notebook from your current directory just type jupyter notebook . .


Problem 5: Write some Markdown

Start a new Python3 notebook by clicking on Python 3 in the New dropdown menu.

You can rename the notebook by clicking on the current name at the top of the page. You should name this assignment HW2-final (as per the instructions on the course webpage).

You can write Python code but we'll put that on hold for now. For this first foray, just write some basic markdown. You can change the character of the cell by selecting the Code dropdown menu and choosing Markdown.

Notice that the cell is highlighted in blue. In order to actually edit the cell, you should hit the return (or enter) key. Now the cell should be be highlighed in green and your curser should be blinking inside the cell. You're ready to add content!

As mentioned previously, Markdown is a very easy language to work with. Now that you have your own Jupyter notebook open and have selected the Markdown cell, you're ready to write some Markdown. Here's a Markdown cheatsheet with everything you need: Markdown Cheatsheet.

The task is to write a short Markdown document. Your Markdown document must have at least one of the following:

  • Two different types of headers
  • A List
  • A table
  • A link

You can include other features if you want. Don't spend too long; this exercise is meant to simply introduce you to Markdown and to get you started working with Jupyter notebooks. You can even use Markdown to write the documentation for your final project! Once you've added all the content you want to add, you would like your notebook to display more than just ugly text and commands. To do this, you just run the cell. I usually do that by using the keyboard sequence control-return. Of course, you can also do it from the toolbar menu: You can choose whatever works best for you. Note: If you click on Help - Keyboard Shortcuts you will find all sorts of shortcuts that make your life easier and the time you spend in Jupyter more efficient.

Once you've completed your Markdown document you can save your notebook. Jupyter saves the notebook periodically but it's good practice to save it yourself. After saving the notebook, you can close it by opening the File menu and clicking on Close and Halt. Finally, go to the terminal window from which you launched Jupyter notebook and press control-C. Now everything is closed!


Exercise 6: Updating your remote repo on GitHub

We just started digging into git recently, but you need to commit the changes you just made to your repo. This will be the last time I ask you to blindly type in git commands. Maybe you're starting to feel more comfortable with the git workflow now anyways. Follow these steps:

  1. Make sure you're in the terminal window from which you launched the Jupyter notebook.
  2. Type git status. You should see something like the following image (with the appropriate modified files, usernames, etc.):
  3. Notice that your new notebook will not be listed under modified:. This is because git doesn't know about it yet! Your new notebook should be listed under Untracked files. You need to tell git about your new notebook. To do that, just type git add new_filename.
  4. Now type git status again. You'll see something similar, but not identical, to:
  5. Great, so now git is aware of the new file. Next, you need to commit that file to your local repository. To do so, just type git commit -m "descriptive message". I might suggest a description like L2 exercises and first Markdown page. You should see something like:
  6. Now your local repo is all up to date. But you're hosting everything on GitHub. So you now need to push the changes in your local repo to your remote repo on GitHub. To do that, just type git push. You'll see something that looks like:

And now you have the basic git workflow down. Of course, we haven't talked about what git is actually doing yet. And we'll have to discuss what to do when things go wrong. But at this point, using steps 1-6 above, you will be able to make local changes and update your GitHub repo.

Open your browser and go to your GitHub repo. You should see the changes along with your commit message.