Curly braces, square brackets, parenthesis?

Is there a guide somewhere for what to use when and where? I have a script calling a named query, it works if I type in a value for the parameter. I can't get the formatting right to pass a value from a tag.

1 Like

When you say a script, do you mean a project script or a binding on a property? In the majority of cases you will encase it in {}, ie {[System]Client/User/Country}. In python scripts you encase the path in ", ie tagPath = "[System]Client/User/Country"

If you are calling it in a binding, and have it set as a query binding, select the parameter you want to use a tag to source and then press the little tag icon next to the parameters area
Untitled
It should auto fill with the expected format.
Untitled2

If you are in an actual python script and calling system.db.runNamedQuery and trying to bring the value from a tag, you will need to make a call to system.tag.readBlocking(["TagPathHere"]) and put the result into a dictionary where the keys match the names of the parameters for the named query.

2 Likes

I'm trying to call a named query from a button in Perspective. I'll post the script tomorrow when I have access to the gateway and designer again.

thanks for the response.

Then you are most likely in a python script. The following code should give you an idea of the general flow you should follow.

tagPaths = [
	"[provider]Path/To/Tag1",
	"[system]Path/To/Tag2"
]

response = system.tag.readBlocking(tagPaths)

value1 = response[0].value
value2 = response[1].value

queryResult = system.db.runNamedQuery("pathToQuery", {"param1": value1, "param2": value2})

self.session.custom.key=queryResult
1 Like

You can also use the one-liner:

value1, value2 = [qval.value for qval in system.tag.readBlocking(tagPaths)]
3 Likes

In scripts (actual scripts, not expressions), the language is jython (pretty much python running on java).
There, the symbols are... python symbols. You can easily find a reference guide online, but basically:

Parenthesis: used either for a function definition/call, ie function_name(parameters/arguments) or for tuples: days_of_the_week = ('monday', 'thursday', 'wednesday', [...]). They can also be used for 'scoping' when indentation annoys you.
For example some_list = get_items().filter().sort().reverse() could be written

some_list = (
    get_items()
    .filter_it()
    .sort_it()
    .reverse_it()
)

or

long_string = (
    "long line of text"
    " and another one"
    " that will be concatenated"
)

Which wouldn't work without the parentheses.

Square brackets are used for accessing values in data structures, for example some_list[3] will access the fourth element in the list (indexing is zero-based), some_dict['key'] will access the key element in a dictionary...
They can also be used to make lists: [1, 2, 3, 4] will create a list with those 4 elements.
You'll often see brackets surrounding expression like this [x*2 for x in range(12)]. This is called a list comprehension. What's inside is a generator expression, and the results it yields are used to create a list. It's equivalent to list(x*2 for x in range(12))

Curly braces are pretty much the equivalent of brackets, but for dictionaries or sets.
{1, 2, 1, 3, 1, 4, 2, 3} will create a set containing 1, 2, 3, 4.
{'name': "foo", 'value': 42} will create a dictionary with the key/value pairs name = foo and value = 42
Can be used in comprehensions just like brackets for lists.

In expressions, parentheses are used for function calls.
brackets are used to access data structure elements, for example some_dataset[row_numer, column_name]
And curly braces, the ones you'll see the most, are used to note that the value inside is a path to a value defined somewhere else, and will be replaced by the value it references. {[default]some/path} will get the value of the tag some/path in the default tag provider. {session.custom.foo} will be the session custom property foo. {view.parameters.bar} will be the value of the parameter bar passed to the view.

You'll get more details, use cases, etc in the inductive university. I highly encourage you to go through it. It takes a little while, because it has a lot to teach, but it's worth it.
There's also the doc, which is frankly quite good.

3 Likes

This works

#Call Named Query to Delete Selected Recipe
param = {"SelectedRecipeName":"junk"}
system.db.runNamedQuery("Rec_DeleteSelected",param)

This doesn't
def runAction(self, event):
#SetUp Tag Variable Name
varTag = ["[default]L8_/Recipe/L8_RecipeName"]
#Read In Tag Value
tagValue = system.tag.readBlocking(varTag)
#Set Parameter Value for SelectedRecipeName To Use In Named Query
param = {"SelectedRecipeName":tagValue}
#Call Named Query to Delete Selected Recipe
system.db.runNamedQuery("Rec_DeleteSelected",param)

I see my missing step now!

value0 = tagValue[0].value

THANK Y'ALL!

One more new issue but part of the same goal (recipes).
/L8_RecipeName is a string tag and the others are floats.
I get the error " Error trying to coerce 'huh' to a number.".

def runAction(self, event):
#SetUp Tag Variable Name
	varTag = ["[default]L8_/Recipe/L8_RecipeName","[default]L8_/Recipe/L8_Rec_Ing1","[default]L8_/Recipe/L8_Rec_Ing2","[default]L8_/Recipe/L8_Rec_Ing3","[default]L8_/Recipe/L8_Rec_Ing4","[default]L8_/Recipe/L8_Rec_Ing5","[default]L8_/Recipe/L8_Rec_Ing6","[default]L8_/Recipe/L8_Rec_Ing7","[default]L8_/Recipe/L8_Rec_Ing8","[default]L8_/Recipe/L8_Rec_Ing7"]
#Read In Tag Value
	tagValue = system.tag.readBlocking(varTag)
#Reference Item
	value0 = tagValue[0].value
	value1 = tagValue[0].value
	value2 = tagValue[0].value
	value3 = tagValue[0].value
	value4 = tagValue[0].value
	value5 = tagValue[0].value
	value6 = tagValue[0].value
	value7 = tagValue[0].value
	value8 = tagValue[0].value
	value9 = tagValue[0].value
#Set Parameter Value for SelectedRecipeName To Use In Named Query
	params = {"ParRecipeName":value0,"ParCloser":value1,"parFiller":value2,"ParJuicer":value3,"ParInfeedAuger":value4,"ParSpiral":value5,"ParSpiralInfeed":value6,"ParSiraldischarge":value7,"ParHopperShaker":value8,"ParInfeedCanBelt":value9}
#Call Named Query to Save Recipe

	system.db.runNamedQuery("Rec_InsertData",params)

Edit your post and wrap your code in triple backticks or use the preformatted text tool.
This is hard to read.

2 Likes

Sorry for my ignorance, no comprende

  • Hit the pencil icon below each of your posts.
  • Select the code block.
  • Then press the </> button.
1 Like

I got it now. Y'alls help has been invaluable

You're using the same value for every parameter. Clearly that's not what you meant to do ?

Anyway, cleaned up version of that script, using values sequentially instead of always the same:

def runAction(self, event):
	tagPaths = [
		"[default]L8_/Recipe/L8_RecipeName",
		"[default]L8_/Recipe/L8_Rec_Ing1",
		"[default]L8_/Recipe/L8_Rec_Ing2",
		"[default]L8_/Recipe/L8_Rec_Ing3",
		"[default]L8_/Recipe/L8_Rec_Ing4",
		"[default]L8_/Recipe/L8_Rec_Ing5",
		"[default]L8_/Recipe/L8_Rec_Ing6",
		"[default]L8_/Recipe/L8_Rec_Ing7",
		"[default]L8_/Recipe/L8_Rec_Ing8",
		"[default]L8_/Recipe/L8_Rec_Ing7"
	]

	values = [qval.value for qval in system.tag.readBlocking(tagPaths)]
	keys = [
		"ParRecipeName",
		"ParCloser",
		"parFiller",
		"ParJuicer",
		"ParInfeedAuger",
		"ParSpiral",
		"ParSpiralInfeed",
		"ParSiraldischarge",
		"ParHopperShaker",
		"ParInfeedCanBelt"
	]
	params = dict(zip(keys, values))
	system.db.runNamedQuery("Rec_InsertData", params)
2 Likes