Cover page images (keyboard)

Lab 2 - emacs & errors

Sue Evans & Patti Ordonez


Hit the space bar for next slide




Overview

Today's lab introduces you to emacs, the preferred text editor for this class, as well as the process of writing, debugging, and running your code.

  1. Your Quota

  2. Editors
    • emacs
    • vi

  3. A Simple Program
    • Where to place your 201 files.
    • Creating convert.py
    • Running convert.py

  4. Exercise
    • Errors Lab
    • Logging Out











Quota

A Note About Your Quota...

Keep an eye on your quota!! Each user is given a certain amount of disk space (299MB) on the GL network. If you exceed this disk space, we cannot send you email (i.e. project grades) and you can't create files (i.e. homework & project files) To check your quota, at the linux prompt, type:

linux3[16]% quota -v
Volume Name             Quota       Used  %Used    Partition
user.natalie2           100000      63551   64%          37%
linux3[17]%

This will show how much disk space you are using. If the percentage used is approaching 100%, you need to clean up your account. This can be done by typing oitcleaner at the linux prompt:

linux3[18]% oitcleaner
This is the cleaner that we want
Volume Name                   Quota      Used %Used   Partition
user.natalie2                100000     63551   64%         37%
**** Start Quota                    ****
**** Start: Cleaning Internet Files ****
find: /afs/umbc.edu/users/n/a/natalie2/home/.2kprofile/Application 
Data/Mozilla/Profiles: No such file or directory
**** Done:  Cleaning Internet Files ****
**** Start: Cleaning Misc. Files    ****
**** Done:  Cleaning Misc. Files    ****
**** Finding Large Files            ****
rm: remove regular file 
`/afs/umbc.edu/users/n/a/natalie2/home/../pub/www/DMP/finalpresentation.ppt'? n
rm: remove regular file 
`/afs/umbc.edu/users/n/a/natalie2/home/../pub/www/DMP/finalpaper.pdf'? n
rm: remove regular file 
`/afs/umbc.edu/users/n/a/natalie2/home/../pub/www/DMP.tar'? n
**** Finished                       ****
Volume Name                   Quota      Used %Used   Partition
user.natalie2                100000     63538   64%         37%
**** End Quota                      ****
linux3[19]%

This script will clear out some unnecessary files in your account. It will also ask you if you want to delete any large files it finds. You must answer either y or n when it prompts you to remove. Make sure to remove any files named core.












Text Editors

Python programs are written using a text editor. They are then executed independently of the editor. For this class, we'll be covering emacs only.

  1. Emacs

    The version of emacs on the school machines have many enhancements specifically for X Windows. There is a graphical toolbar (like in MS Word etc..) for basic options as well as a text driven toolbar for other options. To invoke emacs just type emacs, the name of the file you want to edit, and if you do not want to tie up the shell, use an & afterwards
                                  emacs examplefilename.py &
    The shell is a program that is a command line interface to your operating system when you open up a terminal window. The & starts up your program in a new shell in a new terminal window. This allows you to edit in one window and run your program in another instead of repeatedly getting into and out of the editor. When working from home, the & doesn't work, so just login twice to have two windows.

     

    Some helpful Emacs commands

    • Ctrl-x Ctrl-s > This command is used to save the file while in the Emacs editor.
    • Ctrl-x Ctrl-c > This command is used to exit from the Emacs editor.
    • Ctrl-a > This command is used to move to the beginning of the line.
    • Ctrl-e > This command is used to move to the end of the line.
    • Ctrl-k > This command is used to delete (kill) the text from current cursor position to end of line.
    • Ctrl-y > This command is used to retrieve (yank) the text last deleted (killed) and place it at the cursor location.

     

  2. Vi
    • For those who already know Vi, you may continue to use it, but we can't guarantee to help you with it.  A listing of vi commands can be found at http://www.umbc.edu/oit/Unix/.

     












labs directory

Where to place your 201 homeworks and projects

You should have made a directory for all of your 201 homeworks and projects in Lab #1. If you didn't, at the linux prompt type mkdir 201 and make sure you do Lab 1 before you start doing homeworks.

Now let's create a directory for our labs.
cd into your 201 directory.
Type mkdir labs at the linux prompt.
Change into your labs directory and make a directory for today's lab, lab #2 by typing cd labs, then mkdir lab2.
Change into your lab2 directory (cd lab2) and proceed to the next step.












Creating convert.py

Now you are ready to start up emacs and begin typing your code.

  1. Right click on the desktop to open a terminal.
  2. SSH into your UMBC account by typing ssh gl.umbc.edu
  3. Type emacs convert.py &. This will open a blank file called convert.py in the emacs text editor.
  4. Once the new window opens, you can begin typing your 'convert.py' program.
  5. Save your file by typing Ctrl-x Ctrl-s.

If we are short on time, just type a very minimal comment. You'll see that the indentation will be done automatically. This is because the emacs text editor knows the format of Python files. Any file you make that ends in .py will be treated as a Python source file by the editor.


# File:        convert.py
# Written by:  Patti Ordonez
# Date:        5/?/09
# Section:     All
# Email:       ordopa1@umbc.edu
# Description: This program converts degrees in celsius to 
#	           degrees in fahrenheit.  


degC = input("What is the Celsius temperature ? ")

degF = (9.0/5.0) * degC + 32

print "The temperature is ", degF, " degrees in fahrenheit"












Running your program

There are actually a couple of ways you can run your program. We're going to try them.


# File:        convert.py
# Written by:  Patti Ordonez
# Date:        5/?/09
# Section:     All
# Email:       ordopa1@umbc.edu
# Description: This program converts degrees in celsius to 
#	           degrees in fahrenheit.  

def main():

	degC = input("What is the Celsius temperature ? ")

	degF = (9.0 / 5.0) * degC + 32

	print "The temperature is ", degF, " degress in fahrenheit"

main()

You can now run your program multiple times in the interactive shell.
Start the shell by typing python.
As before, load and run the program by typing import convert
To run the program again, type convert.main()










errors.py - Step 0

Download the following file into a file named errors.py by typing the command:
cp /afs/umbc.edu/users/b/o/bogar/pub/errors.py .

linux2[52]% cat errors.py

# File:        errors.py
# Written by:  Patti Ordonez
# Date:        5/?/09
# Section:     All
# Email:       ordopa1@umbc.edu
# Description: This program contains some buggy code 
#              for you to correct.

print "This program finds the average of two integers."

num1, num2 = input("Enter two numbers separated by a comma");
average = (num1 + Num2) / 2

print "The average of the two numbers is " +  average












errors.py - Step 1

Run the program using the command below.
Enter the values 3,4 when prompted.
It should produce the following error messages.

linux2[63]% python errors.py
This program finds the average of two integers.
Enter two numbers separated by a comma3,4
Traceback (most recent call last):
  File "errors.py", line 10, in ?
    main()
  File "errors.py", line 8, in main
    average = (num1 + Num2) / 2
NameError: global name 'Num2' is not defined












errors.py - Step 2

Examining the error messages after we entered the values 3,4, the interpreter is tracing back an error that occurred in line 10 to one that occurred in line 8 in main. It displays the line and tells us, we have a NameError. Num2 is not defined. Examine the code and try to fix the error.

linux2[63]% python errors.py
This program finds the average of two integers.
Enter two numbers separated by a comma3,4
Traceback (most recent call last):
  File "errors.py", line 10, in ?
    main()
  File "errors.py", line 8, in main
    average = (num1 + Num2) / 2
NameError: global name 'Num2' is not defined












errors.py - Step 3

Examining the error messages after we entered the values 3,4, the interpreter is tracing back an error that occurred in line 10 to one that occurred in line 9 in main. It displays the line and tells us that we have a TypeError. We can not concatenate 'str' objects which are of type string with 'int' objects which are of type integer. Examine the code at line 9 and try to fix the error.


linux2[85]% python errors.py
This program finds the average of two integers.
Enter two numbers separated by a comma3,4
Traceback (most recent call last):
  File "errors.py", line 10, in ?
    main()
  File "errors.py", line 9, in main
    print "The average of the two numbers is " +  average
TypeError: cannot concatenate 'str' and 'int' objects
linux2[86]%












errors.py - Step 4

We are not getting any errors when we run the code, but the output is not correct. The average of the numbers 3 and 4 should be 3.5 not 3. Examine the code and try to fix the error.


linux2[97]% python errors.py
This program finds the average of two integers.
Enter two numbers separated by a comma3,4
The average of the two numbers is  3
linux2[98]%













Bonus Step - errors.py

Now the code is running exactly as expected. However, we would like to see a space after the word comma. Examine the code and try to fix the error.

linux2[104]% python errors.py
This program finds the average of two integers.
Enter two numbers separated by a comma3,4
The average of the two numbers is  3.5
linux2[105]%












Challenge problem

Congratulations, you have successfully debugged a Python script. If you have some time left over, attempt to debug the following code, which includes pieces of code we've yet to cover.


import math

def IsPrime(n):
	if n == 2:
		return True
	for divisor in range(3, int(math.sqrt(n)), 2):
		if n % divisor == 0:
			return False
	return True

for i in [2, 3, 16, 17, 24, 25]:
	print "Is %d prime?  %s" % (i, IsPrime(i))