0

I'm not sure what this is called but this is what I need. A portion of code in a python script that is stored in another file. When the script runs, automatically the code is inserted.... something like a function but without arguments passing.

line 100
line 101
line 102
line 103
line 104

I want lines 101-103 to be stored in another file. When the python script executes, lines 101-103 are automatically inserted as it is. So now my code looks like this

line 100
read code from another file
line 104

Even better, if I am able to select which codes to insert in between lines 100 and 104 (e.g. from file 1 or file 2 depending on condition)
I don't want to use a function because it involves a lot of variable passing.

ng0323
  • 317
  • 1
  • 6
  • 14

2 Answers2

3
if condition==1:
    execfile('filename1.py',globals(),locals())
else:
    execfile('filename2.py',globals(),locals())

UPDATE: To show that variables are accessible back and forth:

f1.py:

x='ha'
execfile('f2.py',globals(),locals())
print('after: '+x)

f2.py

print('before: '+x)
x='blah'

Output:

before: ha
after: blah

Therefore, the value of x is passed to f2.py and the value set there is then accessible in f1.py.

Adam Kerz
  • 919
  • 1
  • 6
  • 14
  • That seems to work, but with limitation- either the variables of the main file was not seen by the secondary file or the variables (or changes to them) in the secondary file was not seen by the main file. I tried this with various combinations of globals(),locals(). – ng0323 Jan 22 '15 at 03:03
  • Works for me in py2.7. Suggest there is something else going on with your code that makes it seem as though it's not working. See update to the answer. – Adam Kerz Jan 22 '15 at 03:45
0

You can use m4 for this. Put this in stuff.py:

print 1
print 2
include(other.py)
print 3
print 4

And this in other.py:

print 'a'
print 'b'

And run it this way:

m4 stuff.py | python

The above assumes a *nix system (because those have m4). If you have a system with a C compiler but no m4, you can use the C preprocessor instead! Just change include(other.py) to #include "other.py" and run with cpp (or whatever the C preprocessor is called on your system) instead of m4. This is rather more hacky, but perhaps more portable.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • All well and good until you want to choose the include file based on a condition that is evaluated within the python script its self - ie in real time vs just before runtime. – Adam Kerz Jan 21 '15 at 05:34
  • @AdamKerz: well you can still `include()` within an `if` condition, but it will make indentation tricky. :) I like your answer if it works for the OP. – John Zwinck Jan 21 '15 at 05:36
  • You are spot on @John Zwinck - I didn't think of that, but, as you say, there is the complication of the indentation issue :-) – Adam Kerz Jan 21 '15 at 11:16
  • @Sören: You'll need to be more specific if you want help. Did you try it and get an error message? – John Zwinck Apr 06 '19 at 00:18