Dumb*ss newbie Q

Larry Bates lbates at syscononline.com
Mon Mar 28 09:42:22 EST 2005


Others have answered your specific question, I thought I
would add some suggestions (not tested):

1) You don't need a separate set_title method.  You can
change the title attribute at any time by just saying
m.title="new title".  No method is required unless you
need to do some pre/post processing.

m.title="anything you want"

2) To get class to prepare its output, just insert a
__str__ method like following:

    def __str__(self):
    return '<a href="avi://%s/%s/%s"> <img src="%s>%s/tn/%s.jpg</a>' % \
           (self.audience, self.title,
            self.driver, self.audience,
            self.title, self.title)


Then you can eliminate the html method and thumb method and just write:

print m

3) Add extend keyword arguments for url and audience to the __init__
method. That way you (or other users) will know that each keyword
means when they run in Idle or other interpreter that expands
keyword arguments on the screen as you are typing):

    m=Movie(title="Fate_is_the_Hunter", audience="kids", \
      driver="X=hermes.seiner.lan:xv,athena.seiner.lan:xmga," \
             "default:x11;console=vesa")
    print m


If you plan on doing a lot of this you may want to take a look
at the htmlgen module at:

http://starship.python.net/crew/friedrich/HTMLgen/html/main.html

(I actually stole the idea of using the the __str__ method to
generate the output from this module).

Hope information helps.

Larry Bates

Captain Dondo wrote:
> OK, I know this is covered somewhere in Python 101, but for the life of me
> I cannot figure this out.  I really need a basic intro to Python book....
> 
> I am trying to do something very simple - create an HTML tag using objects:
> 
> class Movie:
> 
>     def __init__ (self, title="", audience="", driver=""):
> 	#
> 	# Create an instance of Movie
> 	#
> 	self.title = t
> 	self.audience = a
> 	self.driver = d
> 
>     def set_title (self, new_title):
> 	self.title = new_title
> 
>     def set_audience (self, audience):
> 	#
> 	# Only 3 valid values: kids, parents, guests
> 	#
> 	self.audience = audience	    
> 
>     def set_driver (self, new_driver):
> 	self.driver = new_driver
> 
>     def url (self):
> 	self.url = "avi://" + self.audience + "/" + self.title + "/" + self.driver
> 
>     def thumb (self):
> 	self.thumb = self.audience + "/tn/" + self.title + ".jpg"
> 
>     def html (self):
> 	print "<a href=avi://" + self.audience + "/" + self.title + "/" +
> 	self.driver + "> <img src=" + self.thumb + ">" + self.title + "</a>"
> 
> #
> # Code to test this class
> #
> if __name__ == '__main__':
>     print "**** Test 1 ****"
>     m=Movie("Fate_is_the_Hunter")
>     m.set_audience ("kids")
>     m.set_title ("Fate_is_the_Hunter")
>     m.set_driver ("X=hermes.seiner.lan:xv,athena.seiner.lan:xmga,default:x11;console=vesa")
>     m.html ()
>     print "*** Finish ***"
> 
> The problem is that m.html in the test section fails with 
> 
> TypeError: cannot concatenate 'str' and 'instancemethod' objects
> 
> I got it working once.  The output should be something like this:
> 
> <a
> href=avi://kids/Fate_is_the_Hunter/X=hermes.seiner.lan:xv,athena.seiner.lan:xmga,default:x11;console=vesa>
> <img src=kids/tn/Fate_is_the_Hunter.jpg>Fate_is_the_Hunter</a>
> 
> but then I made some minor edits and I can't get it to work again....
> 
> Where do I find documentation on Python classes?  Or how do I convert one
> to the other?  Or, how do I get the above to work?  This is the first time
> I've really tried to work with a class I've defined myself and I obviously
> don't know what I am doing....
> 
> On a minor note, if you look at the audience method, I want to limit it to
> 3 values.  How do I do that?
> 
> TIA....
> 
> --Yan
> 



More information about the Python-list mailing list