Difference between revisions of "ClassExample2.py"
(New page: --~~~~ <onlydft> <source lang="python"> #! /usr/bin/python # D. Thiebaut # Version 2, based on classExample.py # Solution 1 to exercise: # create a 3rd object that is the concatenation of...) |
|||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
--[[User:Thiebaut|D. Thiebaut]] 02:40, 26 January 2010 (UTC) | --[[User:Thiebaut|D. Thiebaut]] 02:40, 26 January 2010 (UTC) | ||
− | + | ;Exercise | |
+ | :Create a new version of [[ClassExample1.py]] such that we create a 3rd document which is the concatenation of the other two. It is not acceptable to create docuClass( text1 + text2 )! (although it would be a good solution otherwise) | ||
+ | |||
<source lang="python"> | <source lang="python"> | ||
#! /usr/bin/python | #! /usr/bin/python | ||
Line 56: | Line 58: | ||
</source> | </source> | ||
− | + | [[Category:CSC352]][[Category:Python]] |
Latest revision as of 13:22, 4 February 2010
--D. Thiebaut 02:40, 26 January 2010 (UTC)
- Exercise
- Create a new version of ClassExample1.py such that we create a 3rd document which is the concatenation of the other two. It is not acceptable to create docuClass( text1 + text2 )! (although it would be a good solution otherwise)
#! /usr/bin/python
# D. Thiebaut
# Version 2, based on classExample.py
# Solution 1 to exercise:
# create a 3rd object that is the concatenation of the first two.
#
class docuClass:
def __init__( self, doc ):
self.doc = doc
self.lines = doc.split( "\n" )
self.wordCount = len( doc.split() )
def getWordCount( self ):
return self.wordCount
def getLines( self ):
return self.lines
def display( self ):
for line in self.lines:
print line
def main():
doc1 = docuClass( """`Have some wine,' the March Hare said in an encouraging tone.
Alice looked all round the table, but there was nothing on it but tea.
`I don't see any wine,' she remarked.
`There isn't any,' said the March Hare.
`Then it wasn't very civil of you to offer it,' said Alice angrily.
`It wasn't very civil of you to sit down without being invited,'
said the March Hare. """ )
doc2 = docuClass( """Piglet sidled up to Pooh from behind. "Pooh," he whispered.
"Yes, Piglet?"
"Nothing," said Piglet, taking Pooh's paw,
"I just wanted to be sure of you."
""" )
print "doc1"
doc1.display()
print "doc2"
doc2.display()
doc3 = docuClass( '\n'.join( doc1.getLines()+doc2.getLines() ) )
print "doc3"
doc3.display()
main()