Running script in __main__ shows no output in IDLE

H

heidi.hunter

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")
 
S

Scott David Daniels

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\Authors\Heidi"])

To investigate the idle environment, simply add:

if __name__ == "__main__":
print "to main"
main([r"\\Iceman\propod\flash\xml\webService\XmlData\Authors\Heidi"])
else:
print 'Sorry, __name was %r, not "__main__".' % __name__


--Scott David Daniels
(e-mail address removed)
 
B

Bruno Desthuilliers

(e-mail address removed) 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\Authors\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...
 
H

heidi.hunter

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
 
B

Bruno Desthuilliers

(e-mail address removed) 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.
 
S

Scott David Daniels

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
(e-mail address removed)
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,022
Latest member
MaybelleMa

Latest Threads

Top