# Issue Grabbing Value Out of set() in Script

**URL:** <https://forum.inductiveautomation.com/t/issue-grabbing-value-out-of-set-in-script/80600>\
**Category:** Ignition\
**Tags:** ignition80, scripting, database, python, perspective\
**Created:** [October 17, 2023, 2:49pm UTC](https://forum.inductiveautomation.com/t/issue-grabbing-value-out-of-set-in-script/80600 "2023-10-17T14:49:22Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Raymond\_Sipes](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@Raymond\_Sipes](https://forum.inductiveautomation.com/u/Raymond_Sipes)\
**Post date:** [October 17, 2023, 2:49pm UTC](https://forum.inductiveautomation.com/t/issue-grabbing-value-out-of-set-in-script/80600/1 "2023-10-17T14:49:22Z")

</div>

Hello all!

With the script below I am querying the bill of materials from a query, comparing that against the bin inventory, and removing the quantity required from the inventory table.

So far, the script is working as intended based on the component\_id matching with the part\_id. Now, I am having trouble trying to get my script to not fire if the BOM has been removed from the inventory table based on the battery\_id.

I have been able to grab the battery\_id from the table within Ignition, but each time i do a print for the battery\_id it prints like this: `set([66933L])`. I am just trying to grab the numeric values within the `set([]) `. Although I have not been able to do so.

Any ideas where I am going wrong? Let me know if I need to provide any more information!

Script:

```auto
	params = {"WOID":self.parent.parent.parent.getChild("Work Order Number").props.text} 
	ds = system.db.runNamedQuery("BOL/SELECT/BOM", params)	  
	pyData = system.dataset.toPyDataSet(ds)
	system.perspective.print(pyData[1])
	
	ds2 = system.db.runNamedQuery("BOL/SELECT/Bin_Inv_Select")
	pyData2 = system.dataset.toPyDataSet(ds2)
	system.perspective.print(pyData[0])
	
	components_to_update = []
	
	processed_battery_ids = set()
	
	battery_id = self.parent.parent.getChild("Batteries Table").props.selection.data[0].BatteryID
	
	#Loop through each BOM Item for WO
	
	inventory_dict = {}
	for row in pyData2:
	    inventory_dict[row["Part_ID"]] = row["Quantity"]
	
	# Loop through the BOM components
	for row in pyData:
	    component_id = row["Component"]
	    quantity_required = row["Quantity Required"]
	    system.perspective.print(component_id)
	    if battery_id is not None and battery_id in processed_battery_ids:
			continue # Skip this entry and move to the next one
			
		#If they match, subtract from inventory
	    if component_id in inventory_dict:
	    	system.perspective.print('found it')
	        # Subtract the BOM quantity from the inventory quantity
	        new_quantity = inventory_dict[component_id] - quantity_required
	
	        if new_quantity != inventory_dict[component_id]:
	                   # Store the component for update
	        	components_to_update.append({"NQ": new_quantity, "PID": component_id})
	
	if battery_id is not None:
		processed_battery_ids.add(self.parent.parent.getChild("Batteries Table").props.selection.data[0].BatteryID)

	system.perspective.print(processed_battery_ids)
	# Update the components that need updates
	for component in components_to_update:
		system.db.runNamedQuery("BOL/UPDATE/Sub_Bin_Inv", component) 

```

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [October 17, 2023, 3:29pm UTC](https://forum.inductiveautomation.com/t/issue-grabbing-value-out-of-set-in-script/80600/2 "2023-10-17T15:29:51Z")

</div>

> [@Raymond\_Sipes](#):
>
> ```auto
> # Loop through the BOM components
> for row in pyData:
> 
> ```

The battery\_id seems to only ever be set once towards the top of this script, so it doesn't make sense within this quoted loop why you would loop through all of the pyData rows if the if condition within the loop that checks the battery\_id is true, since this will force the loop to `continue` and move on to the next row, which will just keep continuing to the next row as the battery\_id never changes.

As for why it's printing `set(...) `, it's because that's what it is, a set of values. Although the square brackets are curious.  
It doesn't seem like you're actually using processed\_battery\_ids though for anything in the logic?

---

<div class="post-metadata">

**Author:** ![Raymond\_Sipes](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@Raymond\_Sipes](https://forum.inductiveautomation.com/u/Raymond_Sipes)\
**Post date:** [October 17, 2023, 3:37pm UTC](https://forum.inductiveautomation.com/t/issue-grabbing-value-out-of-set-in-script/80600/3 "2023-10-17T15:37:36Z")

</div>

Yes, it seems you are correct.

The overall picture that I am trying to accomplish is once the BOM is removed from inventory I want to hold onto the battery\_id incase an operator mis-clicks a button which would automatically remove the value.

I was hoping to accomplish this within the script, but it seems I may not be able to with the set()?

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [October 17, 2023, 3:41pm UTC](https://forum.inductiveautomation.com/t/issue-grabbing-value-out-of-set-in-script/80600/4 "2023-10-17T15:41:56Z")

</div>

A `set` is simply a collection of distinct values, as opposed to a list which can contain non-distinct or duplicated values.  
Edit: So a set it fine for storing these, although if it's only ever going to be a single value and not an array, then just store the value into the variable. The variable will only exist while the function is running though and will be destroyed once it ends

Im not sure where you are hoping to store these battery ids though if the user was to want to add it back? How did you want them to be able to revert the change?

---

<div class="post-metadata">

**Author:** ![Raymond\_Sipes](https://avatars.discourse-cdn.com/v4/letter/r/4491bb/32.png) [@Raymond\_Sipes](https://forum.inductiveautomation.com/u/Raymond_Sipes)\
**Post date:** [October 17, 2023, 3:50pm UTC](https://forum.inductiveautomation.com/t/issue-grabbing-value-out-of-set-in-script/80600/5 "2023-10-17T15:50:00Z")

</div>

I may be getting too far ahead of myself, then. Thank you for the valuable points!

The only reason this _would_ be an issue is, sometimes the clients will send back the battery for warranty. Which in turn would go through the line again but would not need to remove parts that time around.

Now that I am talking this through, it may be better to create a column within the batteries table that has a bool/bit value that is written once the BOM is removed from inventory. Then, I could run a query against this and if it == 1, then do not remove the BOM.

I am going to attempt this rework and come back with an update, thanks again nminchin!

---

<div class="post-metadata">

**Author:** ![nminchin](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/nminchin/32/6911_2.png) [@nminchin](https://forum.inductiveautomation.com/u/nminchin)\
**Post date:** [October 17, 2023, 3:54pm UTC](https://forum.inductiveautomation.com/t/issue-grabbing-value-out-of-set-in-script/80600/6 "2023-10-17T15:54:31Z")

</div>

I may be misunderstanding something here, but would it be better to log the things you've got/made and use that to determine the qty remaining, instead of subtracting from the table value and changing the data itself?

Ie if BOM wants 3x part X and there are 2x part X parts in the parts purchased/made table, then you know you've got 1 part remaining

Removing records from a table is generally a bad idea as you then lose history and table relational links may be broken leading to orphaned data now with no context
