Python - Abrir un script dentro de otro script

 
Vista:
sin imagen de perfil
Val: 4
Ha disminuido su posición en 22 puestos en Python (en relación al último mes)
Gráfica de Python

Abrir un script dentro de otro script

Publicado por Eduardo (3 intervenciones) el 30/04/2020 16:38:30
Tengo un codigo principal que se encarga de validar IP de varios PLC

si una IP esta en linea mandar a llamar al script de ese PLC
si la IP no esta en linea pasa la siguiente IP y valida que este en linea si es asi abre el script de esa IP y si no avanza ala siguiente y asi sucesivamente

el detalle esta en que al validar la IP si abre el archivo correspondiente pero al terminar el script se cierra todo
necesito que al terminar ese script regrese al archivo principal y valide la siguiente IP

este es el script principal

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import subprocess
import sys
import os
 
hostname = '172.21.12.10'
response = os.system("ping " + hostname)
 
 
if response == 0:
    print (hostname, 'PLC EN LINEA, INICIANDO REVISION')
    exec(open("SE1_F1.py").read())
 
else:
    print ('SE1_F1 FUERA DE LINEA, EJECUTANDO SIGUIENTE SCRIPT')
    hostname = ' 192.168.100.5'
    response = os.system("ping " + hostname)
 
    if response == 0:
        print ('SE1_F2 EN LINEA, INICIANDO REVISION')
        exec(open("SE1_F2.py").read())
 
    else:
        print ('SE1_F2 FUERA DE LINEA, EJECUTANDO SIGUIENTE SCRIPT')
        hostname = '172.21.13.10'
        response = os.system("ping " + hostname)
 
        if response == 0:
            print('SE2_R1 EN LINEA, INCIANDO REVISION')
            exec(open("SE2_R1.py").read())
        else:
            print ('SE2_R1 FUERA DE LINEA, EJECUTANDO SIGUIENTE SCRIPT')
            hostname = '172.21.13.12'
            response = os.system("ping " + hostname)
 
            if response == 0:
                print('SE2_R2 EN LINEA, INCIANDO REVISION')
                exec(open("SE2_R2.py").read())
            else:
                print ('SE2_R2 FUERA DE LINEA, EJECUTANDO SIGUIENTE SCRIPT')
                hostname = '172.21.14.10'
                response = os.system("ping " + hostname)
 
                if response == 0:
                    print('SE3_M1 EN LINEA, INCIANDO REVISION')
                    exec(open("SE3_M1.py").read())
                else:
                    print ('SE3_M1 FUERA DE LINEA, EJECUTANDO SIGUIENTE SCRIPT')
                    hostname = '172.21.14.12'
                    response = os.system("ping " + hostname)
 
                    if response == 0:
                        print('SE3_M2 EN LINEA, INCIANDO REVISION')
                        exec(open("SE3_M2.py").read())
                    else:
                        print ('SE3_M2 FUERA DE LINEA, EJECUTANDO SIGUIENTE SCRIPT')
                        hostname = '172.21.11.10'
                        response = os.system("ping " + hostname)
 
                        if response == 0:
                            print('SE4_S1 EN LINEA, INCIANDO REVISION')
                            exec(open("SE4_S1.py").read())
                        else:
                            print ('SE4_S1 FUERA DE LINEA, EJECUTANDO SIGUIENTE SCRIPT')
                            hostname = '172.21.11.11'
                            response = os.system("ping " + hostname)
 
                            if response == 0:
                                print('SE4_S2 EN LINEA, INCIANDO REVISION')
                                exec(open("SE4_S2.py").read())
                            else:
                                print("NINGUN PLC EN LINEA")

y este seria uno de los archivos correspondiente al SE1_F2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
from pylogix import PLC
import sys
sys.path.append('..')
import time
import pandas as pd
 
plc_data = pd.read_excel("Actuadores.xlsx", sheet_name='Result')
with PLC() as comm:
    comm.IPAddress = '172.21.12.12'
    comm.ProcessorSlot = 0
    ret = comm.GetPLCTime()
    print( "Fecha y Hora de Registro Revision Fallas", ret.Value)
    time.sleep(1)
    Estacion = "D2M_150"
    Estacion2 = "D2R_155"
 
#------------------------------------------------------------------------------------------ ESTACION D2M_150 -------------------------------------------------------------------------------#
    print("INICIO DE REVISION ESTACION :" , Estacion)
#ACTUADOR 1
    ACTUADOR = comm.Read('D2M_150.Status.Act._1.WBypassed')
    ret = comm.GetPLCTime()
    Numero_Act_EnBypass = 0
 
    if ACTUADOR.Value == True:
        Sensor = "ACTUADOR 1 WORK SENSOR EN BY PASS"
        Numero_Act_EnBypass= (Numero_Act_EnBypass+1)
        temp_dict = {'PLC IP': comm.IPAddress, 'Date Time': ret.Value, 'Estacion': Estacion, 'Tag': ACTUADOR, 'Value': Sensor}
        plc_data = plc_data.append(temp_dict.copy(), ignore_index=True)
        plc_data.to_excel("Actuadores.xlsx", sheet_name='Result', index=False)
        print(Sensor)
 
    else:
        Sensor = "ACTUADOR 1 WORK OK"
        print(Sensor)
 
 
 
    ACTUADOR = comm.Read('D2M_150.Status.Act._1.HBypassed')
    ret = comm.GetPLCTime()
    if ACTUADOR.Value == True:
        Sensor = "ACTUADOR 1 HOME SENSOR EN BY PASS"
        Numero_Act_EnBypass= (Numero_Act_EnBypass+1)
        temp_dict = {'PLC IP': comm.IPAddress, 'Date Time': ret.Value, 'Estacion': Estacion, 'Tag': ACTUADOR, 'Value': Sensor}
        plc_data = plc_data.append(temp_dict.copy(), ignore_index=True)
        plc_data.to_excel("Actuadores.xlsx", sheet_name='Result', index=False)
        print(Sensor)
 
    else:
        Sensor = "ACTUADOR 1 HOME OK"
        print(Sensor)
#ACTUADOR 2
    ACTUADOR = comm.Read('D2M_150.Status.Act._2.WBypassed')
    ret = comm.GetPLCTime()
 
    if ACTUADOR.Value == True:
        Sensor = "ACTUADOR 2 WORK SENSOR EN BY PASS"
        Numero_Act_EnBypass= (Numero_Act_EnBypass+1)
        temp_dict = {'PLC IP': comm.IPAddress, 'Date Time': ret.Value, 'Estacion': Estacion, 'Tag': ACTUADOR, 'Value': Sensor}
        plc_data = plc_data.append(temp_dict.copy(), ignore_index=True)
        plc_data.to_excel("Actuadores.xlsx", sheet_name='Result', index=False)
        print(Sensor)
 
    else:
        Sensor = "ACTUADOR 2 WORK OK"
        print(Sensor)
 
 
 
    ACTUADOR = comm.Read('D2M_150.Status.Act._2.HBypassed')
    ret = comm.GetPLCTime()
    if ACTUADOR.Value == True:
        Sensor = "ACTUADOR 2 HOME SENSOR EN BY PASS"
        Numero_Act_EnBypass= (Numero_Act_EnBypass+1)
        temp_dict = {'PLC IP': comm.IPAddress, 'Date Time': ret.Value, 'Estacion': Estacion, 'Tag': ACTUADOR, 'Value': Sensor}
        plc_data = plc_data.append(temp_dict.copy(), ignore_index=True)
        plc_data.to_excel("Actuadores.xlsx", sheet_name='Result', index=False)
        print(Sensor)
 
    else:
        Sensor = "ACTUADOR 2 HOME OK"
        print(Sensor)
 
    print("Terminado")
    print("Actuadores en Bypass:", Numero_Act_EnBypass)
    comm._closeConnection
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
sin imagen de perfil
Val: 2.808
Oro
Ha mantenido su posición en Python (en relación al último mes)
Gráfica de Python

Abrir un script dentro de otro script

Publicado por tincopasan (1082 intervenciones) el 30/04/2020 17:03:40
hola:
me parece que una forma. sin modificar tanto el código sería:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
if response == 0:
    print (hostname, 'PLC EN LINEA, INICIANDO REVISION')
    exec(open("SE1_F1.py").read())
else:
    print ('SE1_F1 FUERA DE LINEA, EJECUTANDO SIGUIENTE SCRIPT')
 
hostname = ' 192.168.100.5'
response = os.system("ping " + hostname)
 
if response == 0:
    print ('SE1_F2 EN LINEA, INICIANDO REVISION')
    exec(open("SE1_F2.py").read())
 
 else:
 
    print ('SE1_F2 FUERA DE LINEA, EJECUTANDO SIGUIENTE SCRIPT')
 
hostname = '172.21.13.10'
response = os.system("ping " + hostname)
 
if response == 0:
    print('SE2_R1 EN LINEA, INCIANDO REVISION')
    exec(open("SE2_R1.py").read())
 else:
     etc.

o sea, después del print de cada else, vuelvo a sacar toda la indentación para que vuelva al principal, espero que se entienda.
Otra cosa ese código es sumamente repetitivo, se podría mejorar con un for y el uso de listas.

algo así:

1
2
3
4
5
6
7
8
9
10
11
#-*- coding: utf -8 -*-
 
hostname = [("172.21.12.10","SE1_F1.py"),("192.168.100.5","SE1_F2.py"),("172.21.13.10","SE2_R1.py")]
 
for x in hostname:
    response = os.system("ping " + x[0])
    if response == 0:
        print (x[0], 'PLC EN LINEA, INICIANDO REVISION')
        exec(open(x[1]).read())
    else:
        print (x[1], ' FUERA DE LINEA, EJECUTANDO SIGUIENTE SCRIPT')





Igualcomo dije no estoy seguro que funcione como te comente antes.
Saludos
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar