I’m looking at checking for a specific type of class within a Scripting package in Python and would like some error checking and type checking built-in (at least during development)
So in the past I would use assert and isType for this purpose, making sanity checks for passed objects to the library.
I’m running into a wall when trying to specify PyDataSets as types though.
maquinas = system.db.runPrepQuery(machq, [2],'tablas')
listo = []
print type(listo)
if type(listo) == list:
print "List!!"
else:
print "Not a list!"
print type(maquinas)
if type(maquinas) == com.inductiveautomation.ignition.common.script.builtin.DatasetUtilities$PyDataSet:
print "Machine!!"
else:
print "Not a Machine!!"
The second example seems to break the function.
Any direction would be great.I can definitely work around this (and I will) but it’s going to be a nagging voice in the back of my head I’d like to quell.
What version of Ignition are you using? The class com.inductiveautomation.ignition.common.script.builtin.DatasetUtilities has existed at least since v7.7.
In what language can you avoid spelling out the class/package name, at least as an import?
{ Hint: Look at the first line of phillip.bow’s suggested code. }
I use an empty dataset and type to type comparison which eliminates the need for the lengthy spelling or imports and hopefully will survive updates to root object types.
if type(possibleDatasetSource) == type( system.dataset.toDataSet([],[]) ):
print ‘you have a dataset’
else:
print ‘not a dataset’
It is a bad practice. Your sample will only match a BasicDataset, the output type of system.dataset.toDataSet(). There are other classes that implement the Dataset interface that Ignition can deliver at any point. It’s also slow as death as you are creating and throwing away an empty dataset every time you check.
Actually, in my code, I have a reliable, known dataset variable of the type I’m expecting to get so I don’t have the slowness of creating and throwing away a dataset but that will not always be the case.
I’ve fiddled with several different options from other posts but can’t seem to get the right syntax so the type compare has bailed me out.
I’ve copied from this thread:
from com.inductiveautomation.ignition.common.script.builtin.DatasetUtilities import DatasetUtilities
but get:
ImportError: cannot import name DatasetUtilities
When I import the entire library I still get errors:
import com.inductiveautomation.ignition.common.script.builtin.DatasetUtilities
try:
if type(dsType) == DataSetUtilities.PyDataSet:
print ‘DataSetUtilities worked’
except:
print ‘error using DataSetUtilities.PyDataSet’
try:
if type(dsType) == DatasetUtilities.PyDataSet:
print ‘DatasetUtilities worked’
except:
print ‘error using DatasetUtilities.PyDataSet’
I appreciate the simple type validation of other data types, e.g.
d = {‘abc’:123,‘xyz’:789}
if type(d) == dict:
print ‘you have a dict’
l = [‘a’,‘b’,‘c’]
if type(l) == list:
print ‘you have a list’
and I would really appreciate an actual working example that would help me know when I have a legitimate dataset.