Referring to database for keys of authenticated user roles

Hi, new user here

I'm getting the following userroles from this table using
ResultPydataset=system.db.runQuery("SELECT USERROLES FROM DATABASETABLE WHERE FUNCID='Force Bit'")

image

I also have a dictionary in my scripting library:
RoleDictionary = {
'0': "Normal",
'1': "Administrator",
'2': "Engineers",
'3': "MM",
'4': "SS",
'5': "CC",
'6': "OO",
'7': "AA",
'8': "EO",
'9': "Supervisor",
'10': "Chargehand",
'11': "Mechanic",
}

How can I create a new dictionary that filters RoleDictionary based on the keys in ResultPydataset?

Try this in Designer Script Console:

ResultPydataset = "1,2,9,10,11"
RoleDictionary = {
	'0': "Normal",
	'1': "Administrator",
	'2': "Engineers",
	'3': "MM",
	'4': "SS",
	'5': "CC",
	'6': "OO",
	'7': "AA",
	'8': "EO",
	'9': "Supervisor",
	'10': "Chargehand",
	'11': "Mechanic"
}

results = ResultPydataset.split(",")
print results

output = {}
for r in results:
	output[r] = RoleDictionary[r]
print(output)

Result:

>>> 
['1', '2', '9', '10', '11']
{'11': 'Mechanic', '1': 'Administrator', '2': 'Engineers', '9': 'Supervisor', '10': 'Chargehand'}

Tips:

  1. You had a comma after "Mechanic". Python seems to handle it OK but it shouldn't really be there.
  2. When pasting code use the </> button to format it, preserve indents and apply syntax highlighting. For inline bits of code enclose it in single `backticks`.
1 Like

It worked, especially after substituting my query line. Thanks!

Disagree. Trailing commas are a net positive for diffing of lines. With trailing commas, a new entry can be added in a list by only modifying one line. Without trailing commas, adding a new element to the end of a list of entries necessitates modifying two lines.

Can we still be friends?
I understand your thinking. I did find it a problem in SQL query debugging, for example, and resorted to putting the commas on the start of lines so that they could be commented out without generating errors. It was much worse than a trailing comma.

1 Like

Oh, don't get me started on SQL and how awful I consider the idea of "fourth generation languages.".

But in my opinion, if you're in an environment that does support trailing commas, you should use them.