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…') |
|||
Line 124: | Line 124: | ||
+ | |||
+ | </source> | ||
+ | <br /> | ||
+ | ==Version 3== | ||
+ | |||
+ | <br /> | ||
+ | <source lang="python"> | ||
""" | """ | ||
Line 143: | Line 150: | ||
- change the email address | - change the email address | ||
""" | """ | ||
− | |||
#-------------------------------------------------------- | #-------------------------------------------------------- | ||
− | # | + | # Student class |
#-------------------------------------------------------- | #-------------------------------------------------------- | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
class Student: | class Student: | ||
Line 171: | Line 163: | ||
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 189: | ||
#-------------------------------------------------------- | #-------------------------------------------------------- | ||
− | # | + | # 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 223: | ||
#--- 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 239: | ||
</source> | </source> | ||
<br /> | <br /> | ||
− | ==Version | + | ==Version 1== |
<br /> | <br /> | ||
Line 292: | Line 262: | ||
- 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 290: | ||
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 316: | ||
#-------------------------------------------------------- | #-------------------------------------------------------- | ||
− | # | + | # 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 364: | ||
#--- 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 374: | ||
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 388: | ||
</source> | </source> | ||
<br /> | <br /> | ||
− | ==Version | + | ==Version 4== |
<br /> | <br /> | ||
Line 410: | Line 417: | ||
#-------------------------------------------------------- | #-------------------------------------------------------- | ||
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 516: | ||
#--- 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 526: | ||
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 531: | ||
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() ) |
main() | main() | ||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
− | |||
</source> | </source> | ||
<br /> | <br /> |
Revision as of 06:53, 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 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
"""
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 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, 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()