Difference between revisions of "CSC111 studentClass Programs"

From dftwiki3
Jump to: navigation, search
(Created page with '--~~~~ ---- ==Version 1== <br /> <source lang="python"> """ studentClass.py D. Thiebaut A program for the Exercises on Class/Objects Creates a class to keep information abou…')
 
 
(5 intermediate revisions by the same user not shown)
Line 2: Line 2:
 
----
 
----
  
 +
<bluebox>
 +
<br />
 +
A collection of programs illustrating the use of classes and objects...
 +
<br />
 +
</bluebox>
 +
<br />
 +
__TOC__
 +
<br />
 
==Version 1==
 
==Version 1==
  
Line 49: Line 57:
 
<br />
 
<br />
  
 +
<onlydft>
 
==Version 2==
 
==Version 2==
  
Line 124: Line 133:
  
  
 +
 +
</source>
 +
<br />
 +
==Version 3==
 +
 +
<br />
 +
<source lang="python">
  
 
"""  
 
"""  
Line 143: Line 159:
 
- change the email address
 
- change the email address
 
"""
 
"""
import os
 
  
 
#--------------------------------------------------------
 
#--------------------------------------------------------
# original list of students
+
# Student class
 
#--------------------------------------------------------
 
#--------------------------------------------------------
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:
 
class Student:
 
      
 
      
Line 171: Line 172:
 
         self.email  = e
 
         self.email  = e
  
     def printAll( self ):
+
     def display( self ):
 
         """ display all info about student """
 
         """ display all info about student """
 
         print "account: %s name: %s \tpassw: %s\temail: %s" \
 
         print "account: %s name: %s \tpassw: %s\temail: %s" \
Line 197: Line 198:
  
 
#--------------------------------------------------------
 
#--------------------------------------------------------
# sendmail
+
# original list of students
 
#--------------------------------------------------------
 
#--------------------------------------------------------
def sendmail( recipient, sender, subject, body ):
+
text="""
    """sends the body string to the recipient, specifying the sender and
+
111c-ca, Liz Cloud, xxyzzyK, lcloud@email.smith.edu
    the subject of the message.  The program specified in MAIL is used
+
111c-cb, Fiona Poory, ahlhla, fpoory@email.smith.edu
    as the mailer program"""
+
111c-ck, Alex Doe, aluoiid, adoe@gmail.com
 
+
"""
 
 
    #--- 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
 
 
 
 
 
  
 
#--------------------------------------------------------
 
#--------------------------------------------------------
Line 245: Line 232:
 
     #--- display the list ---
 
     #--- display the list ---
 
     for stu in List:
 
     for stu in List:
         stu.printAll()
+
         stu.display()
  
 
     #--- change all email addresses ---
 
     #--- change all email addresses ---
    """
 
 
     for stu in List:
 
     for stu in List:
 
         email = stu.getEmail().replace( "email.smith.edu", "smithcollege.edu" )
 
         email = stu.getEmail().replace( "email.smith.edu", "smithcollege.edu" )
 
         stu.setEmail( email )
 
         stu.setEmail( email )
    """
 
  
 
     #--- show the list again ---
 
     #--- show the list again ---
 
     print "\n\nNew List"
 
     print "\n\nNew List"
 
     for stu in List:
 
     for stu in List:
         stu.printAll()
+
         stu.display()
 
 
    #--- 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() )
 
 
      
 
      
  
Line 269: Line 248:
 
</source>
 
</source>
 
<br />
 
<br />
==Version 3==
+
==Version 4==
  
 
<br />
 
<br />
Line 292: Line 271:
 
- change the email address
 
- change the email address
 
"""
 
"""
 +
import os
  
 
#--------------------------------------------------------
 
#--------------------------------------------------------
# Student class
+
# 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:
 
class Student:
 
      
 
      
Line 305: Line 299:
 
         self.email  = e
 
         self.email  = e
  
     def display( self ):
+
     def printAll( self ):
 
         """ display all info about student """
 
         """ display all info about student """
 
         print "account: %s name: %s \tpassw: %s\temail: %s" \
 
         print "account: %s name: %s \tpassw: %s\temail: %s" \
Line 331: Line 325:
  
 
#--------------------------------------------------------
 
#--------------------------------------------------------
# original list of students
+
# sendmail
 
#--------------------------------------------------------
 
#--------------------------------------------------------
text="""
+
def sendmail( recipient, sender, subject, body ):
111c-ca, Liz Cloud, xxyzzyK, lcloud@email.smith.edu
+
    """sends the body string to the recipient, specifying the sender and
111c-cb, Fiona Poory, ahlhla, fpoory@email.smith.edu
+
    the subject of the message.  The program specified in MAIL is used
111c-ck, Alex Doe, aluoiid, adoe@gmail.com
+
    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
 +
 
 +
 
  
 
#--------------------------------------------------------
 
#--------------------------------------------------------
Line 365: Line 373:
 
     #--- display the list ---
 
     #--- display the list ---
 
     for stu in List:
 
     for stu in List:
         stu.display()
+
         stu.printAll()
  
 
     #--- change all email addresses ---
 
     #--- change all email addresses ---
Line 375: Line 383:
 
     print "\n\nNew List"
 
     print "\n\nNew List"
 
     for stu in List:
 
     for stu in List:
         stu.display()
+
         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() )
 
      
 
      
  
Line 381: Line 397:
 
</source>
 
</source>
 
<br />
 
<br />
==Version 1==
+
==Version 5==
  
 
<br />
 
<br />
Line 410: Line 426:
 
#--------------------------------------------------------
 
#--------------------------------------------------------
 
text="""
 
text="""
111c-ca, Liz Cloud, xxyzzyK, lcloud@email.smith.edu
+
111c-ca, Liz Cloud, xxyzzyK, dthiebau@email.smith.edu
111c-cb, Fiona Poory, ahlhla, fpoory@email.smith.edu
+
111c-cb, Fiona Poory, ahlhla, thiebaut@cs.smith.edu
111c-ck, Alex Doe, aluoiid, adoe@gmail.com
+
111c-ck, Alex Doe, aluoiid, dominique.t@gmail.com
 
"""
 
"""
  
Line 509: Line 525:
  
 
     #--- change all email addresses ---
 
     #--- change all email addresses ---
 +
    """
 
     for stu in List:
 
     for stu in List:
 
         email = stu.getEmail().replace( "email.smith.edu", "smithcollege.edu" )
 
         email = stu.getEmail().replace( "email.smith.edu", "smithcollege.edu" )
 
         stu.setEmail( email )
 
         stu.setEmail( email )
 +
    """
  
 
     #--- show the list again ---
 
     #--- show the list again ---
Line 517: Line 535:
 
     for stu in List:
 
     for stu in List:
 
         stu.printAll()
 
         stu.printAll()
 
    return
 
  
 
     #--- send mail to all students ---
 
     #--- send mail to all students ---
Line 524: Line 540:
 
         sendmail( stu.getEmail(), "thiebaut@cs.smith.edu",  
 
         sendmail( stu.getEmail(), "thiebaut@cs.smith.edu",  
 
                   "No homework assignment due",  
 
                   "No homework assignment due",  
                   "dear %s\nThe current hw is lifted" % stu.getName() )
+
                   "Dear %s\nThe current hw is lifted" % stu.getName() )
 
      
 
      
  
Line 530: Line 546:
 
</source>
 
</source>
 
<br />
 
<br />
==Version 1==
+
</onlydft>
 
+
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 +
<br />
 
<br />
 
<br />
<source lang="python">
 
 
</source>
 
 
<br />
 
<br />
 +
[[Category:CSC111]][[Category:Python]]

Latest revision as of 04:23, 8 April 2010

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



A collection of programs illustrating the use of classes and objects...


Contents


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()



...