I took the liberty of refactoring this script a bit.
Assuming you showed us the whole thing, it shouldn't change anything about its functionality, it's mostly a readability" (except for batching tag reads, which does improve performances noticeably).
I also assumed the indentation was boinked up, as it doesn't make much sense otherwise.
I changed the names of variables. It's not something that's set in stone, but a good naming convention makes things easier. Here are a few rules I use, and which are I believe quite common:
- Variables names don't start with a capital, that's for classes
- Full caps variable names are for constants
- Once you pick a style, stick with it
- Names in English, makes things easier for everyone when you're sharing code with people who don't speak your language (I'm guessing 'consulta' means 'query' from context, but guessing is not something to do when it comes to code)
Aside from that:
- Might as well check the 4 elements at once:
event.path[0] == 0 and event.path[1] == 6 and event.path[2] == 1 and event.path[3] == 0
# becomes
event.path[:3] == [0, 6, 1, 0]
- don't compare booleans to
True
or False
:
if Paso == True:
# becomes
if paso:
Though those conditions can be removed:
you're setting paso
's to True
or False
based on another condition. Might as well used that condition directly:
FILAS_INSERTADAS = system.db.runPrepUpdate(CONSULTA, VALORES)
if FILAS_INSERTADAS > 0:
Paso = True
else:
Paso = False
if Paso == True:
# code
# becomes
if system.db.runPrepUpdate(query, [t_stamp]+values) > 0:
# code
You're setting up another boolean variable (Paso1
) in a if
block, then checking that out of the if's scope.
Two things here: If the first block is not executed, Paso1
does not exist and an error will be raised.
And it's not necessary anyway, you can just put that 2nd if's block in the first one.
Also, the inner functions don't serve any purpose here. Just put their code directly where the function is called.
Adding all this together:
def runAction(self, event):
if event.path[:3] == [0, 6, 1, 0]:
t_stamp = system.date.now()
tag_paths = [
"[Kenmex]Kenmex/Axle CV1/stopcond/Matutino/TEO_TP/TEOTOTALMIN",
"[Kenmex]Kenmex/Axle CV1/stopcond/Matutino/TEO_TP/TPTOTALMIN",
"[Kenmex]Kenmex/Axle CV1/stopcond/Vespertino/TEO_TP/TPTOTALMIN",
"[Kenmex]Kenmex/Axle CV1/stopcond/Vespertino/TEO_TP/TEOTOTALMIN",
"[Kenmex]Kenmex/Axle CV1/stopcond/TEO_TP_TOTALTURNO/TEOTOTALMIN",
"[Kenmex]Kenmex/Axle CV1/stopcond/TEO_TP_TOTALTURNO/TPTOTALMIN",
"[Kenmex]Kenmex/CV 2/stopcond/Matutino/TEO_TP/TEOTOTALMIN",
"[Kenmex]Kenmex/CV 2/stopcond/Matutino/TEO_TP/TPTOTALMIN",
"[Kenmex]Kenmex/CV 2/stopcond/Vespertino/TEO_TP/TEOTOTALMIN",
"[Kenmex]Kenmex/CV 2/stopcond/Vespertino/TEO_TP/TPTOTALMIN",
"[Kenmex]Kenmex/CV 2/stopcond/TEO_TP_TOTALTURNO/TEOTOTALMIN",
"[Kenmex]Kenmex/CV 2/stopcond/TEO_TP_TOTALTURNO/TPTOTALMIN",
"[Kenmex]Kenmex/CV 3/stopcond/Matutino/TEO_TP/TEOTOTALMIN",
"[Kenmex]Kenmex/CV 3/stopcond/Matutino/TEO_TP/TPTOTALMIN",
"[Kenmex]Kenmex/CV 3/stopcond/Vespertino/TEO_TP/TEOTOTALMIN",
"[Kenmex]Kenmex/CV 3/stopcond/Vespertino/TEO_TP/TPTOTALMIN",
"[Kenmex]Kenmex/CV 3/stopcond/TEO_TP_TOTALTURNO/TEOTOTALMIN",
"[Kenmex]Kenmex/CV 3/stopcond/TEO_TP_TOTALTURNO/TPTOTALMIN",
"[Kenmex]Kenmex/CV5/stopcond/Matutino/TEO_TP/TEOTOTALMIN",
"[Kenmex]Kenmex/CV5/stopcond/Matutino/TEO_TP/TPTOTALMIN",
"[Kenmex]Kenmex/CV5/stopcond/Vespertino/TEO_TP/TEOTOTALMIN",
"[Kenmex]Kenmex/CV5/stopcond/Vespertino/TEO_TP/TPTOTALMIN",
"[Kenmex]Kenmex/CV5/stopcond/TEO_TP_TOTALTURNO/TEOTOTALMIN",
"[Kenmex]Kenmex/CV5/stopcond/TEO_TP_TOTALTURNO/TPTOTALMIN",
"[Kenmex]Kenmex/CV 6/stopcond/Matutino/TEO_TP/TEOTOTALMIN",
"[Kenmex]Kenmex/CV 6/stopcond/Matutino/TEO_TP/TPTOTALMIN",
"[Kenmex]Kenmex/CV 6/stopcond/Vespertino/TEO_TP/TEOTOTALMIN",
"[Kenmex]Kenmex/CV 6/stopcond/Vespertino/TEO_TP/TPTOTALMIN",
"[Kenmex]Kenmex/CV 6/stopcond/TEO_TP_TOTALTURNO/TEOTOTALMIN",
"[Kenmex]Kenmex/CV 6/stopcond/TEO_TP_TOTALTURNO/TPTOTALMIN",
"[Kenmex]Kenmex/CV 7/stopcond/Matutino/TEO_TP/TEOTOTALMIN",
"[Kenmex]Kenmex/CV 7/stopcond/Matutino/TEO_TP/TPTOTALMIN",
"[Kenmex]Kenmex/CV 7/stopcond/Vespertino/TEO_TP/TEOTOTALMIN",
"[Kenmex]Kenmex/CV 7/stopcond/Vespertino/TEO_TP/TPTOTALMIN",
"[Kenmex]Kenmex/CV 7/stopcond/TEO_TP_TOTALTURNO/TEOTOTALMIN",
"[Kenmex]Kenmex/CV 7/stopcond/TEO_TP_TOTALTURNO/TPTOTALMIN",
"[Kenmex]Kenmex/Kenmex Monitoreo/Kenmex Monitoreo Totales/TEO_TOTALMIN",
"[Kenmex]Kenmex/Kenmex Monitoreo/Kenmex Monitoreo Totales/TP_TOTALMIN"
]
values = [t_stamp] + [qval.value for qval in system.tag.readBlocking(tag_paths)]
query = "INSERT INTO assy_kenmex_monitoreo_click (t_stamp,CV1_M_TEO,CV1_M_TP,CV1_V_TEO,CV1_V_TP,CV1_TEO_TOTAL,CV1_TP_TOTAL,CV2_M_TEO,CV2_M_TP,CV2_V_TEO,CV2_V_TP,CV2_TEO_TOTAL,CV2_TP_TOTAL,CV3_M_TEO,CV3_M_TP,CV3_V_TEO,CV3_V_TP,CV3_TEO_TOTAL,CV3_TP_TOTAL,CV5_M_TEO,CV5_M_TP,CV5_V_TEO,CV5_V_TP,CV5_TEO_TOTAL,CV5_TP_TOTAL,CV6_M_TEO,CV6_M_TP,CV6_V_TEO,CV6_V_TP,CV6_TEO_TOTAL,CV6_TP_TOTAL,CV7_M_TEO,CV7_M_TP,CV7_V_TEO,CV7_V_TP,CV7_TEO_TOTAL,CV7_TP_TOTAL,KENMEX_TEO_TOTAL,KENMEX_TP_TOTAL) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"
if system.db.runPrepUpdate(query, values) > 0:
data = system.report.executeReport("assy_kenmex/Kenmex Monitoreo/Kenmex Monitoreo_Click", fileType="pdf")
system.perspective.download("Kenmex Monitoreo_{}.pdf".format(system.date.format(t_stamp, "MM-dd-yy_hh-mm a")), data)
system.db.runPrepUpdate("DELETE FROM assy_kenmex_monitoreo_click; TRUNCATE TABLE assy_kenmex_monitoreo_click")