tags:

views:

88

answers:

5

Hi everyone,

I am having little problem with importing classes in python. My work flow goes like this

index.py

    class Template:

        def header():
        def body():
        def form():
        def footer():

display.py

I want to call function header(), body() and footer () in my display.py page. Will anyone make me clear about this issue in python. Thanks for your concern.

Index file--- [Index.py][1]

[1]: http://pastebin.com/qNB53KTE and display.py -- "http://pastebin.com/vRsJumzq"

+1  A: 

I am not sure if I understand you correctly, but I believe you are asking how to import the template class in another script. The import statement is what you need:

from index import template

foo = template()

foo.header()
foo.body()
foo.footer()
Jørn Schou-Rode
there's a couple of typos here, that render your answer completely nonsensical.
SilentGhost
@Sil: The first revision of my answer was indeed quite buggy. If it was not for the annoying "are you human" feature, the typos would have been fixed before your comment :)
Jørn Schou-Rode
+4  A: 

What have you tried? The following would be normal way of using methods of Template class after import.

from index import Template

t = Template()
t.header()
t.body()
t.footer()

ETA: at the end of your index.py file (lines 99-105) you're calling all the functions from the above-defined Template class. That's why you're seeing duplicates.

SilentGhost
i have tried the same like u did. but i always get repetition like 2 times body() etc
@user: you're probably calling methods from the `Template`'s `__init__`. Otherwise, there is nothing in this code that would produce duplicate.
SilentGhost
I have given the link for my files thanks
@user: see my edit
SilentGhost
+1  A: 

Edit: Okay, I see what your problem is, given your code.

You're calling the following:

## Calling all the functions of the class template with object (objx)
objx=HtmlTemplate()
objx.Header()
objx.Body()
objx.Form()
objx.Footer()
objx.CloseHtml()

And then in your display.py:

t = HtmlTemplate()
t.Header()
t.Body()

See how Body() gets called twice?

As a footnote, you should use lowercase for method names, and Capital words for classes as you're doing now. It's a good convention. I greatly recommend it.

You should simply construct the object once in display.py and call all the methods.

Xavier Ho
yeah every function has self parameter which im aware of
What other problems do you have? Can you provide us with more code?
Xavier Ho
Like my workflow above, i have index.py file [with given class and fucntions] and from index.py file i want to pass values from form() to display.py. In display.py i need to call only header(), body() and footer(). But i am doing same things like u guys said, but got repetition in the display page. Display page goes like this- header(), body(), form(), footer() and again body() and some other cotent from display page.
@user: we need to see the actual code of your template class and its methods. We're not psychics.
SilentGhost
@user343934: If you call `body()` twice, then surely you expect the output of `body()` twice...
Johnsyweb
@user343934: Umm, you called `body()` again?
Xavier Ho
no i haven't define body in display.py file
okei i have provided file source code in above question
@user343934: Thanks for the pastebin. See how easy that was? `:]`
Xavier Ho
I mean in the display.py i can find body, form repetitions
Here you can see how it got repeated-- http://i45.tinypic.com/1hf9xu.jpg
@user343934: Because everything gets "run" when you import it. See my edited answer.
Xavier Ho
+1  A: 

You have the following code at the top and the bottom of index.py:

cgitb.enable()
print 'Content-type: text/html\n\n'
print "<link rel=\"stylesheet\" type=\"text/css\" href=\"css/msa.css\" >"

# [...]

## Calling all the functions of the class template with object (objx)
objx=HtmlTemplate()
# [...]      
objx.CloseHtml()

This will be called each time you import index.

To prevent this happening, put it in a block thus:

if __name__ == '__main__':
    cgitb.enable()
    print 'Content-type: text/html\n\n'
    print "<link rel=\"stylesheet\" type=\"text/css\" href=\"css/msa.css\" >"

    # [...]

    ## Calling all the functions of the class template with object (objx)
    objx=HtmlTemplate()
    # [...]      
    objx.CloseHtml()

...or better still put this code functions that can be called from elsewhere.

Johnsyweb
Okei i have provided file source code above
New answer provided!
Johnsyweb
It seems working but still with one error that in display.py i receive "Content-type: text/html " on the top
Yes... that code is pulled in with the module too. I've included that in my revised answer.
Johnsyweb
@user343934: Yeah, because you should have put the content type also into a HTML-proper syntax.
Xavier Ho
Thanks to everyone :)
Yep, yay thanks John :)
You're welcome, @user343934. Don't forget to mark the answer that helped you as accepted.
Johnsyweb
A: 

At the bottom of your index file you create a HtmlTemplate object and call all the methods on it. Since this code is not contained in any other block, it gets executed when you import the module. You either need to remove it or check to see if the file is being run from the command line.

if __name__ == "__main__":
    objx=HtmlTemplate()
    objx.Header()
    objx.Body()
    objx.Form()
    objx.Footer()
    objx.CloseHtml()
unholysampler