Issue Grabbing Value Out of set() in Script

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:

	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) 

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?

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()?

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?

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!

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

3 Likes