Passing a list into runScript() expression binding

Given @pturmel's feedback, I think I'll update my library to a single function:

def unQualify(qvObject):
	if qvObject is None:
		return None
	if isinstance(qvObject, list):
		return [val.value for val in qvObject]
	elif isinstance(qvObject, dict):
		return dict([(k, qv.value) for (k, qv) in qvObject.items()])

Not a bad plan, but that implementation will not handle java implementations of lists and dictionaries. Consider checking for the items method (via hasattr()) to trigger dictionary handling, then iterating over the rest. (Duck typing is more reliable than isinstance().

2 Likes

Thanks, that explains why it wasn't working. This does:

def unQualify(qvObject):
	if qvObject is None:
		return None
	if hasattr(qvObject, 'items'):
		return dict([(k, qv.value) for (k, qv) in qvObject.items()])
	else:
		return [val.value for val in qvObject]

You don't need the brackets, you can use a generator here. And if the java dicts implement .iteritems (I don't remember if they do), you could also use that, so no list is produced while you extract the value from the qv.

2 Likes

This does appear to work:

def extractQualifiedValues(qvObject):
	if qvObject is None:
		return None
	if hasattr(qvObject, 'iteritems'):
		return dict((k, qv.value) for (k, qv) in qvObject.iteritems())
	else:
		return [val.value for val in qvObject]

I am interested in making it as efficient as possible, since that's the main reason for using runScript. Maybe one day I'll finally start using @pturmel's Toolkit...

2 Likes

Please don't call it unQualify(), as it is not a 1:1 replacement for mine. My toolkit recursively handles nested lists and dictionaries, with cycle detection to avoid infinite loops.

1 Like