Trouble with aggerage functions

I am trying to do some math in my application.

I get name errors on sum() and mean() functions.

When I try the basic sum(1,2,3) or mean(1,2,3) it still pops the name error.

I am using Ignition 7.2.6(b128)

Matt

Where are you using the functions? Can you post the exact error message (with stack trace if applicable)?

Travis,

I was trying to code something instead of binding number displays to an expression. The sum function did not work within the coding part, but when I did bind it to an expression, it worked just fine.

I have made a test button on my project to demonstrate an example of the problem.

The code…

nsum = sum(1,2,3)
system.gui.messageBox(“The answer is %d” % nsum)

Traceback (innermost last):
File “event:actionPerformed”, line 1, in ?
NameError: sum

Ignition v7.2.6 (b128)
Java: Sun Microsystems Inc. 1.6.0_24

Perhaps we’re missing some sort of import , but in the meantime, I got around it by creating a script in the Module editor:


That way you can use it globally:

I defined sumargs as float, because if all the arguments are integers, then the result will also be an integer-- and a truncated one at that. Not so much a problem for sums, but for means that can be annoying! :laughing:

You can simply import this file into the Script Module Editor…
math.py (222 Bytes)

…or, if you’re a copy and paste kind of guy: :mrgreen:

[code]def sum(*args):
sumargs=float(0)
for i in args:
sumargs += i
return sumargs

def mean(*args):
sumargs=float(0)
countargs=0
for i in args:
sumargs += i
countargs += 1
meanargs=sumargs/countargs
return meanargs[/code]

Jordan,

Thanks. I guess I got confused on the manual. Since this is listed under the expression functions appendix I automatically assumed that it was available to the scripting.

It is available as an expression function. Expression functions can only be used in expression bindings not in scripting. Appendix B of the user manual are expression functions and Appendix C are scripting functions. All scripting functions start with system… Jordan just created a couple of functions to do the same thing as the expression functions. Hope this helps.

I found out why it wouldn’t work originally. The current python implementation in Ignition is v2.1-- sum() wasn’t added until v2.3.