Difference between revisions of "CSC212 Lab 1 2014"

From dftwiki3
Jump to: navigation, search
(Part 3: Playing with emacs)
Line 194: Line 194:
 
Simply work on the first paragraph of the file, and try to straighten it up. I have indicated below, on the right-hand side, which emacs commands you can use to fix the text on your screen.
 
Simply work on the first paragraph of the file, and try to straighten it up. I have indicated below, on the right-hand side, which emacs commands you can use to fix the text on your screen.
  
[[Image:fulghum_emacs.jpg]]
+
[[Image:fulghum_emacs.jpg|700px]]
  
 
When you are done with the modification, or if you want to stop for right now and save your modifications, type C-x C-c, and press y when emacs asks if you want to save the file.
 
When you are done with the modification, or if you want to stop for right now and save your modifications, type C-x C-c, and press y when emacs asks if you want to save the file.

Revision as of 11:33, 3 September 2014

--D. Thiebaut (talk) 09:37, 3 September 2014 (EDT)


Part 1: Python Review


The problem below is taken from CSC111, Homework 10 given Spring 2014. Work out a solution in pair or individually. There is nothing to submit for this part.

Lost Cat

(image from nicepixy.net)

Cats.jpg


The program below is missing its class! And it is also missing its documentation! It used to contain a class defined, with methods and member variables, and a fully developed documentation, but unfortunately all of them got erased.

We do have the output of a run of the program, though, when it still had its class definition. Both the incomplete code and the output are shown below.


#
# missing class goes here...
#



def getNewCat():
    print( "\nPlease enter the information for the new cat:")
    name = input(       "Cat name?         " )
    age  = int( input(  "Age, in years?    " ) )
    vac  = input(       "Vaccinated (Y/N)? " ).strip().lower()
    vac  = vac in ['y', 'yes']
    neut = input(       "Neutered (Y/N)?   " ).strip().lower()
    neut = neut in ['y', 'yes' ]
    breed= input(       "Breed?            " ).strip()
    
    return Cat( name, age, vac, neut, breed )

def display( L, caption ):
    print( "\n\n"+caption )
    print( "=" * len( caption ) )
    for cat in L:
        print( cat )

def main():
    text="""Minou, 3, Yes, Yes, stray
            Max, 1, Yes, No, Burmese
            Gizmo, 2, No, No, Bengal
            Garfield, 4, Yes, Yes, Orange Tabby"""

    for line in text.split( "\n" ):
        words = line.strip().split( "," )
        if len( words ) != 5:
            continue
        name, age, vaccinated, neutered, breed = words
        age = int( age.strip() )
        if vaccinated.strip().lower() == "yes":
            vaccinated = True
        else:
            vaccinated = False

        neutered = neutered.strip().lower() == "yes"
        
        cat = Cat( name.strip(), age, vaccinated, neutered, breed.strip() )
        try:
            cats.append( cat )
        except NameError:
            cats = []
            cats.append( cat )

    display( cats, "List of cats:" )

    cats.append( getNewCat() )

    display( cats, "List of cats with new addition:" )

    vaccinatedNeuteredCats = []
    for cat in cats:
        if cat.isVaccinated() and cat.isNeutered():
            vaccinatedNeuteredCats.append( cat )

    display( vaccinatedNeuteredCats, "Vaccinated and neutered cats:" )
    
    
main()


Output


List of cats:
=============
Cat: Minou, age: 3, vaccinated, neutered (stray)
Cat: Max, age: 1, vaccinated, not neutered (Burmese)
Cat: Gizmo, age: 2, not vaccinated, not neutered (Bengal)
Cat: Garfield, age: 4, vaccinated, neutered (Orange Tabby)

Please enter the information for the new cat:
Cat name?         Ralph
Age, in years?    4
Vaccinated (Y/N)? y
Neutered (Y/N)?   y
Breed?            Angora


List of cats with new addition:
===============================
Cat: Minou, age: 3, vaccinated, neutered (stray)
Cat: Max, age: 1, vaccinated, not neutered (Burmese)
Cat: Gizmo, age: 2, not vaccinated, not neutered (Bengal)
Cat: Garfield, age: 4, vaccinated, neutered (Orange Tabby)
Cat: Ralph, age: 4, vaccinated, neutered (Angora)


Vaccinated and neutered cats:
=============================
Cat: Minou, age: 3, vaccinated, neutered (stray)
Cat: Garfield, age: 4, vaccinated, neutered (Orange Tabby)
Cat: Ralph, age: 4, vaccinated, neutered (Angora)


Question

Recreate the original Python program with its Cat class. Make sure that it will generate the same output shown above if fed the same cat information.


Solution






Part 2: Secure Shell, Login in to Beowulf2


Login/logout

To login refers to the action of connecting to the computer as a user. To connect to Beowulf, the computer used for Computer Science classes, one must have an account and a password. Logout is the term used when you are finished with the computer and want to terminate your session. You should have a Unix account form at this point, and have found on it your account name (of the form 111c-xx, where xx are two letters, different for everybody), and your password. Using the Windows PC in front of you, find the SecureShell program/icon on the desktop (or via the Start/Program menu) and click on it.

If the Quick Connect window is not greeting you, click on the Quick Connect icon to get it:

Enter beowulf.csc as the host name, 111c-xx as your user name (change the xx to reflect your 2-letter Id) Click on the Connect button Enter your password. and click "Connect".

If everything went well, you should see this:

SSH Secure Shell 3.1.0 (Build 235)
Copyright (c) 2000-2001 SSH Communications Security Corp - http://www.ssh.com/

This copy of SSH Secure Shell is a non-commercial version.
This version does not include PKI and PKCS #11 functionality.

[111c-xx@beowulf ~]$ 

Keep your account and password information in a safe place, or learn both by heart! You will need them every time you need to use your account and work on your homework assignment or lab.

You may want to log out now, to practice this login procedure a couple times. If you are working with a lab partner, it is a good time to log out and let her/him try the procedure.

To logout of the session with beowulf, simply type exit at the Linux prompt:

 [111c-xx@beowulf ~]$  exit

Don't hesitate to call on the TA or your instructor if you need help.




Part 3: Playing with emacs


In order for you not to suffer from a misbehaving backspace/delete key as happened in class yesterday, you should execute the following command (your input is in bold face)

  [111c-xx@beowulf ~]$  getcopy .emacs
  cp: overwrite `./.emacs'? y

(That is "getcopy" space period "emacs") It's a configuration file that will allow emacs to understand the backspace key correctly. You only need to do this once and you will be set for the semester.

You will now get a copy of a file from your instructor's account, and edit it. The file is called Fulghum and is a short story from Robert Fulghum's book All I really need to know I learned in Kindergarten.

To get a copy of this file, type


  [111c-xx@beowulf ~]$  getcopy fulghum


followed by the return key. Then edit the file with the command


  [111c-xx@beowulf ~]$  emacs fulghum


Simply work on the first paragraph of the file, and try to straighten it up. I have indicated below, on the right-hand side, which emacs commands you can use to fix the text on your screen.

Fulghum emacs.jpg

When you are done with the modification, or if you want to stop for right now and save your modifications, type C-x C-c, and press y when emacs asks if you want to save the file. Below is a summary of some useful emacs commands.

You should find some time this week to go to a lab and read the Emacs at Smith handout while in front of the terminal, on your own. This is the best way to test and learn the different features of the editor!

DEL/Shift Backspace Delete previous character
c-d Delete character at cursor
Esc-d Delete word
c-k Kill line
c-a Go to beginning of line
c-e Go to end of line
c-f Move cursor 1 character forward
c-b Move cursor 1 character backward
c-p Move to previous line
c-n Move to next line
c-x c-c quit emacs and save file
c-x c-s save file but remain in emacs
c-g GE OUT OF TROUBLE!

Part I of the homework assignment for this week is to edit the complete story about Larry Walters and his flight over Los Angeles.