Nested dictionaries are very useful and the first step to more complex dictionaries.
Next step I think is realizing that you can use a dictionary as a makeshift switch statement. Imagine the following on a vision client startup script
STARTUP_FUNC_MAPPING = {
"brian":user.brianStartUp
"eric":user.adminStartUp
"john":user.salesStartUp
}
username = system.security.getUsername()
try:
# will call the right function if brian/eric/john login
STARTUP_FUNC_MAPPING[username]()
except KeyError, e:
# calls this function if anyone else logs in
user.normalStartUp()
Ultimately the dictionary is just a mapping technique where you can map two arbitrary objects together. Recently I had a situation where I had to be able to save a bunch of plc tags to a database table where each plc had its own column, and at another point, be able to retrieve them from the columns and write them to the plc tags. I used a dictionary like
PLC_TO_DB_MAPPING = {
"some/plc/tag/address":"someDBColumn",
"another/plc/tag/address":"anotherDBColumn",
...
}
This allowed me to write two generic functions, one to read from the plc and write to the db, and another that did the opposite.
Now combining this idea mapping two arbitrary objects with what @JordanCClark has shown about how you can nest dictionaries arbitrarily deep and you can now make your configurations different. Save for instance I only wanted to save some of the PLC tags when writing but not all of them, because when reading them back sometimes I want to give them a constant instead. I could do something like
PLC_TO_DB_MAPPING = {
"some/plc/tag/address":{"saveToDb":True, "dbColumn":"someDBColumn"},
"another/plc/tag/address":{"saveToDb":False, "constantValue":5},
...
}
The last “advanced” or complex usage I do or have seen with dictionaries is basically trying to create an ORM with them that maps to a database table or row directly. Say you have a db table
id name number value_1 value_2
1 'bob' 45 1.234 3.145
And that you need to use a record from this table in your code somethwere, it becomes much more legible as you will be able to reference the table names directly
recordData = mySpecialDbLibrary.getRecord("someTable",id=1)
if recordData['name'] == 'bob':
# do something
That’s all I got to say about dictionaries and the advanced/complex way to use in relation ignition. I hope this is relevant to your topic as you said you wanted to know more about complex dictionaries. I’m sure there are a few other use cases others can have but these are the big ones I’ve seen/used personally.