Calling a script requiring user input from another script

Discussion in 'Python' started by mzagursk@gmail.com, Feb 18, 2009.

  1. Guest

    I'm kind of new to this so bear with me.

    I have a script made that requires user input (lets call it script A)
    while it's running. However, I would like to create another script
    (script B) that can batch process (i.e. run script A over and over
    with different user inputs based on script B). Is this possible? and
    how so? Thanks in advance.
     
    , Feb 18, 2009
    #1
    1. Advertising

  2. Chris Rebert Guest

    On Wed, Feb 18, 2009 at 1:00 AM, <> wrote:
    > I'm kind of new to this so bear with me.
    >
    > I have a script made that requires user input (lets call it script A)
    > while it's running. However, I would like to create another script
    > (script B) that can batch process (i.e. run script A over and over
    > with different user inputs based on script B). Is this possible? and
    > how so? Thanks in advance.


    Define a function in A that lets its functionality be used
    programmatically. Then use the `if __name__ == "__main__"` trick to
    have A take input from the user and call the function you just defined
    with the user input if it's run as a script.

    In B, import A's function and call it repeatedly on the inputs.

    Example (assume addition is A's fancy functionality):

    #A.py BEFORE:
    while True:
    input_ = raw_input()
    if input_ == "exit":
    break
    x = int(input_)
    y = int(raw_input())
    print x + y

    #A.py AFTER:
    #functionality refactored into a function
    def add(x, y):
    return x + y

    if __name__ == "__main__":
    while True:
    if input_ == "exit":
    break
    x = int(input_)
    y = int(raw_input())
    print add(x, y)#use the function

    #B.py
    from A import add

    for i,j in some_input_pairs:
    add(i, j)#use the function


    Cheers,
    Chris

    --
    Follow the path of the Iguana...
    http://rebertia.com
     
    Chris Rebert, Feb 18, 2009
    #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. Garrick
    Replies:
    0
    Views:
    571
    Garrick
    Sep 27, 2005
  2. qwerty
    Replies:
    3
    Views:
    9,328
    Scott Allen
    Sep 30, 2004
  3. Santosh

    Validate User Input before calling java script function

    Santosh, Jun 3, 2006, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    167
    Scott M.
    Jun 5, 2006
  4. Robert Cohen
    Replies:
    3
    Views:
    306
    Andrew Durstewitz
    Jul 15, 2003
  5. Martin Landa
    Replies:
    6
    Views:
    104
    Cameron Simpson
    Mar 26, 2014
Loading...

Share This Page