Use tag name as variable name in script

Is there a way to use tag name as a variable name in script? I have several tags that I need to use for calculations in script so I need to assign value of a tag as literal value and variable name same as tag name. So for example if I have tag name as F1 with value 2 then I need a variable F1 in script which can be used later in script for calculations like F1+F2. Thanks for the help.

I’m not sure why you would want to do that. Does the number of tags your working with change? Even if it does I would think using lists would be the way to go. Even if your trying to link to the tag name that is passed in, if you had a list of tag names and a list for your values, you can link the two together in your script to use when your doing different functions. Without knowing more of how your script works though, its hard to make a suggestion about how to go about it.

Use a dictionary.

tagDict = {'[default]path/to/F1' : 2}

Number of tags are not changing. I have list of tags and names but in calculations I need to only few values in the list like in example below. I can still use values by using variables with literal value from the list but I have to create like 30-35 variables so I want to use for loop and assign name of the tags to variable name as the tag name also represent the parameter in used in calculations

Master_Dimension_Tags = system.tag.browseTags("Blueprint/Master Dimensions",sort="ASC")
Gage_Value_Tags = system.tag.browseTags("Blueprint/Gage Values",sort="ASC")
FinalValueTags = system.tag.browseTags("Blueprint/Final Values",sort="ASC")
CalculatedValues = system.tag.browseTags("Blueprint/Calculated Values",sort="ASC")

A = [tag.path for tag in Master_Dimension_Tags]
Master_Dimensions = system.tag.readAll(A)

B = [tag.path for tag in Gage_Value_Tags]
Gage_Values = system.tag.readAll(B)

F = [round(float(Gage_Values[x].value)+ float(Master_Dimensions[x].value),4) for x in range(len(Gage_Values))]

C = [tag.path for tag in FinalValueTags]
system.tag.writeAll(C,F)

TagNames = [tag.name for tag in FinalValueTags]
TagValues = [tag.value for tag in FinalValueTags] 


#F7a, F7b, F7c are from list F above.
HS = (F7a+F7b+F7c)/3 
Range_HS = max(F7a,F7b,F7c) - min(F7a,F7b,F7c)
tagDict = {name:value for name, value in zip(TagNames, TagValues)}

1 Like

I do something like the following, in a project script:

tagsA = ["Blueprint/Master Dimensions/"+x for x in ['A1', 'A2', 'A3', 'Awhatever']]
tagsB = ["Blueprint/Gage Values/"+x for x in ['B1', 'B2', 'B3', 'Bwhatever']]
tagsF = ["Blueprint/Final Values/"+x for x in ['F1', 'F7a', 'F7b', 'F7c']]
tagsC = ["Blueprint/Calculated Values/"+x for x in ['C1', 'C7a', 'C7b', 'C7c']]

def myCalculations():
	reads = [x.value for x in system.tag.readAll(tagsA+tagsB)]
	A1, A2, A3, Awhatever = reads[:4]
	B1, B2, B3, Bwhatever = reads[4:]

	F = [round(float(a)+float(b), 4) for a, b in zip(reads[:4], reads[4:])]
	system.tag.writeAll(tagsF, F)

	F1, F7a, F7b, F7c = F
	HS = (F7a+F7b+F7c)/3 
	Range_HS = max(F7a,F7b,F7c) - min(F7a,F7b,F7c)

	# Do something with tagsC ....

The above is called tuple (or sequence) packing/unpacking. Note that I would never use .browseTags() for names that you must know ahead of time (you can’t tuple unpack into a variable number of names).