How to return a row and column of specific data from a dataset

The whole script seems... kinda wrong.
the findEquals function takes a dataset as parameter, but it's never used.
Instead, you use the session variable defined out of it.
You're importing system twice, including right in the middle of the script, AFTER system has already been used. And you shouldn't be importing it at all
You're also import System from java.lang and not using it.

Let me start a sandbox and try to make it prettier

1 Like

Thanks for your reply. I have never used the script library, and didn't even know it existed tbh. Is it bad to write scripts in the gateway? Likewise, why is it bad to import system? I saw that it was needed by some dev posts to use the system library in the gateway. Is this bad practice?

Until the next edit.

Follow pascal's advice. In particular, never use def directly in a gateway event. Use a project library script.

1 Like

Will do. Thanks for helping me work through the beginner's bad habits.

You don't need to import system except in the special case of a defined function in a gateway event script, because they use legacy (insane) Python 2.5 or 2.1 or something scoping rules. Everywhere else, things work consistently, it's just bad luck you happened upon this.

1 Like

Alright so I think this should work. 2 versions, one that's closer to what you were doing, using a function to filter rows then counting the number of rows in the filtered results (it's always useful to have a filter function anyway),
and another one that's more... direct

v1:
in the script library (note that I have filterrows in a ds_utils script, which is why the function call is ds_utils.filter_rows(...). Put things wherever you want and name them whatever you want, but adapt the paths/names accordingly)

def filter_rows(ds, predicate):
	return system.dataset.toDataSet(
		list(ds.columnNames),
		[row for row in system.dataset.toPyDataSet(ds) if predicate(row)]
	)

def lockout_user_role():
	sessions = system.util.getSessionInfo()
	filtered_ds = ds_utils.filter_rows(sessions, lambda row: row['username'] == "Lockout" and row['address'] == "DESKTOP-XXXXXX")

	if filtered_ds.rowCount > 0:
		system.tag.writeBlocking(['[default]LockoutUserRole'], True)

in the gateway event:

your_script_path.lockout_user_role()

v2:

def lockout_user_role():
	session = system.dataset.toPyDataSet(system.util.getSessionInfo())
	if any(row['username'] == "Lockout" and row['address'] == "DESKTOP-XXXXXX" for row in session):
		system.tag.writeBlocking(['[default]LockoutUserRole'], True)

edit: That's assuming I understood the requirements :X

1 Like

Wow. Very eye opening. I feel like I unlocked a new level doing this task and learning from you guys. This code makes MUCH more sense. Thanks for taking the time to work that out and show me. I will be implementing this, and using project libraries from now on.

1 Like