Script Console vs. Gateway Script

Could someone explain the difference between running a script in the script console vs. under gateway scripts vs. a button?

I have a script that runs under a button and the script console. I put that same script to run at midnight on the gateway, it fails with one of the variable not defined (vmean, in this case).

After the code, I put in the error. Tried placing ‘vmean = 0’ in a few places to define it, but I’m getting nowhere.

Thanks!!

import math
import time

print "Standard Deviation Vessels Population Updated"
#--------------------------------------------------------------
# Process Lot Numbers
#--------------------------------------------------------------
lot = 0

rspn = system.db.runQuery("SELECT DISTINCT(Part_Number) FROM StdDev_Vessel_Lot ORDER BY Part_Number")
rspnlist = []										#create blank list
for row in rspn:
	a = str(row[0])
	rspnlist.append(a)							
for j in range (len(rspnlist)):
	pn = str(rspnlist[j])							#loop through list, get lot number, convert to string
#	print "---------------"
#	print "Part Number: ", pn

	v_oal = system.db.runQuery("SELECT Part_No, Vessel_OAL, ReInsp_Oal, CASE WHEN ReInsp_Oal > 0 THEN ReInsp_Oal ELSE Vessel_OAL END AS OAL FROM Liner2Vessel_Qry WHERE Part_No = '" + pn + "'")
	
	vesselpn = []									#create blank list
	
#	print "Record Set: ", v_oal
	
	if len(v_oal) > 0:
		for row in v_oal:
			val = row[3] 							#vessel oal
			if val < 250 and val > 20:				#lengths greater than 200" and less than 20" is discounted
				vesselpn.append(val)				#create a list of the values (val)
				pn = str(row[0])
				pn = pn.strip()
													#get part number of the record
		
		def vmean(values):
			return sum(values)*1.0/len(values)		#all steps crunched into one return statement
			

		def vstanDev(values): 						#again, values is the list of numbers
			length = len(values)					#find the length of the list
			m = vmean(values)						#get mean of values
			total_sum = 0							#create a sum variable since this is not the same
			
			for i in range(length): 				#creates a list 0 - length minus one
				total_sum += (values[i]-m)**2		#add the number for stan. dev. to sum
			under_root = total_sum*1.0/length
			return math.sqrt(under_root)			#return the standard deviation

		vstan_dev = (vstanDev(vesselpn))
		vmean_pop = vmean(vesselpn)
#		print "Standard Dev	", vstan_dev
		
		vst_dmax = vmean_pop + (vstan_dev)
		vst_dmin = vmean_pop - (vstan_dev)
		
#		print "Standard Dev Max	", vst_dmax
#		print "Mean of Population	", vmean_pop
#		print "Standard Dev Min	", vst_dmin
		
		

		rspn = system.db.runScalarQuery("SELECT Part_Number FROM StdDev_Vessel_Lot_Population WHERE Part_Number = '"+ str(pn) +"'")

		if rspn <> pn:
			system.db.runPrepUpdate("INSERT INTO StdDev_Vessel_Lot_Population (Part_Number, Std_Dev, Mean) VALUES ('%s',%f,%f)" %(pn, vstan_dev, vmean_pop))
		else:
			system.db.runUpdateQuery("UPDATE StdDev_Vessel_Lot_Population SET Std_Dev = %f, Mean = %f WHERE Part_Number = '%s'" %(vstan_dev, vmean_pop, pn))

ERROR:
INFO | jvm 1 | 2017/01/12 14:46:56 | E [TagChangeScriptExecutor ] [20:46:56]: Error executing tag change script: Statistical_Operating_Data/Standard Deviation Vessels Population Update
INFO | jvm 1 | 2017/01/12 14:46:56 | com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last):
INFO | jvm 1 | 2017/01/12 14:46:56 | File “<TagChangeScript:Statistical_Operating_Data/Standard Deviation Vessels Population Update>”, line 49, in
INFO | jvm 1 | 2017/01/12 14:46:56 | File “<TagChangeScript:Statistical_Operating_Data/Standard Deviation Vessels Population Update>”, line 41, in vstanDev
INFO | jvm 1 | 2017/01/12 14:46:56 | NameError: global name ‘vmean’ is not defined
INFO | jvm 1 | 2017/01/12 14:46:56 |
INFO | jvm 1 | 2017/01/12 14:46:56 | at org.python.core.Py.NameError(Py.java:260)
INFO | jvm 1 | 2017/01/12 14:46:56 | at org.python.core.PyFrame.getglobal(PyFrame.java:265)
INFO | jvm 1 | 2017/01/12 14:46:56 | at org.python.pycode._pyx246.vstanDev$2(<TagChangeScript:Statistical_Operating_Data/Standard Deviation Vessels Population Update>:47)
INFO | jvm 1 | 2017/01/12 14:46:56 | at org.python.pycode._pyx246.call_function(<TagChangeScript:Statistical_Operating_Data/Standard Deviation Vessels Population Update>)
INFO | jvm 1 | 2017/01/12 14:46:56 | at org.python.core.PyTableCode.call(PyTableCode.java:165)
INFO | jvm 1 | 2017/01/12 14:46:56 | at org.python.core.PyBaseCode.call(PyBaseCode.java:134)
INFO | jvm 1 | 2017/01/12 14:46:56 | at org.python.core.PyFunction.call(PyFunction.java:317)
INFO | jvm 1 | 2017/01/12 14:46:56 | at org.python.pycode._pyx246.f$0(<TagChangeScript:Statistical_Operating_Data/Standard Deviation Vessels Population Update>:15)
INFO | jvm 1 | 2017/01/12 14:46:56 | at org.python.pycode._pyx246.call_function(<TagChangeScript:Statistical_Operating_Data/Standard Deviation Vessels Population Update>)
INFO | jvm 1 | 2017/01/12 14:46:56 | at org.python.core.PyTableCode.call(PyTableCode.java:165)
INFO | jvm 1 | 2017/01/12 14:46:56 | at org.python.core.PyCode.call(PyCode.java:18)
INFO | jvm 1 | 2017/01/12 14:46:56 | at org.python.core.Py.runCode(Py.java:1275)
INFO | jvm 1 | 2017/01/12 14:46:56 | at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:626)
INFO | jvm 1 | 2017/01/12 14:46:56 | at com.inductiveautomation.ignition.common.script.ScriptManager.runCode(ScriptManager.java:585)
INFO | jvm 1 | 2017/01/12 14:46:56 | at com.inductiveautomation.ignition.common.script.TagChangeScriptExecutor$TagChangeExecutionCallback.execute(TagChangeScriptExecutor.java:187)
INFO | jvm 1 | 2017/01/12 14:46:56 | at com.inductiveautomation.ignition.common.script.TagChangeScriptExecutor$TagChangeExecutionCallback.execute(TagChangeScriptExecutor.java:134)
INFO | jvm 1 | 2017/01/12 14:46:56 | at com.inductiveautomation.ignition.common.util.SerialExecutionQueue$PollAndExecute.run(SerialExecutionQueue.java:99)
INFO | jvm 1 | 2017/01/12 14:46:56 | at java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source)
INFO | jvm 1 | 2017/01/12 14:46:56 | at java.util.concurrent.FutureTask.run(Unknown Source)
INFO | jvm 1 | 2017/01/12 14:46:56 | at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
INFO | jvm 1 | 2017/01/12 14:46:56 | at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
INFO | jvm 1 | 2017/01/12 14:46:56 | at java.lang.Thread.run(Unknown Source)
INFO | jvm 1 | 2017/01/12 14:46:56 | Caused by: org.python.core.PyException: Traceback (most recent call last):
INFO | jvm 1 | 2017/01/12 14:46:56 | File “<TagChangeScript:Statistical_Operating_Data/Standard Deviation Vessels Population Update>”, line 49, in
INFO | jvm 1 | 2017/01/12 14:46:56 | File “<TagChangeScript:Statistical_Operating_Data/Standard Deviation Vessels Population Update>”, line 41, in vstanDev
INFO | jvm 1 | 2017/01/12 14:46:56 | NameError: global name ‘vmean’ is not defined
INFO | jvm 1 | 2017/01/12 14:46:56 |
INFO | jvm 1 | 2017/01/12 14:46:56 | … 22 common frames omitted

Hi! Welcome to the forums! :slight_smile:

If I recall, the scoping is a little bit different in the gateway. Try importing app in your second function:

def vstanDev(values): import app

You might also take a look the the Math Extensions for Ignition that might also help you out.

Regards,

Hi jdrichards,

The scoping of variables in Gateway scripts is different than in scripts on buttons and in the script console.

The best way around this is to put all your code in a function defined in a Python module such as project.mymodule and call the function in your gateway script.

I never write code in a Gateway script because variable scoping is incorrect in it. Instead I write a function in a Python module where scoping works correctly and call the function in the Gateway script.

Ignition 7.7 changed the way scoping of Python variables worked, except the change hasn’t been made to Gateway scripts yet. You can read about Legacy Scoping and Standard Scoping in my blog post here: perfectabstractions.com/blog … rd-scoping

Best,

1 Like

This other topic might help.

That makes more sense now, somewhat. thanks.

I ended up taking out the functions and reprogramming it without them.