Lab Exercise 1:
Getting around a computer

xkcd.com

The purpose of this lab time is to give you an introduction to the basic tools we will be using this semester. These tools include a terminal, a text editor, and the Python interpreter.

Terminal

Open up the Terminal application, which is a text-based method of using your computer.

A terminal is simply a text-based interface to the computer. In a terminal, you can type commands, manipulate files, execute programs, and open documents. Anything you can do in the Finder, you can do in a terminal (the reverse is not true).

When working in a terminal, the current directory is called your working directory. The terminal will usually start in the home directory of your account, which is a location that is private to your user account on each computer. For example:

$ pwd
/Users/bseastwo

This is an example of a command entered at the terminal. The $ marks the terminal prompt. The text to the right of the $ indicates what a user has typed at the terminal (you do not type the dollar sign). The line below shows what gets printed as a result of the command. So, typing pwd at the terminal displays the current working directory.

We often need to specify the names of files or directories on the hard drive. The complete description of where a file or directory is located is called its path.

Directories are separated by a forward slash /. The topmost directory is indicated by a single slash. The directory tree for your computer is all relative to the top level directory. In other words, starting with a /, you can type the complete path to any file on your computer. The complete path to my home directory is /Users/bseastwo.

Most of the time you will be specifying paths relative to the current directory—any path that starts without a slash is a relative path. Note that paths are case-sensitive, so be sure to use proper capitalization. You will use paths and filenames to change directories, list the contents of directories, and run Python programs.

Some very important terminal commands are the following. We will walk through how to use them together in lab.

ls
lists the files in the current directory
cd <directory>
change directory. If you use the command with no arguments, it changes the directory back to your top-level home directory, /Users/username/. The current directory is specified as . and the parent directory of the current working directory is specified using ..
cdSet the current directory to your home directory
cd ..Move up to the parent directory of the current directory
cd blahMove into the sub-directory blah
cd -Move to your last working directory
mkdir <directory>
create a directory
mv <from> <to>
move a file, including just renaming it in the current directory. The mv command will remove the file in the old location after copying it to the new location.
cp <from> <to>
copy a file to a new location or name
rm <filename>
remove the file (will not remove a directory)
rm -r <directory>
remove the directory and all the files and subdirectories in it (be very careful with this one)

Some very useful terminal properties are the following.

Some other useful terminal commands are as follows.

pwd
tells you the complete pathname of the current directory
cat <filename>
print the contents of a file
less <filename>
scroll through a file
echo "a string"
write the text within the quotes to the terminal output
touch <filename>
touch either updates the modification date on a file or creates an empty file if the named file does not exist. This can be useful in various situations, like when you are learning to create, rename, and delete files using a terminal.

Text Editors

Text editors are the workhorse programs for writing code. You don't want fancy fonts or WYSIWYG layouts, you just want to see lines of text, preferably with syntax highlighting, which means that special words in a language are highlighted to make it easer to read the code.

There are many editors to choose from, and each programmer has their own reasons for choosing their favorite. We'll go over a few in lab together; try several and see which one you like best.

nano
nano (or pico on some systems) is a very simple text editor that works in a terminal. All of the commands use the control key and are listed at the bottom of the screen. To open up a file, just type the name of the editor followed by the name of the file. If the file does not exist nano creates the file.

For example, open a file called smart.py:

$ nano smart.py

Add the following line to the file:

print 'You are smart'

Then quit nano (Ctrl-o, Ctrl-x) and run the file:

$ python smart.py

Congratulations, you just ran your first python program. (Python humor.)

emacs
emacs is the battery-powered swiss-army knife with optional nuclear reactor of the editor world (emacs humor).

If you open emacs, the opening screen tells you how to access a tutorial. The most important thing to know is how to save and get out. To save, hold the control key down and type x then s (C-x C-s). It will ask you for a filename (and give you a default if you opened a file) and then save it. To exit, hold down the control key and type x then c (C-x C-c).

TextWrangler
TextWrangler is a very capable GUI text editor available for free. Go to the TextWrangler web site to get your own copy.
BBEdit
BBEdit is another GUI editor designed for writing code and html (web pages), but it is not free. Colby has a set of licenses for it, and it is installed on the lab computers and the computers in Roberts 225.
JEdit
JEdit is very similar to BBEdit, except that you can download it for free and run it on any operating system. Many students like to use it instead of BBEdit since they can use it on their own computer. It is slightly more complex than TextWrangler.
XCode
XCode is a powerful development environment for Macs, but you can also use it just for its editor, which is pretty decent.

Python

Python is a simple yet powerful interpreted language for making computers do stuff

You have already created and run a very simply python program. You can startup the Python interpreter in interactive mode by typing python in the terminal.

Many people have written modules for python that do some sophisticated things. One of those modules is called turtle and implements turtle graphics (e.g. forward, left, right, pen down, and pen up). To import the turtle graphics module, just type:

from turtle import *

Then call the function reset(), which should bring up a window with a turtle in the middle (OK, it's just an arrow, but pretend).

We can make the turtle move forwards, backwards, turn left, turn right, and pick up or put down the pen using the following commands.

forward(x)
move forward x pixels
backward(x)
move backward x pixels
left(a)
turn left a degrees
right(a)
turn right a degrees
up()
pick up the pen (don't draw when the turtle moves)
down()
put the pen down (draw when the turtle moves)

More complete documentation is available on the Python documentation site.

Draw a hexagon using turtle graphics.

Files

As you continue on, you may want to store a sequence of commands in a file. Then you can execute the sequence of commands in the file by typing from the command line:

$ python <filename>

Create a file with a .py ending and write some turtle commands into the file that make a shape. On the last line, put the instruction:

raw_input('hit Enter to continue')

That will keep the turtle window open until you hit return in the terminal. When you are finished, save a copy of your python file to your personal directory on filer, as discussed below

Network

You will want to keep the files you create in the lab on a network file server. The lab computers can (and probably will) be cleared at any time, and you do not want to lose your work. Using a file server is like having a virtual USB key that you can plug into a computer to store your files.

Colby maintains two different file servers used in CS151:
filer is for personal use, not just CS151.
fileserver1 is for submitting assignments; use the private directory within your user's directory.

You can access these network file servers from a Finder window. Select Go -> Connect To Server... from the menu bar (or press Command-k). This will bring up a dialog box titled "Connect to Server". Depending on which file server you want to connect to, enter the following network path, where you replace username and u with your Colby login name and first letter.

ServerPath
filer smb://filer/personal/students/u/username
fileserver1 smb://fileserver1/Academics/COMP/CS151/username

You can copy files to and from your network directories using Finder or the terminal.

We strongly suggest the following strategy for managing files. Create a directory called CS151 in your user's directory on the filer file server.

When you start each project, create a new directory within CS151 to store all the files you work on for that project (e.g. project01). If the project builds on what you did in a previous project, copy the files from the previous project into the new project directory.

This way, you will have one directory on the network drive that keeps all your work from this course. And when it comes time to hand in a project, you just copy that project's directory over to the Academics server. There is more information in this FAQ.

When you hand in your code for projects, you will do so by copying your files to the Academics file server. You should create a new directory for each project within your user's private directory on this file server. For example:
smb://fileserver1/Academics/COMP/CS151/username/private/project1.

Colby Wiki

A Wiki is a convenient way to put content up on the web quickly and easily. We'll watch a video on how to create your personal space and then go through the process of creating a new page in your space.

Colby Wiki
Tutorial Videos

You can edit any page in your space by going to the Edit tab. There you will find one or two options for editing your page. The Rich-text option (available when using FireFox) lets you do the normal select and point and click type formatting. The wiki markup lets you use the wiki markup language to make your page.

To make a new page that links from your home page, edit your home page in wiki markup mode and put the name of the new page in square brackets, like:

[Project 1]

After saving your page, the link should show up as red text. Clicking on the red text creates the new page and takes you to the editor for it. You can always choose to remove a page in the edit tab using the link on the upper right on the window.

You should make wiki pages only within your own space. We can link a page in your space to the course wiki page if you put a label on the page. Each assignment will specify a label. Once you have labeled a page, it will show up in a list on the course wiki home page.

 

When you are done with the lab exercises, you probably want to find a partner for the first assignment.