Question about 'if __name__ == "__main__":'

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Amy G

    Question about 'if __name__ == "__main__":'

    I have a program that needs a little help.
    Right now the program runs in my crontab. When it runs, it sets a few
    variables based on a query to a MySQL database. I would like to modify it
    so that it can run as it is... or if arguments are supplied, use those
    instead of querrying the database.

    Will using this statement help me out?

    if __name__ == "__main__":

    I seem to recall that this returns true if it is run as a script by python,
    rather than as a module from another prog. Since I am going to run this
    from a command line - or from my crontab... it will always return true as
    far as I can tell.

    Any suggestions to help me out.

    Ultimately I want something like this pseudocode...

    if (no args supplied):
    curs.execute("" "SELECT userid FROM users""")
    data = curs.fetchall()
    else:
    data = sys.argv[1]

    do something with data:
    blah
    blah
    blah

    Thanks in advance for your help.


  • PoD

    #2
    Re: Question about 'if __name__ == "__main__& quot;:'

    On Mon, 02 Feb 2004 23:33:42 -0800, Amy G wrote:
    [color=blue]
    > I have a program that needs a little help.
    > Right now the program runs in my crontab. When it runs, it sets a few
    > variables based on a query to a MySQL database. I would like to modify it
    > so that it can run as it is... or if arguments are supplied, use those
    > instead of querrying the database.
    >[/color]

    Hint: len(sys.argv)

    Comment

    • sean

      #3
      Re: Question about 'if __name__ == "__main__& quot;:'

      Thanks....

      exactly what I was looking for.

      "PoD" <pod@internode. on.net> wrote in message
      news:pan.2004.0 2.03.07.39.39.5 4419@internode. on.net...[color=blue]
      > On Mon, 02 Feb 2004 23:33:42 -0800, Amy G wrote:
      >[color=green]
      > > I have a program that needs a little help.
      > > Right now the program runs in my crontab. When it runs, it sets a few
      > > variables based on a query to a MySQL database. I would like to modify[/color][/color]
      it[color=blue][color=green]
      > > so that it can run as it is... or if arguments are supplied, use those
      > > instead of querrying the database.
      > >[/color]
      >
      > Hint: len(sys.argv)
      >[/color]


      Comment

      • Bill Anderson

        #4
        Re: Question about 'if __name__ == &quot;__main__& quot;:'

        On Mon, 02 Feb 2004 23:33:42 -0800, Amy G wrote:
        [color=blue]
        > I have a program that needs a little help.
        > Right now the program runs in my crontab. When it runs, it sets a few
        > variables based on a query to a MySQL database. I would like to modify it
        > so that it can run as it is... or if arguments are supplied, use those
        > instead of querrying the database.
        >
        > Will using this statement help me out?
        >
        > if __name__ == "__main__":
        >
        > I seem to recall that this returns true if it is run as a script by python,
        > rather than as a module from another prog. Since I am going to run this
        > from a command line - or from my crontab... it will always return true as
        > far as I can tell.[/color]

        This allows you to set up your script to act differently when imported vs.
        ran. For return code, you can put in things like "sys.exit(retur nvalue)"
        to exit w/non true.

        [color=blue]
        > Any suggestions to help me out.
        >
        > Ultimately I want something like this pseudocode...
        >
        > if (no args supplied):
        > curs.execute("" "SELECT userid FROM users""")
        > data = curs.fetchall()
        > else:
        > data = sys.argv[1][/color]


        Personally, I'd go with:

        try:
        data = sys.argv[1]
        except IndexError:
        curs.execute(.. .)
        data = curs.fetchall()

        "Better to Ask Forgivness Than Permission"-ly y'rs.

        Bill

        Comment

        • Amy G

          #5
          Re: Question about 'if __name__ == &quot;__main__& quot;:'

          Thanks. I think I will probably use the len(sys.args) line. That seems
          like a really staright forward approach since I will either be supplying an
          argument - or not.


          "Amy G" <amy-g-art@cox.net> wrote in message
          news:k9ITb.2869 7$P17.28414@fed 1read03...[color=blue]
          > I have a program that needs a little help.
          > Right now the program runs in my crontab. When it runs, it sets a few
          > variables based on a query to a MySQL database. I would like to modify it
          > so that it can run as it is... or if arguments are supplied, use those
          > instead of querrying the database.
          >
          > Will using this statement help me out?
          >
          > if __name__ == "__main__":
          >
          > I seem to recall that this returns true if it is run as a script by[/color]
          python,[color=blue]
          > rather than as a module from another prog. Since I am going to run this
          > from a command line - or from my crontab... it will always return true as
          > far as I can tell.
          >
          > Any suggestions to help me out.
          >
          > Ultimately I want something like this pseudocode...
          >
          > if (no args supplied):
          > curs.execute("" "SELECT userid FROM users""")
          > data = curs.fetchall()
          > else:
          > data = sys.argv[1]
          >
          > do something with data:
          > blah
          > blah
          > blah
          >
          > Thanks in advance for your help.
          >
          >[/color]


          Comment

          Working...