Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Python script calling another and passing all vars set in first 2

Status
Not open for further replies.

AIXouch

Technical User
Mar 27, 2003
5
US
I've seen many examples of python script A calling python script B, but in testing I can't figure out how ALL variables set in the script A can be referenced by the called script B. eg I set x = "foo" in the 1st script and then when I call the 2nd script, I want to reference x which has been set to "foo". If I have 50 + (an unknown # ) vars set in the first script, I don't want to explicitly hand the second script all 50+ variables since I can't really know what they are each time it runs, I just want it to script B to 'know' all 50+. Is that possible ? Some languages call this an 'include'. thanks !
 
It seems the answer is there is no way to do what I require and will just have to concatenate the scripts into one.
 
Since you haven't said what you need to to do I can't comment further.
 
Hmm, I guess my original post wasn't clear. A.py runs and an unknown # of vars is set in it, A.py then 'reads' in B.py and it is executed, the code in B.py can reference any variable set by A.py before it was read in and any variable set or changed in B.py is known to A.py after the B.py code is at the end. The key here is each time it runs you don't know what variables will be set, so you can't explicitly 'hand' those variables to B.py. Anyway, it seems that splicing in B.py into A.py may be easiest.
 
Do you have to convert PHP which does such questionable things as includes?
I don't know another language which does includes this way as just in place adding of the include file as if this was one script.

The only way this can make sense, also in PHP, is when this only happens conditionally and not always. If it's always it only may help with mainting code in two or more shorter files.
I would really think about refactoring the code to shorten it, make it OOP or at least procedural with several functions to which you pass the necessary vars.

For now the fastest solution you have is really just merging your two files into one, it's not good programming style anyway. My experience with such features only one or few languages offer is the feeling of superiority of this feature, but it really asks for bad programming style.

Actually the closest I know is exec(open("./filename").read()), but I don't use that. This will run the file in the same context, but I'm not sure whether that means having same variable access, what's for sure is it can access the same sys.argv arguments initially going onto the first py script call.

Chriss
 
Chris - the question is about python not PHP.
 
It seems like you need first to understand the important differences between:

A file containing Python statements.

A Python program.

Classes.

Functions.
 
If you tell us what you need to do instead of how you think it should be done you will likely get better suggestions.
 
I needed to convert an archaic language into python quite similarly to what Chris mentioned. I have enough to go on now, so the lecturing can cease. As you were.
 
AIXouch said:
eg I set x = "foo" in the 1st script and then when I call the 2nd script, I want to reference x which has been set to "foo".
IMO, it is not possible without making substantial changes to the scripts.
I would try it as follows: At the end of the 1.script save the variables into a JSON-file and at the begin of the 2.script read the saved JSON into dictionary.
 
It is a proprietary IBM language without a name really. It is used to process snmp, syslog, etc events. Great regex capabilities, but costly to run.
 
xwb,

I'm totally aware. I asked whether his job is to do a conversion from PHP to Python.

Chriss
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top