Difference between revisions of "CSC111 studentClass Programs"
(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 | ||
""" | """ | ||
− | |||
#-------------------------------------------------------- | #-------------------------------------------------------- | ||
− | # | + | # Student class |
#-------------------------------------------------------- | #-------------------------------------------------------- | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
class Student: | class Student: | ||
Line 171: | Line 172: | ||
self.email = e | self.email = e | ||
− | def | + | 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: | ||
#-------------------------------------------------------- | #-------------------------------------------------------- | ||
− | # | + | # 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 | |
− | + | """ | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
#-------------------------------------------------------- | #-------------------------------------------------------- | ||
Line 245: | Line 232: | ||
#--- display the list --- | #--- display the list --- | ||
for stu in List: | for stu in List: | ||
− | stu. | + | 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. | + | stu.display() |
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
Line 269: | Line 248: | ||
</source> | </source> | ||
<br /> | <br /> | ||
− | ==Version | + | ==Version 4== |
<br /> | <br /> | ||
Line 292: | Line 271: | ||
- change the email address | - 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: | class Student: | ||
Line 305: | Line 299: | ||
self.email = e | self.email = e | ||
− | def | + | 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: | ||
#-------------------------------------------------------- | #-------------------------------------------------------- | ||
− | # | + | # 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 | ||
+ | |||
+ | |||
#-------------------------------------------------------- | #-------------------------------------------------------- | ||
Line 365: | Line 373: | ||
#--- display the list --- | #--- display the list --- | ||
for stu in List: | for stu in List: | ||
− | stu. | + | 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. | + | 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 | + | ==Version 5== |
<br /> | <br /> | ||
Line 410: | Line 426: | ||
#-------------------------------------------------------- | #-------------------------------------------------------- | ||
text=""" | text=""" | ||
− | 111c-ca, Liz Cloud, xxyzzyK, | + | 111c-ca, Liz Cloud, xxyzzyK, dthiebau@email.smith.edu |
− | 111c-cb, Fiona Poory, ahlhla, | + | 111c-cb, Fiona Poory, ahlhla, thiebaut@cs.smith.edu |
− | 111c-ck, Alex Doe, aluoiid, | + | 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() | ||
− | |||
− | |||
#--- 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() ) |
Line 530: | Line 546: | ||
</source> | </source> | ||
<br /> | <br /> | ||
− | + | </onlydft> | |
− | + | <br /> | |
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
+ | <br /> | ||
<br /> | <br /> | ||
− | |||
− | |||
− | |||
<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()