# Script scope question

**URL:** <https://forum.inductiveautomation.com/t/script-scope-question/96543>\
**Category:** Ignition\
**Tags:** perspective\
**Created:** [September 27, 2024, 12:29pm UTC](https://forum.inductiveautomation.com/t/script-scope-question/96543 "2024-09-27T12:29:04Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Olivier\_Carrier](https://avatars.discourse-cdn.com/v4/letter/o/977dab/32.png) [@Olivier\_Carrier](https://forum.inductiveautomation.com/u/Olivier_Carrier)\
**Post date:** [September 27, 2024, 12:29pm UTC](https://forum.inductiveautomation.com/t/script-scope-question/96543/1 "2024-09-27T12:29:04Z")

</div>

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):

```python
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:_)

````python
	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
````

---

<div class="post-metadata">

**Author:** ![Benjamin\_Furlani](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/benjamin_furlani/32/66084_2.png) [@Benjamin\_Furlani](https://forum.inductiveautomation.com/u/Benjamin_Furlani)\
**Post date:** [September 27, 2024, 12:42pm UTC](https://forum.inductiveautomation.com/t/script-scope-question/96543/2 "2024-09-27T12:42:43Z")

</div>

Just passed it in a onClick event and got no indent errors taking the if val away

```python
	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 🤷‍♂️

---

<div class="post-metadata">

**Author:** ![Olivier\_Carrier](https://avatars.discourse-cdn.com/v4/letter/o/977dab/32.png) [@Olivier\_Carrier](https://forum.inductiveautomation.com/u/Olivier_Carrier)\
**Post date:** [September 27, 2024, 1:26pm UTC](https://forum.inductiveautomation.com/t/script-scope-question/96543/3 "2024-09-27T13:26:48Z")

</div>

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....

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [September 27, 2024, 2:36pm UTC](https://forum.inductiveautomation.com/t/script-scope-question/96543/4 "2024-09-27T14:36:02Z")

</div>

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:

```python
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:

```python
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)

```

---

<div class="post-metadata">

**Author:** ![Olivier\_Carrier](https://avatars.discourse-cdn.com/v4/letter/o/977dab/32.png) [@Olivier\_Carrier](https://forum.inductiveautomation.com/u/Olivier_Carrier)\
**Post date:** [September 27, 2024, 2:50pm UTC](https://forum.inductiveautomation.com/t/script-scope-question/96543/5 "2024-09-27T14:50:50Z")

</div>

Will try this, thank you very much for the quick assistance!

---

<div class="post-metadata">

**Author:** ![pascal.fragnoud](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pascal.fragnoud/32/24728_2.png) [@pascal.fragnoud](https://forum.inductiveautomation.com/u/pascal.fragnoud)\
**Post date:** [September 27, 2024, 2:56pm UTC](https://forum.inductiveautomation.com/t/script-scope-question/96543/6 "2024-09-27T14:56:10Z")

</div>

> [@paul-griffith](#):
>
> The `zip` builtin can also be used to _unzip_ a list-of-lists

Don't miss an opportunity to add stuff to your library !

```python
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
		)
	)

```
