Dumb*ss newbie Q

C

Captain Dondo

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, t="", a="", d=""):
#
# 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
 
B

beliavsky

A subject line should say what the message is about, for example

"Create HTML tag using objects (newbie Q)"

and enable people who are not interested in or knowledgable about a
topic to skip it, while grabbing the attention of people who are
knowledgable/interested.
 
L

Larry Bates

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
 
C

Captain Dondo

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.

Thanks, it does. I am trying to write a much simplified album -
<http://marginalhacks.com/Hacks/album/> . The author has taken it in a
direction that is no longer useful to me and I am desparately incompetent
in perl.... Python and I like each other, except that I don't have much
experience writing it from scratch....

For now I am using this tutorial as a go-by:
<http://www.linuxgazette.com/issue19/python.html>, but I will check out
the link you provided.

--Yan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top