[Tutor] Returning multiple values from a script
Kent Johnson
kent37 at tds.net
Thu Jan 12 02:02:20 CET 2006
Hans Dushanthakumar wrote:
> Yes I agree that it'd be cleaner to import the second script and call
> it.
>
> The reason I'm keen to find a alternate method is that I have a whole
> lot of scripts that were designed to run only as standalone scripts. ie
> each of these scripts is not a "function" that I could just call from
> another script. They are all of the format:
>
> 1) Test.py
> -------
> import sys
>
> sys.exit(5)
>
>
> Now I'm trying to write a master script that'd run each one of these
> scripts. I'm sure it would have been a lot easier if the scripts were of
> the following format. Unfortunately they are not.:
>
> Test.py
> -------
> import sys
>
> Def test():
> return(5, "Junk")
>
> if __name__ == __main__:
> test()
>
>
> Well if there is no other way I think I'll have to alter all the scripts
> to be of the above format. Just wondering if anyone has any suggestions
ISTM that you have to change all the scripts anyway if you want to
return two values...why not change them to call a different function
(other than sys.exit) that does what you want? Do the scripts still have
to run standalone?
For example you could make a module mysys.py:
returnedvalue = None
def exit(value):
global returnedvalue
returnedvalue = value
Then just edit the scripts to import mysys and call mysys.exit(), run
the script with import and get the returnedvalue from mysys.
If you want a base hack that I couldn't possibly recommend :-) I suppose
you could replace sys.exit() with a function of your own choosing.
Something like this should work...
>>> import sys
>>> returnedvalue = None
>>>
>>> def mysysexit(value):
... global returnedvalue
... returnedvalue = value
...
>>> original_exit = sys.exit # if you need to keep the old value...
>>> sys.exit = mysysexit
>>>
>>> sys.exit(5) # here you can just import the module you want to run
>>> returnedvalue
5
Isn't Python wonderful!
Kent
> ...
>
> Cheers
> Hans
>
>
> -----Original Message-----
> From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On
> Behalf Of Kent Johnson
> Sent: Thursday, 12 January 2006 12:06 p.m.
> Cc: Python Tutor
> Subject: Re: [Tutor] Returning multiple values from a script
>
> Hans Dushanthakumar wrote:
>
>>Hi,
>> Can a script return multiple values to the os?
>
>
> Is there a reason why you have to call the second test.py using
> os.system()? I would write it to be imported and called.
>
> test.py
> ------
>
> def findR():
> return 7, 'Hans'
>
>
> script1.py
> ---------
>
> import test
>
> res = test.findR()
> print res
>
> Kent
>
>
>>What I have in mind is something like the following:
>>
>>
>>1) Test.py
>>-------
>>import sys
>>
>>r = 7
>>sys.exit(r)
>># What I really want to do is something along the lines of sys.exit(r,
>>"Hans")
>>
>>
>>
>>2) Script1.py (This script executes script test.py and prints out its
>>exit code):
>>----------
>>import os
>>
>>t = "test.py"
>>res = os.system('"python test.py")
>>print res
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> _______________________________________________
> Tutor maillist - Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
More information about the Tutor
mailing list