By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,213 Members | 1,086 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,213 IT Pros & Developers. It's quick & easy.

Running script in __main__ shows no output in IDLE

P: n/a
I just downloaded the most recent (2.4.3) Python and IDLE (1.1.3) to
Windows XP Professional. I'm new to the IDLE environment, so hopefully
someone can tell me what I'm missing here! Below is the code, which I'm
editing within IDLE, and attempting to test with the Run commands.

I was expecting to see at least see the prints to stdout from the if
__name__ ... statement and main() function, but when I use F5 Run
module I don't see any output or errors. Any suggestions? Does IDLE
still need the executable file statement at the top even if I run it
explicity?

---------------------------------------------------------------------------------------
import sys
from xml.sax import ContentHandler, make_parser
from xml.sax.handler import feature_namespaces

class AuthorHandler(ContentHandler):
# """Converts an author file"""

def __init__(self,outfilename):
# """Constructor. Takes output file name."""
self.outfile = ""
self.insertCmd = "INSERT Authors SET "

def startElement(self,name,attrs):
# """Overridden. Creates SQL statements from author nodes."""
if name != "root":
self.insertCmd += "%s = '" % self.convertColumnName(name)

def characters(self,chars):
# """Overridden. Add tag values as column values."""
self.insertCmd += chars

def endElement(self,name):
# """Overridden. Close column value and add separator."""
if name == "root":
self.insertCmd += "';"
else:
self.insertCmd += "',"

def convertColumnName(self,name):
return name

def main(args):
print "In main"
# Create an instance of the handler classes
ah = AuthorHandler()

# Create an XML parser
parser = make_parser()

# Tell the parser to use your handler instance
parser.setContentHandler(ah)

# Parse the file; your handler's methods will get called
parser.parse(args[0])

print ah.insertCmd

if __name__ == "__main__":
print "to main"
main("\\Iceman\propod\flash\xml\webService\XmlData \Authors\Heidi")

May 22 '06 #1
Share this Question
Share on Google+
5 Replies


P: n/a
he**********@fantasy-interactive.com a écrit :
I just downloaded the most recent (2.4.3) Python and IDLE (1.1.3) to
Windows XP Professional. I'm new to the IDLE environment, so hopefully
someone can tell me what I'm missing here! Below is the code, which I'm
editing within IDLE, and attempting to test with the Run commands.

I was expecting to see at least see the prints to stdout from the if
__name__ ... statement and main() function, but when I use F5 Run
module I don't see any output or errors. Any suggestions? Does IDLE
still need the executable file statement at the top even if I run it
explicity?
I can't tell you about IDLE related stuff (I don't use it myself), but
FWIW, you have a problem with main()'s 'arg' param : you use it like this:
parser.parse(args[0]) but pass it like this: main("\\Iceman\propod\flash\xml\webService\XmlData \Authors\Heidi")


The first problem is that:
"\\Iceman\propod\flash\xml\webService\XmlData\Auth ors\Heidi"
already raises an exception ('ValueError: invalid \x escape').

The second problem is that somestring[0] returns the first characted of
somestring.

First correct your code, then see what happens. And eventually, find a
better tool than IDLE...
May 22 '06 #2

P: n/a
he**********@fantasy-interactive.com wrote:
I just downloaded the most recent (2.4.3) Python and IDLE (1.1.3) to
Windows XP Professional. I'm new to the IDLE environment, so hopefully
someone can tell me what I'm missing here! Below is the code, which I'm
editing within IDLE, and attempting to test with the Run commands.

I was expecting to see at least see the prints to stdout from the if
__name__ ... statement and main() function, but when I use F5 Run
module I don't see any output or errors. Any suggestions? Does IDLE
still need the executable file statement at the top even if I run it
explicity?

---------------------------------------------------------------------------------------
import sys
from xml.sax import ContentHandler, make_parser
from xml.sax.handler import feature_namespaces

class AuthorHandler(ContentHandler):
# """Converts an author file"""

def __init__(self,outfilename):
# """Constructor. Takes output file name."""
self.outfile = ""
self.insertCmd = "INSERT Authors SET " ....
Don't comment out those strings, you'll find them more useful in
Idle if they are there.
def main(args):
...
# Parse the file; your handler's methods will get called
parser.parse(args[0])

print ah.insertCmd

if __name__ == "__main__":
print "to main"
main("\\Iceman\propod\flash\xml\webService\XmlData \Authors\Heidi")


As someone else said, use both a better string constant and a list,
since main looks at "args[0]".
If nothing else:

main([r"\\Iceman\propod\flash\xml\webService\XmlData\Aut hors\Heidi"])

To investigate the idle environment, simply add:

if __name__ == "__main__":
print "to main"
main([r"\\Iceman\propod\flash\xml\webService\XmlData\Aut hors\Heidi"])
else:
print 'Sorry, __name was %r, not "__main__".' % __name__
--Scott David Daniels
sc***********@acm.org
May 23 '06 #3

P: n/a
Thanks for your help! Shouldn't Idle have shown an error when trying to
read the string constant if it's not interpretable as a normal string,
then?

/Heidi

May 23 '06 #4

P: n/a
he**********@fantasy-interactive.com a écrit :
Shouldn't Idle have shown an error when trying to
read the string constant if it's not interpretable as a normal string,
then?


Yes. In my python shell, it raised an error. I don't know for sure why
this did not appear in IDLE, but there are far better tools anyway.

May 23 '06 #5

P: n/a
he**********@fantasy-interactive.com wrote:
Thanks for your help! Shouldn't Idle have shown an error when trying to
read the string constant if it's not interpretable as a normal string,
then?


I suspect it should. The error probably got lost in the start-script
handling somewhere.

--Scott David Daniels
sc***********@acm.org
May 24 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.