Type Assertion and built in Ignition Types

Hello All!

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.

Try:

from com.inductiveautomation.ignition.common.script.builtin import DatasetUtilities
maquinas = system.db.runQuery("SELECT 1", "")
listo = []

print type(listo)
if type(listo) == list:
	print "List!!"	
else:
	print "Not a list!"

if type(maquinas) == DatasetUtilities.PyDataSet:
	print "Machine!!"	
else:
	print "Not a Machine!!"

No dice.

It can’t resolve the “DatasetUtilities” typepath.

It is case-sensitive so if you have “DataSetUtilities” it’ll fail to resolve. Should be “DatasetUtilities”.

Otherwise it could be a version issue, I tested on 7.9.6.

My apologies, I had it typed right in the machine console.

NameError: name 'DatasetUtilities' is not defined

And the import is in there within the scope of your script?

I believe it will fail at the import call with a different message if it truly cannot find the class within the class path.

Something like:
ImportError: cannot import name DatasetUtilities

I haven’t imported it… I will try that and follow up later today.

Thanks!

Nope! Throws an error, saying it can’t be found just the same.

What version of Ignition are you using? The class com.inductiveautomation.ignition.common.script.builtin.DatasetUtilities has existed at least since v7.7.

Ayup! That did the trick.

Not used to having to give the verbose version of the class name, but I suppose Jython forces the issue.

Thanks pturmel!

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. }

No I meant on the type assertion. I had typed the entire package and that was what made it work.

The first line that had

from XXXXX(*32) import YYYY

It didn’t have the color call out I’m used to reading for imports, so I glanced through it as text.

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'

Please let me know if this is a bad practice.

1 Like

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.

That's good to know thanks!

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.

Thanks

You put "DatasetUtilities" twice. Try copying and pasting the first line of phillip.bow's code.

1 Like

Great, Thank You!
This is what works:

from com.inductiveautomation.ignition.common.script.builtin import DatasetUtilities
from com.inductiveautomation.ignition.common import BasicDataset

basicdataset = system.dataset.toDataSet(,)
pidataset = system.dataset.toPyDataSet(basicdataset)
print type(basicdataset)
print type(pidataset)
if type(basicdataset) == BasicDataset:
 print 'Successfully detected a BasicDataset'
if type(pidataset) == DatasetUtilities.PyDataSet
 print 'Successfully detected a PyDataSet'