Hi guys, we're having a bit of trouble regarding script scope, in perspective. Script works on the script console, but it's throwing indentation errors when on button action script. Weirdly, adding and If val: solves the issue, and we can't seem to find out how that works. (val always true)
here's the script on the button (we had to add if val: to make it work):
val = 1
values = []
tagPath2 = "[Sample_Tags]Writeable/WriteableInteger2"
print(0)
data = system.db.runNamedQuery("Ignition 101/All_Query", {})
tagPath = "[Sample_Tags]Siemens_Test/Recette_"
# IdFilamentRecipeDetail
col = 0
for row in range(data.getRowCount()):
truetagPath = tagPath + str(row) + "_/IdFilamentRecipeDetail"
values = data.getValueAt(row, col)
system.tag.writeAsync(truetagPath, values)
here's the same script on the console (without the if val:)
val = 1
values = []
tagPath2 = "[Sample_Tags]Writeable/WriteableInteger2"
system.tag.writeBlocking(tagPath2, 0)
data = system.db.runNamedQuery("Ignition 101/All_Query", {})
tagPath = "[Sample_Tags]Siemens_Test/Recette_"
# IdFilamentRecipeDetail
col = 0
for row in range(data.getRowCount()):
if val:
truetagPath = tagPath + str(row) + "_/IdFilamentRecipeDetail"
values = data.getValueAt(row, col)
system.tag.writeAsync(truetagPath, values)```
The indentation difference is because of the runAction function on the button.
So both of these seem to work, the problem is we don't understand how come it stops working if we remove the *if val:* on the button one.
Thanks in advance,
Olivier
Just passed it in a onClick event and got no indent errors taking the if val away
val = 1
values = []
tagPath2 = "[Sample_Tags]Writeable/WriteableInteger2"
system.tag.writeBlocking(tagPath2, 0)
data = system.db.runNamedQuery("Ignition 101/All_Query", {})
tagPath = "[Sample_Tags]Siemens_Test/Recette_"
# IdFilamentRecipeDetail
col = 0
for row in range(data.getRowCount()):
truetagPath = tagPath + str(row) + "_/IdFilamentRecipeDetail"
values = data.getValueAt(row, col)
system.tag.writeAsync(truetagPath, values)
might try and paste this in 
1 Like
Thanks! Something weird happened. copy/paste worked in console but in button scope we had to retype it manually, now it works.
Anyhow, thanks for your time, much appreciated. Thought we were missing some indentation parameter somewhere....
1 Like
As an aside, you could rewrite your script like this to make a single call to the tag system with all of your paths and values at once, which is much more efficient:
data = system.db.runNamedQuery("Ignition 101/All_Query", {})
basePath = "[Sample_Tags]Siemens_Test/Recette_"
col = 0
tagPaths, values = zip(*[
(
"{}{}_/IdFilamentRecipeDetail".format(basePath, row),
data.getValueAt(row, col)
)
for row in xrange(data.rowCount)
])
system.tag.writeAsync(tagPaths, values)
The zip
builtin can also be used to unzip a list-of-lists, which is then unpacked into two variables, so you can build your two items in a single comprehension. Slightly magic, but the worst part of the whole operation.
You could also just build the two lists imperatively, and get the same performance benefit less obtusely:
tagPaths = []
values = []
for row in xrange(data.rowCount):
tagPath = "{}{}_/IdFilamentRecipeDetail".format(basePath, row)
value = data.getValueAt(row, col)
tagPaths.append(tagPath)
values.append(value)
system.tag.writeAsync(tagPaths, values)
2 Likes
Will try this, thank you very much for the quick assistance!
Don't miss an opportunity to add stuff to your library !
def unzip(dicts, keys):
"""
Unpack lists from a list of dicts.
params:
dicts (list or generator of dicts): Dicts from which the lists will be built
keys (list or generator of strings): The keys from which the data will be retrieved.
return:
A list containing a tuple for each key in the `keys` parameter.
Each tuple contains the values for that key in each dict.
Values default to None if a key is not present in a dict.
exemple usage:
keys = ('name', 'value')
names, values = unzip(data, keys)
"""
return zip(
*(
tuple(d.get(k) for k in keys)
for d in dicts
)
)
3 Likes