Lab Exercise 1: Getting around a computer
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.
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.
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:
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.
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
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.
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:
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
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.
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.
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:
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.
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.
Python - a simple yet powerful interpreted language for making
computers do stuff
You have already created and run a very simple python program. You can start up the python interpreter in interactive mode by typing python in the terminal.
The python interpreter is useful for trying out simple things. For example, we can use the interpreter as a calculater by typing mathematical expressions.
>> 5 + 5 * 2
You can also assign values to variables and print out their values or expressions based on their values.
>> a = 5
>> b = 5
>> print a + b
To quit the python interpeter, just type cntl-d (hold down the cntl key and type d).
One important aspect of python is that you can import packages that have code written by you or other people. Save the following two files to your working directory.
Start up the python interpreter and then import the soundturtle package using the following statement. This is one of two methods of importing a package. This form of importing lets you use functions from the soundturtle package without any prefix.
from soundturtle import *
The soundturtle package is a simple interface to a MIDI synthesizer. It will work on the lab computers. If you want to use it on your own computer, stop by outside of lab and we can get you set up. It requires the fluidsynth application and the pyfluidsynth package in order to work.
With the soundturtle package, you can play a sound for a certain duration by using the play(d) function, where d is the time in seconds to play the note. You can adjust the pitch of the note, change the velocity (sort of the volume), and even play chords. The following is a quick description of the functions. Try them all out.
play( d ) Play the current pitch for the given duration in seconds using the current velocity. The play() function halts the program operation while the note is playing. hit( d ) Play the current pitch for the given duration in seconds using the current velocity. The hit() function does not halt program operation while the note plays, which means you can hit multiple notes and have them play simultaneously. rest( d ) Rest for the specified amount of time. The rest() function will halt program operation for the duration of the rest. However, previously hit notes will continue to play. up( o ) Move the current pitch up by the specified offset. A MIDI step corresponds to a half-step, and there are 12 pitches in a scale. This function does not play a sound. down( o ) Move the current pitch down by the specified offset. This function does not play a sound. pitch( p ) Set the current pitch to the value p (range 0 to 127). If you call the function with no arguments, it returns the current pitch value. velocity( v ) Set the current velocity to the value v (range 0 to 127). If you call the function with no arguments, it returns the current velocity value. damp() Immediately stops any currently playing notes that were begun with a call to hit().
One thing you'll find very quickly is that you want to save a sequnce of commands so you can edit them and play them repeatedly. Open a text editor and create a file with a .py ending, then put your python commands into the file. You can run the file using the Terminal. The following would run the file hworld.py
Once you are comfortable with the above topics, move on to the current assignment.