Due: , 11:59 pm
The purpose of this lab 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.
Labs and projects are tightly coupled. Each project is prefaced with a set of lab exercises coded in Python (v 3.6). In the projects, you will write code that expands on the code written in lab. The programs typically create images. You will be posting some of these images, along with a description of the project, on a Wiki page. You will then turn in your code by putting it in a special directory on one of the campus computer servers. Today's lab will guide you through each of these tasks.
Open up the Terminal application, which is Apple's command line environment.
A command line environment 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 the terminal. (The reverse is not true.)
When working in a terminal, the current directory is called your
working directory. (Type
pwd--which stands for Print Working Directory--and hit Enter in the terminal, and it will display 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
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
path to your instructor's home directory is:
(Recall that my username is
eaaron; your path would have your username in it.)
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.
Below are some important Terminal commands. We'll walk through how to use them together in lab.
ls- With no argument,
lslists the files in the current directory. If you put a path after the
lscommand, 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 ~- Set the current directory to your home directory. (The
~character is shorthand for 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). Note that this is not the same as dragging a file to the trash. It cannot be undone. Once the filed as been removed, it is gone for good.
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:
Heads Up:   A possibly bad thing to do is to type
Tab completion - When you have typed part of the name of a file
or program, pressing 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 press tab again, then the terminal will show you the options (all the files that start with 'b').
Some useful (but not as common) terminal commands are the following.
pwd- Display 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>- Either update the modification date on an existing file or create an empty file if the named file does not yet exist. This can be useful in various situations, like when you are learning to create, rename, and delete files using a terminal.
A file server 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 two different file servers we use in this course. One of these (the Personal server) is for you to store the code you are working on. The other (Courses) is for you to turn in code. We will give instructions regarding the Courses directory in the project instructions. For now, let's focus on where you should keep code that is work in progress.
To mount the file server for our CS152 work, go to the Finder and type Cmd-k, or select 'Connect To Server...' from the Go menu. It will bring up a dialog box, into which you want to enter the following.
You can then go from there to your home directory -- click on the appropriate directories that come up until you find your personal directory.
This file system is 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. Let's practice doing that for the first project.
In the terminal, navigate to it. Type
cd followed by the path
to the directory (and, as usual, hit Enter). E.g. for your instructor, it may look like this:
Or, use a shortcut. If you type cd in the terminal, then drag the icon for your directory onto the terminal, then the path to it will automatically be pasted.
mkdir project1into the terminal.
cd project1into the 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 easier to read the code. There are many editors to choose from.
In lab, I will be using TextWrangler (or the free version of BBEdit, which is closely related). It is a free text editor for the Mac and has all the features we need for writing programs. If you would like to install a free version of BBEdit on your personal computer, go to the BBEdit download site and follow their instructions. (You might also want to look at BBEdit's FAQs about switching from TextWrangler to BBEdit.)
Put in the line:
print('You are smart')
Now, you can instruct Python to run the program. In the terminal type
Congratulations, you just ran your first Python program! (Python humor)
Note that it works only if the current working directory is the one that contains the file smart.py. So, when working on a project, please be sure that your Terminal is in the right directory. Think of it as making sure TextWrangler and Terminal agree about where the programs are. Note that, in general, you run Python programs by typing
Here are some other text editors you should know about:
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).
vim - vim is an extended version of the classic vi text editor, which was created 40 years ago for the influential Unix operating system. (In fact, the name vim is said to be a short form of Vi IMproved.) Like emacs, vi is extremely powerful, although it has a bit of a learning curve for newcomers. It can be a useful, convenient editor for systems programming on Unix-based systems (such as the popular Linux operating system).
Users of vi and users of emacs have sometimes disagreed on which text editor was preferable--see the Wikipedia page on the (humorously named, in your instructor's opinion) Editor war. Feel free to ask your instructor if you have any questions about vi.
Python is a simple yet powerful interpreted language for making computers do stuff.
We generally write code using a text editor and then run the code using the command line, just as in the prior example. Now we're going to write a program that takes in three numbers and calculates their sum and average.
Type your name, a date, and the course and project in a comment at the top of the file. Every Python file you ever write should start with these three lines.
A comment in Python is any text preceded by a hash
#. Comments are not executed,
so they do not have to contain Python-approved syntax. This is a great way to annotate your code with useful information
in a human-friendly format, like sentences. It is essential that you use comments throughout your code to
explain what it is meant to do and why.
Type the following three lines of code:
print('version 1') print('sum', 42 + 21 + 5) print('avg', (42 + 21 + 5)/3)
Run the program from the terminal:
What does the program do? What happens if you change the 5 to a 6 on the last two lines of code and run it again?
Remember, you have to save the file before you run it again on the Terminal.
For more information about Python, see the Python 3.6 documentation pages for reference materials and tutorials.
Colby Wiki - a Wiki is a convenient way to put content up on the web quickly and easily. Below is a link to some videos on how to create your personal space and then go through the process of creating a new page in your space.
We won't watch the videos during lab--we'll just walk through the main steps together. To start, go to wiki.colby.edu, click the Log In button, and log in with your Colby username and password.
From there, you'll be able to create your own personal Wiki space--look in the drop-down menu at the top right of your screen for an item for this. IMPORTANT: When you're logged in to the wiki, please wait until your instructor says "Your turn" before clicking on the "Create your personal space" link. The system can't handle more than one space-creation at a time.
For each project, you will create a new page in your wiki space. You can edit any page in your space by going to the Edit tab. There you may find one or two options for editing your page. The Rich-text option (available when using FireFox browser) 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.
If you ever need to remove a page, go to that page and select "Remove" from the "Tools" menu in the upper right.
Remember to save your wiki page before navigating away from it. The wiki won't detect it and remind you.
You will turn in your code by putting it in a directory in the Courses server. On the Courses server, you should have access to a directory called CS152, 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.
To get to the appropriate directory, mount the Colby file server root directory 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.
Then click on the CS152 directory, and then your hand-in directory (it will have your username as its name).
Practice turning in your code by copying your entire project1 directory from your Personal server into your private folder on the Courses server. The easiest way to do this is to drag and drop the folder from one Finder (one open to Personal) to another (one open to your private folder on Courses).
When you are done with the lab exercises, you may start on the rest of the project.
© 2019 Eric Aaron (with contributions from Colby CS colleagues).