Variables from one script in another

0

I want to add a new task to a program made by someone else

The other program is composed of several files that work together, but I only need to modify a file which contains only an array with values that it uses for calculation (file name case30 ) p>

from numpy import matrix
GD = matrix('   1   0.0     0        0          0       1       ;'
             '  2   0.0     0       -0.4        0.5     1       ;'
             '  5   0       0       -0.4        0.4     1       ;'
             '  8   0       0       -0.1        0.4     1       ;'
             '  11  0       0       -0.06       0.24    1       ;'
             '  13  0       0       -0.06       0.24    1        ')

subsequently other files use these values to work

my program calculates optimal values and assigns them variables within Calc (the program is very long and I can not put it all), file name window

def Calc():
    P_Barra_1 = resultados_m [0]
    P_Barra_2 = resultados_m [1]
    P_Barra_3 = resultados_m [2]
    P_Barra_4 = resultados_m [3]
    P_Barra_5 = resultados_m [4]
    P_Barra_6 = resultados_m [5] 

I want to replace the values in the first column of the matrix with the results of my program

from numpy import matrix
GD = matrix('   1   0.0     0        0          0       1       ;'
             '  2   0.0     0       -0.4        0.5     1       ;'
             '  5   0       0       -0.4        0.4     1       ;'
             '  8   0       0       -0.1        0.4     1       ;'
             '  11  0       0       -0.06       0.24    1       ;'
             '  13  0       0       -0.06       0.24    1        ')

GD [0,0]= P_Barra_1
GD [1,0]= P_Barra_2
GD [2,0]= P_Barra_3 
GD [3,0]= P_Barra_4
GD [4,0]= P_Barra_5
GD [5,0]= P_Barra_6

I do not know how to make the other file read the variables of my program P_Barra_1,2,3, ...

    
asked by Luis Hugo Barrera Luna 24.10.2017 в 01:24
source

1 answer

0

If I understand what you want, you want the module caso30 to have other values for GD . You do not say if you want the values to be persistent or only for the session . In the first case, there is no alternative but to modify the python file.

But in the case that it is only for the session, there is a trick: the import of modules in python is optimized and only done the first time. The other times use the module already imported.

Make your main program import the module before and change what you need:

import caso30

caso30.GD [0,0]= P_Barra_1
caso30.GD [1,0]= P_Barra_2
caso30.GD [2,0]= P_Barra_3 
caso30.GD [3,0]= P_Barra_4
caso30.GD [4,0]= P_Barra_5
caso30.GD [5,0]= P_Barra_6

# Llama al resto de módulos

import modulo2

modulo2.run()

Although modulo2 import again caso30 , you will see% wd_% the matrix that has been modified.

    
answered by 24.10.2017 в 21:49