CS 151: Lab #1

Lab Exercise 1: Getting around a computer

Main course page

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.


Tasks

  1. 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. A terminal will usually start in the home directory of your account.

    In a terminal we often need to specify a file or directory on the hard drive. The complete description of where a directory or file on a computer is located is called its path.

    Directories in a path are separated by a forward slash /. The topmost directory on the filesystem 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. For example, the following is the path to my home directory.

    /Users/maxwell

    Most of the time, however, you will be specifying paths relative to your current directory. Any path that starts without a slash is relative to your current working directory. 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 important terminal commands are the following. We'll walk through how to use them together in lab.

    • ls - with no argument, ls lists the files in the current directory. If you put a path after the ls command, it will list the contents of the specified path.
    • cd <directory> - change directory. If you use the command with no arguments, it changes the directory back to your top-level home directory. The current directory is specified as . and the parent directory of the current working directory is specified using ..

      cd Set the current directory to your home directory
      cd .. Move up to the parent directory of the current directory
      cd blah     Move into the sub-directory blah
      cd - Move to your last working 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

    Some useful terminal properties are the following.

    • Wildcard characters: the star character * is a wildcard character. If you want to see all the files in a directory that start with A, you can type:

      ls A*

      A possibly bad thing to do is to type rm *

    • Tab completion: when you have typed part of the name of a file or program, hitting the tab key will complete the filename as far as possible while the choice is unique. For example, if you have only one file that starts with the letter b, then typing b and then the tab key will complete the filename.

      If you have two or more files that start with b, tab completion will beep at you. If you hit tab again, then the terminal will show you the options (all the files that start with b).

    • Hitting return - you don't have to be at the end of the line to hit return. If you go back and edit something in a terminal command, you can hit return and execute the whole line no matter where the cursor is located. Try it.

    Some useful (but not as common) terminal commands are the following.

    • pwd - tells you the complete pathname of the current directory
    • less <filename> - scroll through a file
    • cat <filename> - send the file to standard output
    • echo "a string" write the text within the string to standard 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.

  2. 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. We'll go over a few in lab together.

    nano/pico - 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.

    nano <filename>

    Open up a file called smart.py and put in the line:

    print 'You are smart'

    Then quit nano and run the file by using:

    python smart.py

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

    Some other editors you should know about include the following:

    • TextWrangler - this is the recommended editor for this course, and it is a free download. Go to the TextWrangler web site to get your own copy.
    • BBEdit - BBEdit is an upscale version of TextWrangler designed for writing code and html (web pages). Colby has a set of licenses for it, and it is installed on the lab computers and the computers on Mudd 4th. It has a few more features than TextWrangler, but will only work on the lab computers.
    • emacs - emacs is a text editor that has been used heavily by computer scientists for almost 40 years. It has many powerful capabilities, and the key commands in emacs are used in many other places (like TextWrangler). 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).

    • JEdit - JEdit is very simlar to TextWrangler, and you can download it for free and run it on any operating system. Many students use it since it runs on Windows. It is slightly more complex than TextWrangler.
    • XCode - XCode is a powerful integrated development environment for Mac OSX, but you can also use it just for its editor, which is pretty decent.

  3. 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.

    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. The label field is a text field at the bottom of the page, and is different from the title of your 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.

    Always use a meaningful title for your wiki pages--something like 'CS 151 Project 1'--so that the page content is clear from the title.


  4. Fileservers - a fileserver is a central file system you can access from any computer. It's like having a virtual USB key you can plug into a computer to store your files. Colby maintains several different fileservers we use in this course.

    You can mount your the Colby fileserver directories by going to the Finder and typing cmd-K, or selecting 'Connect To Server...' from the Go menu. It will bring up a dialog box, into which you want to enter the following.

      Mac 10.6 (CS Computer Lab): smb://files.colby.edu/MACFiles
      Mac 10.7 and above (Lion/Mountain Lion):   smb://files.colby.edu/MyFiles
      Windows: \\files.colby.edu\MyFiles

    Mounting the file systems this way should give you access to all of the file systems you can access. The important ones are Personal and Courses. The Personal directory is for your own use, anywhere on campus. It gets backed up regularly, and you can access it from any computer on the Colby network. We strongly suggest you store all of your work for this course into your personal directory. You can, in fact, work directly from your personal directory.

    The Courses directory is where you will hand in your code. You should have access to a directory called CS151, and within that, a directory with your user name. Files that you put into that directory you can edit, read, and write, and the professor can edit, read, and write, but no one else. To hand in your code and other materials, you will create a new directory, such as project1, and then copy your code into the project directory for that week.

    You can also mount your personal directory explicitly, using the the following path in the 'Connect To Server...' dialog.

    smb://filer/personal/<first initial>/<username>


  1. Python - 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 into 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.

    Create a hexagon

  2. 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 either by typing:

    python <filename>

    or by typing

    import <filename>

    while you are in the python interpreter.

    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.

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