Call script which accepts com. line par. from another script anderror control

Discussion in 'Python' started by Karim Ali, May 24, 2007.

  1. Karim Ali

    Karim Ali Guest

    Hi,

    I would really appreciate help on this. I have a script (s1.py) which I
    would like to call from another script (s2.py). No problem there.

    The first issue is that s1.py has a command line parser which I would like
    to keep but instead of fetching the information from the command line, I
    would like to direct the parser to get the information from a variable so i
    can call sp1.py from sp2.py and give it an expression to parse that normally
    would go on the command line. To make things clearer:

    s1.py (currently) --------------------------------------------

    def ...
    def ...

    cmdl_parser = optparse.OptionParser..
    cmdl_parser.add_option..

    (cmdl_opts, cmdl_args) = cmdl_parser.parse_args()

    -----------------------------------------------------------------

    sp1.py (the one I would like)
    ---------------------------------------------------------------------------------------------

    def ...
    def ...

    def MAIN(expression2parse) <----- add a main so can call
    from other script
    cmdl_parser = optparse.OptionParser..
    cmdl_parser.add_option..

    (cmdl_opts, cmdl_args) = cmdl_parser.parse_args()
    <---------------------------do this but on "expression2parse"

    -----------------------------------------------------------------------------------------------------------------------------------


    The second issue is error control. In case of an error in sp1.py I want the
    error handling to happen at the level of sp2.py so I can better manage
    things. Does anyone know how this could be done. Essentially return control
    to sp2.py?

    I am new to Python (5 hours) but have extensive programming in C++. I would
    really appreciate your input.

    Thanks!

    Kakeez

    _________________________________________________________________
    Fight Allergies With Live Search
    http://search.live.com/results.aspx?q=Remedies For Spring Allergies&mkt=en-ca&FORM=SERNEP
     
    Karim Ali, May 24, 2007
    #1
    1. Advertising

  2. Karim Ali

    James Stroud Guest

    Re: Call script which accepts com. line par. from another scriptand error control

    Karim Ali wrote:
    > def MAIN(expression2parse) <----- add a main so can
    > call from other script


    Of course you don't mean you want another python interpreter to fire up
    and run the other script?

    Here is an example of the way to do what you are suggesting:

    # mod1.py

    def doit(param):
    if not param % 37:
    raise ValueError, 'Why u want to do dat?'
    else:
    print 'All right!'
    return 'Value is: %s' % param

    # end of mod1.py


    # mod2.py

    import mod1

    print mod1.doit(20)
    print
    print mod1.doit(30)
    print
    try:
    print mod1.doit(37)
    except ValueError:
    print 'Gracefully handling 37....'
    print
    print mod1.doit(37)

    # end of mod2


    James
     
    James Stroud, May 24, 2007
    #2
    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. Daniel Bress
    Replies:
    0
    Views:
    489
    Daniel Bress
    Sep 15, 2003
  2. Replies:
    0
    Views:
    384
  3. PAR, PAR::Packer

    , Dec 1, 2006, in forum: Perl Misc
    Replies:
    1
    Views:
    216
    John Bokma
    Dec 1, 2006
  4. Replies:
    0
    Views:
    228
  5. Oliver Soeder
    Replies:
    1
    Views:
    251
Loading...

Share This Page