Here is what I have been using to test if a variable is a dict like object or list like object (lifted from here):
I'm curious if others have recommendation for better/preferred "Duck typing" methodology to detect a dict like objects or list like objects.
I ran into the issue with DotReferenceJythonMap
types when I used a binding on a Perspective view custom property to create a list of dicts. I then tried to reference each dict in other property bindings with a script transform, attempting to merge with other programmatically created dicts. My custom method to merge dictionaries failed in some weird ways due to the DotReferenceJythonMap
type.
Here is my previous custom merge function (lifted from here) that uses type checking.
def merge(d1, d2):
import collections
for k,v2 in d2.items():
v1 = d1.get(k) # returns None if v1 has no value for this key
if ( isinstance(v1, collections.Mapping) and
isinstance(v2, collections.Mapping) ):
merge(v1, v2)
else:
d1[k] = v2
return d1
Here is my revised custom merge function that uses "duck typing".
def merge_test(d1, d2):
for k,v2 in d2.items():
v1 = d1.get(k)
if ( 'iteritems' in dir(v1) and 'iteritems' in dir(v2) ):
merge(v1, v2)
else:
d1[k] = v2
return d1
Not sure if my examples are ideal (I welcome feedback) but the thread was light on actual examples so I thought I'd put these out there..
One last note... Notably the system.util.jsonEncode()
function does not handle the object type systemcom.inductiveautomation.perspective.gateway.script.DotReferenceJythonMap
type, nor does it handle many (any) of the other dict/list like wrapper objects Ignition like to create.