Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Calling a script requiring user input from another script

Reply
Thread Tools

Calling a script requiring user input from another script

 
 
mzagursk@gmail.com
Guest
Posts: n/a
 
      02-18-2009
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.
 
Reply With Quote
 
 
 
 
Chris Rebert
Guest
Posts: n/a
 
      02-18-2009
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Validate User Input before calling java script function Santosh ASP .Net Datagrid Control 1 06-05-2006 12:08 AM
Getting User Input after getting Input from a file dei3cmix@uga.edu C++ 3 03-23-2006 05:01 AM
can I use scanf to get input (some times user enters input sometimes not, just hit keyboard)? santa19992000@yahoo.com C Programming 4 09-09-2005 03:38 AM
How to access: a page from a User control, and another User controlfrom another one? qwerty ASP .Net 3 09-30-2004 05:32 PM
Passing value from one script on one page to another script on another page. Robert Cohen ASP General 3 07-15-2003 01:46 PM



Advertisments