Running script in __main__ shows no output in IDLE

Discussion in 'Python' started by heidi.hunter@fantasy-interactive.com, May 22, 2006.

  1. Guest

    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, 2006
    #1
    1. Advertising

  2. 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\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
     
    Scott David Daniels, May 23, 2006
    #2
    1. Advertising

  3. 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...
     
    Bruno Desthuilliers, May 23, 2006
    #3
  4. Guest

    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, 2006
    #4
  5. 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.
     
    Bruno Desthuilliers, May 24, 2006
    #5
  6. 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
     
    Scott David Daniels, May 24, 2006
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. gce
    Replies:
    0
    Views:
    776
  2. Charlie@CBFC
    Replies:
    2
    Views:
    939
    Charlie@CBFC
    May 23, 2006
  3. Replies:
    5
    Views:
    283
  4. vsoler

    "Breaking" the __main__ script

    vsoler, Mar 14, 2010, in forum: Python
    Replies:
    15
    Views:
    477
    Jean-Michel Pichavant
    Mar 15, 2010
  5. Gianni

    IE shows false and Firefox shows true

    Gianni, Jul 10, 2009, in forum: Javascript
    Replies:
    3
    Views:
    471
    Thomas 'PointedEars' Lahn
    Jul 10, 2009
Loading...

Share This Page