Best way to copy tag values from one register to another

I am recreating a project from vision to perspective. One of the scripts simply copy values over from one UDT to another. They are all either double, integer, or float values with the exception of the first one. The location I want to copy from is ‘Memory’ and the one I want to copy to is ‘Cell/Current’. The vision project was done by a programmer who did a script which accomplished this but they did it by first writing values from ‘Memory’ into a dataset and then wrote the values from the dataset into ‘Cell/Current’.
I decided to accomplish this without creating a dataset to transfer the information. The code I have below does work perfectly fine:

def load_mem_to_current(cell_id):
   """Load recipe parameters from memory to cell/current location
   
   Param:
   	cell_id (str): Id designation of cell
   """

   recipelog = system.util.getLogger("RecipeLogger")
   mempartTag = '[{}]/Cell/Recipe/Memory/0'.format(cell_id)
   curpartTag = '[{}]/Cell/Recipe/Current/Cell/0'.format(cell_id)
   completeTag = '[{}]/Cell/Control/Recipe Loaded'.format(cell_id)
   
   #write memory part number to current partnumber
   memPartNo = system.tag.read(mempartTag).value
   system.tag.write(curpartTag,memPartNo)
   
   index = 1
   while index < 49:
   	memTag = '[{}]/Cell/Recipe/Memory/{}'.format(cell_id, str(index))
   	memValue = system.tag.read(memTag).value
   	curTag = '[{}]/Cell/Recipe/Current/Cell/{}'.format(cell_id, str(index))
   	system.tag.write(curTag, memValue)
   	index = index + 1

My question is is there an advantage to going the dataset method versus what I have? I only have 48 values within the UDT to transfer over and it seems to copy values over pretty quickly.

Consider reducing this to single read and write calls; create lists of tags to read and write, read the former via system.tag.readBlocking, extract values in a list comprehension, and then write the values to the other list with system.tag.writeBlocking.

I do see how list comprehension would be easier. New to scripting and definitely having issues creating your logic. suggestions on how it should look?

One version I believe replaces all the functionality in use in the function you shared above (caveat: untested):

def load_mem_to_current(cell_id):
   """Load recipe parameters from memory to cell/current location
   
   Param:
   	cell_id (str): Id designation of cell
   """
   # List tags to read.
   readTags = ['[{}]/Cell/Recipe/Memory/{}'.format(cell_id, x) for x in range(0,49)]
   # Read tags and extract values.
   writeValues = [x.value for x in system.tag.readBlocking(readTags)]
   # List tags to write.
   writeTags = ['[{}]/Cell/Recipe/Current/Cell/{}'.format(cell_id, x) for x in range(0,49)]
   # Write values to tags.
   system.tag.writeBlocking(writeTags, writeValues)

Yes, I like list comprehensions :slight_smile:

2 Likes

Nice, pretty short! I’ll give it a try, thanks!

1 Like

Worked awesome! Thanks.