Python dynamic import and changing a variable - python

Lets say I have the following config file:
config.py:
x = 2
y = x * 2
I would like to import this in the file main.py, preferably using load_source command, but I also want to be able to change the value of x at the time of import such that the change in x propagates to the other variables in the config.py. For example, I want the following code, prints 6 and not 4.
main.py:
import imp
config = imp.load_source('', 'config.py')
config.x = 3
print config.y
What is the best way to do that? I know I can write functions in config.py to do this for me, but I prefer the config to be simple variable definitions only.

Put the code into a class:
class Config(object):
def __init__(self, x=2):
self.x = x
self.y = x * 2
Then, in your main program:
c = Config(3)
print c.y

Related

using functions with variables from other python files at notebook's directory

For example, in my folder, I have my ipython notebook "program.ipynb" and a python file "functions.py" which has some functions in it, for example, "func"
from numpy import sqrt
def func(x):
return N + sqrt(x)
that is going to be used in "program.ipynb" which looks like that
from functions import func
N = 5
func(2)
--> name 'N' is not defined
To fix the bug i need to define the variable N in my functions.py file but isn't there a way around? I want to define all my global variables in my main programm (program.ipynb).
You can't access a variable like that, the best way would be:
functions.py
from numpy import sqrt
def func(x, N):
return N + sqrt(x)
program.ipynb
from functions import func
N = 5
func(2, N)

Python Updating Source Code wrt Output of a Function

As example, my first module is
from sympy import *
x,y=symbols('x y')
def A():
equation=2*x+y
return equation
print(A())
the output is
2*x + y
my second module is
def B(x,y):
equation=2*x + y
return equation
I have to copy past the output of first module to second one each time from the terminal. Is there a way to pass this output automatically to the source code of B(x,y)?
Thank you for your attention

Share a variable between two files?

What is a mechanism in Python by which I can do the following:
file1.py:
def getStatus():
print status
file2.py:
status = 5
getStatus() # 5
status = 1
getStatus() # 1
The function and the variable are in two different files and I'd like to avoid the use of a global.
You can share variables without making them global by putting them in a module. Anybody who imports the module gets the same module object, so its contents are shared; changes made at one location show up in all the others.
notglobal.py:
status = 0
get.py:
import notglobal
def getStatus():
return notglobal.status
Testing:
>>> import notglobal
>>> import get
>>> notglobal.status = 5
>>> get.getStatus()
5
>>> notglobal.status = 1
>>> get.getStatus()
1

Python: cannot import name x for importing module

** EDIT: Copy-pasting my actual file to ease confusion. The code snippet below is in a file named train_fm.py:
def eval_fm(x,b,w,V):
# evaluate a degree 2 FM. x is p X B
# V is p x k
# some python code that computes yhat
return(yhat);
Now in my main file: I say the following
from train_fm import eval_fm
and I get the error:
ImportError: cannot import name f1
When I type
from train_fm import train_fm
I do not get an error.
OLD QUESTION BELOW :
def train_fm(x,y,lb,lw,lv,k,a,b,w,V):
# some code
yhat = eval_fm(x,b,w,V);
# OUTPUTS
return(b,w,V);
I have a file called f2.py, where I define 2 functions (note that one of the functions has the same name as the file)
def f1():
some stuff;
return(stuff)
def f2():
more stuff;
y = f1();
return(y)
In my main file, I do
from aaa import f1
from aaa import f2
but when I run the first of the 2 commands above, I get
ImportError: cannot import name f1
Any idea what is causing this? The second function gets imported fine.

Changing some part of a function in a module

In #PSP_soil.py:
def evaporation_flux(psi):
h_s = exp(mw*psi/(R*T))
return(E_p*(h_s-h_a)/(1-h_a))
I want to change this function to:
def evaporation_flux(psi):
h_s = exp(mw*psi/(R*T))
return(h_s)
but console in spyder (Python 2.7) do not run the program (E_p and h_a are constant variables), and just show UMD has deleted: PSP_readDataFile, PSP_grid, PSP_ThomasAlgorithm, PSP_soil
Any advice in this case?
You can do this:
from PSP_soil import *
def evaporation_flux(psi):
h_s = exp(mw*psi/(R*T))
return(h_s)
This redefines evaporation_flux from PSP_soil so when you do evaporation_flux(value), it gets called.
from PSP_soil import * imports all constants you need for this function, but you can also do from PSP_soil import evaporation_flux, mv, R, T

Categories