Lab 1: Workflow and Toolchain
This labs focuses on the tools, techniques, and workflows used by Python programmers and programmers in general. We will adopt most of these best practices and use them for lab distribution, comments, and feedback
Step 0: Lab Preparation
-
Sign up for a GitHub
account. Please thoughtfully choose a username so that I can
reasonably infer who you are from it. Make sure to choose
the
free plan (it's the default) and don't set up an organization. After you've registered, please send me an email with your GitHub username. I will add you to the GitHub Williams CS organization and to the CS 135 Students team. - Read a virtualenv tutorial up to Why not virtualenv.
- Read a bit about pip and virtualenv
Step 1: SSH Keys
We will use SSH keys to idenitfy ourselves as trusted sources. This means you won't have to type a password every time you interact with GitHub. It's very convenient, secure and a good best practice.
Follow the instructions on GitHub to generate an SSH keypair and add your public key to your GitHub account.
Step 2: Forking
Navigate to the Lab 1 GitHub repository. Fork the repository by using the Fork button in the top right-hand corner. This copies the repository to your GitHub account.
Step 3: Cloning
You should now have a copy of the repository in your own GitHub account. Now clone the repository to your local disk.
- Look clone URL label along the bottom right of the repository page. You can clone via HTTPS, SSH, or Subversion. We'll use SSH so first click on that link and then click on the little clipboard with an arrow icon to copy the URL to the local clipboard.
- Open up the Terminal and navigate to your home directory with
$ cd
- Make a new directory called cs135 using
$ mkdir cs135
- Change directories into cs135 using
$ cd cs135
- Now clone your fork of lab 1 by typing git clone
and then pasting (⌘-v) the copied repo on the command
line. It should look like this
$ git clone git@github.com:<git-username>/cs135-lab1.git
- Change into the new repository directory using
$ cd cs135-lab1
Step 4: virtualenv
virtualenv is a program that isolates collections of python libraries for a particular programming project. It also allows you to associate a specific python verion with a project.
- Create a new python environment using
$ virtualenv -p python3 —-system-site-packages venv
(type it, don't copy it) - The -p python3 tells virtualenv that you want to use python3.x as your python interpreter
- The --system-site-packages says you want the site packages installed on the system to be available to you
- The venv is the directory where you want the project-specific libraries to live
- Explore the directory structures inside venv to see what happened
- You can activate the new environment by using
$ . venv/bin/activate
- This alters your PATH environment variable so that venv/bin/ is checked first
- Type
$ echo $PATH
to see what paths are searched and in what order - Deactivate the virual environment by typing
$ deactivate
at anytime (but not now)
Step 5: pip
Use pip to install the goslate package, which uses Google translation services to translate sentences from one language into another.
- Type
$ pip install goslate
to install the goslate python package - This package is now installed in your virtual environment
- Examine the directory venv/lib/python3.4/site-packages
Step 6: Text Editors
There are many good text editors. People swear by emacs and vim. In a pinch, you can also use nano. Currently, many people like sublime. Today we will use Atom, but in the future feel free to use whatever you want.- Edit the rain.py file by typing
$ atom rain.py
Step 7: Python
Opening the rain.py file in the text editor should reveal the following code.''' Use Google translate API via the goslate python package to translate the phrase "the rain in spain stays mainly on the plain" from english to spanish and then back to english again. ''' # make the goslate package available import goslate # create an instance of the Goslate class gs = goslate.Goslate() # our phrase english = 'the rain in spain stays mainly on the plain' # translate from english to spanish and store the result spanish = gs.translate(english, 'es') # translate from spanish back to english and store the result english2 = gs.translate(spanish, 'en') # print out the original and translated sentences print(english) print(spanish) print(english2)
- You can execute this code by typing
$ python rain.py
from the Terminal. Try it out. - Edit the code so that it translates from english to french instead of english to spanish. The two-character country code for france is fr.
- Try running your code. It should print
the rain in spain stays mainly on the plain la pluie en Espagne reste principalement dans la plaine the rain in Spain stays mainly in the plain
Step 8: Submitting Code
We will use Git and GitHub for submitting lab assignments.
Commit your changes
Recall that you cloned a forked a repository to your local disk. You need to save your changes to your locally cloned repository by committing them using$ git commit -m "updated to french" rain.py
Push your changes to GitHub
You will now push these changes back to your forked repository on GitHub using$ git push
Verify your changes on Github
Navigate now in your web browser to your forked repository on GitHub. It should be available athttps://github.com/<git-username>/cs135-lab1
. If you click on rain.py you should see your changes reflected there.Issue a pull request
You should see a little green button next to the repostory name and branch on your GitHub repository page. Clicking this link will issue a pull request to the base repository from which the fork was originally made. Go ahead and issue a pull request.