Difference between revisions of "CSC111 studentClass Programs"

From dftwiki3
Jump to: navigation, search
(Version 4)
Line 537: Line 537:
 
</source>
 
</source>
 
<br />
 
<br />
 +
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
[[Category:CSC111]][[Category:Python]]

Revision as of 06:55, 7 April 2010

--D. Thiebaut 11:51, 7 April 2010 (UTC)


Version 1


""" 
studentClass.py
D. Thiebaut

A program for the Exercises on Class/Objects
Creates a class to keep information about a student.
The class keeps track of the student's name, account number, 
password, and email address.

Object issued from this class have methods to
- print the student information (everything we know about a student)
- create a new student with all the information
- get the name of a student
- get the account number
- get the password
- get the email
- change the email address
"""

text="""
111c-ca, Liz Cloud, xxyzzyK, lcloud@email.smith.edu
111c-cb, Fiona Poory, ahlhla, fpoory@email.smith.edu
111c-ck, Alex Doe, aluoiid, adoe@gmail.com
"""

def parseInput( text ):
    lines = text.split( "\n" )
    List = []
    for line in lines:
        words = line.split( ',' )
        if len( words )==4:
            (account, name, passw, email) = words
        List.append( (account, name, passw, email) )

def main():
    List = parseInput( text )
    
main()


Version 2


""" 
studentClass.py
D. Thiebaut

A program for the Exercises on Class/Objects
Creates a class to keep information about a student.
The class keeps track of the student's name, account number, 
password, and email address.

Object issued from this class have methods to
- print the student information (everything we know about a student)
- create a new student with all the information
- get the name of a student
- get the account number
- get the password
- get the email
- change the email address
"""

text="""
111c-ca, Liz Cloud, xxyzzyK, lcloud@email.smith.edu
111c-cb, Fiona Poory, ahlhla, fpoory@email.smith.edu
111c-ck, Alex Doe, aluoiid, adoe@gmail.com
"""

class Student:
    def __init__( self, a, n, p, e ):
        self.account = a
        self.name    = n
        self.passw   = p
        self.email   = e

    def display( self ):
        print "account: %s name: %s \tpassw: %s\temail: %s" \
            % (self.account, self.name, self.passw, self.email)

    def getName( self ):
        return self.name

    def getAccount( self ):
        return self.account

    def getPassword( self ):
        return self.passw

    def getEmail( self ):
        return self.email

    def setEmail( self, newEmail ):
        self.email = newEmail 


def parseInput( text ):
    lines = text.split( "\n" )
    List = []
    for line in lines:
        words = line.split( ',' )
        if len( words )==4:
            (account, name, passw, email) = words
            List.append( Student( account, name, passw, email ) )
    return List

def main():
    List = parseInput( text )
    for stu in List:
        stu.display()
    
main()


Version 3


""" 
studentClass.py
D. Thiebaut

A program for the Exercises on Class/Objects
Creates a class to keep information about a student.
The class keeps track of the student's name, account number, 
password, and email address.

Object issued from this class have methods to
- print the student information (everything we know about a student)
- create a new student with all the information
- get the name of a student
- get the account number
- get the password
- get the email
- change the email address
"""

#--------------------------------------------------------
# Student class
#--------------------------------------------------------
class Student:
    
    def __init__( self, a, n, p, e ):
        """ constructor: takes account, name, password, and email """
        self.account = a
        self.name    = n
        self.passw   = p
        self.email   = e

    def display( self ):
        """ display all info about student """
        print "account: %s name: %s \tpassw: %s\temail: %s" \
            % (self.account, self.name, self.passw, self.email)

    def getName( self ):
        """ return name """
        return self.name

    def getAccount( self ):
        """ return account """
        return self.account

    def getPassword( self ):
        """ return password """
        return self.passw

    def getEmail( self ):
        """ return email """
        return self.email

    def setEmail( self, newEmail ):
        """ return email """
        self.email = newEmail 

#--------------------------------------------------------
# original list of students 
#--------------------------------------------------------
text="""
111c-ca, Liz Cloud, xxyzzyK, lcloud@email.smith.edu
111c-cb, Fiona Poory, ahlhla, fpoory@email.smith.edu
111c-ck, Alex Doe, aluoiid, adoe@gmail.com
"""

#--------------------------------------------------------
# parseInput
#--------------------------------------------------------
def parseInput( text ):
    """ parses the original text and creates a list of student objects"""
    lines = text.split( "\n" )
    List = []
    for line in lines:
        words = line.split( ',' )
        if len( words )==4:
            (account, name, passw, email) = words
            List.append( Student( account, name, passw, email ) )
    return List

#--------------------------------------------------------
# main
#--------------------------------------------------------
def main():
    """ creates a list of students, displays the list, changes the email
    address to reflect new change """

    #--- create a list of students ---
    List = parseInput( text )

    #--- display the list ---
    for stu in List:
        stu.display()

    #--- change all email addresses ---
    for stu in List:
        email = stu.getEmail().replace( "email.smith.edu", "smithcollege.edu" )
        stu.setEmail( email )

    #--- show the list again ---
    print "\n\nNew List"
    for stu in List:
        stu.display()
    

main()


Version 4


""" 
studentClass.py
D. Thiebaut

A program for the Exercises on Class/Objects
Creates a class to keep information about a student.
The class keeps track of the student's name, account number, 
password, and email address.

Object issued from this class have methods to
- print the student information (everything we know about a student)
- create a new student with all the information
- get the name of a student
- get the account number
- get the password
- get the email
- change the email address
"""
import os

#--------------------------------------------------------
# original list of students 
#--------------------------------------------------------
text="""
111c-ca, Liz Cloud, xxyzzyK, lcloud@email.smith.edu
111c-cb, Fiona Poory, ahlhla, fpoory@email.smith.edu
111c-ck, Alex Doe, aluoiid, adoe@gmail.com
"""

MAIL = "/usr/sbin/sendmail"    # the program that sends mail on beowulf

MESSAGE = """To: %s
From: %s
Subject: %s

%s
"""  
class Student:
    
    def __init__( self, a, n, p, e ):
        """ constructor: takes account, name, password, and email """
        self.account = a
        self.name    = n
        self.passw   = p
        self.email   = e

    def printAll( self ):
        """ display all info about student """
        print "account: %s name: %s \tpassw: %s\temail: %s" \
            % (self.account, self.name, self.passw, self.email)

    def getName( self ):
        """ return name """
        return self.name

    def getAccount( self ):
        """ return account """
        return self.account

    def getPassword( self ):
        """ return password """
        return self.passw

    def getEmail( self ):
        """ return email """
        return self.email

    def setEmail( self, newEmail ):
        """ return email """
        self.email = newEmail 

#--------------------------------------------------------
# sendmail
#--------------------------------------------------------
def sendmail( recipient, sender, subject, body ):
    """sends the body string to the recipient, specifying the sender and
    the subject of the message.  The program specified in MAIL is used
    as the mailer program"""


    #--- create message ---
    msg = MESSAGE % ( recipient, sender, subject, body )

    #--- open a pipe to the mailer and send it the text of the message ---
    p = os.popen("%s -t" % MAIL, 'w')
    p.write( msg ) 
    exitcode = p.close()

    #--- if the mailer closed with an error, report it ---
    if exitcode:
        print "Exit code: %s" % exitcode



#--------------------------------------------------------
# parseInput
#--------------------------------------------------------
def parseInput( text ):
    """ parses the original text and creates a list of student objects"""
    lines = text.split( "\n" )
    List = []
    for line in lines:
        words = line.split( ',' )
        if len( words )==4:
            (account, name, passw, email) = words
            List.append( Student( account, name, passw, email ) )
    return List

#--------------------------------------------------------
# main
#--------------------------------------------------------
def main():
    """ creates a list of students, displays the list, changes the email
    address to reflect new change """

    #--- create a list of students ---
    List = parseInput( text )

    #--- display the list ---
    for stu in List:
        stu.printAll()

    #--- change all email addresses ---
    for stu in List:
        email = stu.getEmail().replace( "email.smith.edu", "smithcollege.edu" )
        stu.setEmail( email )

    #--- show the list again ---
    print "\n\nNew List"
    for stu in List:
        stu.printAll()

    return

    #--- send mail to all students ---
    for stu in List:
        sendmail( stu.getEmail(), "thiebaut@cs.smith.edu", 
                  "No homework assignment due", 
                  "dear %s\nThe current hw is lifted" % stu.getName() )
    

main()


Version 5


""" 
studentClass.py
D. Thiebaut

A program for the Exercises on Class/Objects
Creates a class to keep information about a student.
The class keeps track of the student's name, account number, 
password, and email address.

Object issued from this class have methods to
- print the student information (everything we know about a student)
- create a new student with all the information
- get the name of a student
- get the account number
- get the password
- get the email
- change the email address
"""
import os

#--------------------------------------------------------
# original list of students 
#--------------------------------------------------------
text="""
111c-ca, Liz Cloud, xxyzzyK, dthiebau@email.smith.edu
111c-cb, Fiona Poory, ahlhla, thiebaut@cs.smith.edu
111c-ck, Alex Doe, aluoiid, dominique.t@gmail.com
"""

MAIL = "/usr/sbin/sendmail"    # the program that sends mail on beowulf

MESSAGE = """To: %s
From: %s
Subject: %s

%s
"""  
class Student:
    
    def __init__( self, a, n, p, e ):
        """ constructor: takes account, name, password, and email """
        self.account = a
        self.name    = n
        self.passw   = p
        self.email   = e

    def printAll( self ):
        """ display all info about student """
        print "account: %s name: %s \tpassw: %s\temail: %s" \
            % (self.account, self.name, self.passw, self.email)

    def getName( self ):
        """ return name """
        return self.name

    def getAccount( self ):
        """ return account """
        return self.account

    def getPassword( self ):
        """ return password """
        return self.passw

    def getEmail( self ):
        """ return email """
        return self.email

    def setEmail( self, newEmail ):
        """ return email """
        self.email = newEmail 

#--------------------------------------------------------
# sendmail
#--------------------------------------------------------
def sendmail( recipient, sender, subject, body ):
    """sends the body string to the recipient, specifying the sender and
    the subject of the message.  The program specified in MAIL is used
    as the mailer program"""


    #--- create message ---
    msg = MESSAGE % ( recipient, sender, subject, body )

    #--- open a pipe to the mailer and send it the text of the message ---
    p = os.popen("%s -t" % MAIL, 'w')
    p.write( msg ) 
    exitcode = p.close()

    #--- if the mailer closed with an error, report it ---
    if exitcode:
        print "Exit code: %s" % exitcode



#--------------------------------------------------------
# parseInput
#--------------------------------------------------------
def parseInput( text ):
    """ parses the original text and creates a list of student objects"""
    lines = text.split( "\n" )
    List = []
    for line in lines:
        words = line.split( ',' )
        if len( words )==4:
            (account, name, passw, email) = words
            List.append( Student( account, name, passw, email ) )
    return List

#--------------------------------------------------------
# main
#--------------------------------------------------------
def main():
    """ creates a list of students, displays the list, changes the email
    address to reflect new change """

    #--- create a list of students ---
    List = parseInput( text )

    #--- display the list ---
    for stu in List:
        stu.printAll()

    #--- change all email addresses ---
    """
    for stu in List:
        email = stu.getEmail().replace( "email.smith.edu", "smithcollege.edu" )
        stu.setEmail( email )
    """

    #--- show the list again ---
    print "\n\nNew List"
    for stu in List:
        stu.printAll()

    #--- send mail to all students ---
    for stu in List:
        sendmail( stu.getEmail(), "thiebaut@cs.smith.edu", 
                  "No homework assignment due", 
                  "Dear %s\nThe current hw is lifted" % stu.getName() )
    

main()