Best Way to Determine Number of Items in a List Component

I have a window containing a list component. I want to determine, within a script, the number of items in the list. What’s the best way to do this?

I’ve tried something like “len(list.data)” but apparently the dataset doesn’t have the “len” attribute.

First of all, if you’re just trying to display the length, bind a label’s text property to an expression like:

len({Root Container.MyList.data})

Ok, now assuming you have a good reason to get the length of the list’s dataset in a script, you just need to convert it to a PyDataSet first. like this:

data = fpmi.db.toPyDataSet(event.source.parent.getComponent("List").data) theLength = len(data)

Hope this helps,

Thanks for the help. Your code sample did the trick.

I think I have discovered a better way to determine the length of a data set (i.e., number of rows/items in a data set). You can use the following to return a data set’s length:

dataSetLength = dataSet.rowCount

This doesn’t require the data set to be converted to a PyDataSet.

Hope this helps! :smiley:

I thought Carl had that tagline copyrighted :laughing:

1 Like

lol, and I wasn't going to comment... I'm thinking Mickey threw that one out there to say, "You've got nothing on me, Carl" or, equivalently, "I can do everything you can do, better" :wink:

haha, looks like I’ve got some competition! :wink:

Perhaps MickeyBob discovered Jython’s nifty ability to introspect objects, so that you can see exactly what properties/methods they have.

A note on that: We’ve gotten a number of people lately lamenting that it appears that the documentation doesn’t include all functions that are actually available. I just wanted to let you all know that this isn’t necesarilly a mistake! If we were to document all available functions on all objects in FactoryPMI, our documentation would grow by an order of magnitude, and users would get lost in a mire of off-topic functionality. The reason for this lies in how Jython can call into Java functions.

We have picked and chosen what to document from this vast array of functions. Sometimes we miss useful functions, but we’ll try to add them into the documentation as they are pointed out (typically in these forums). The flip side to this is that there are functions which are purposely un-documented, usually because they are deprecated. You may find these using python reflection, but caveat emptor - you might not like the results.

All that said, our scripting documentation, especially as it applies to DataSets and PyDataSets, could certainly be improved.

I definitely wasn’t throwing down the guantlet to Carl. I guess I was just sub-consciously imitating him. But you know what they say about imitation - it’s the sincerest form of complement. I just wish I could tap into his head a little more!

Regarding introspection, I know enough to be dangerous. In this case, I didn’t really use introspection. I just thought that if there was a .columnCount property, there was likely a .rowCount property. I tried it and it worked.

I just happen to have some background in Python, having used it to teach my daughter a computer science class for our home school. I have also used it to develop a simple application that needed to run on both Windows and Linux boxes. I truly like the power and elegance of Python. Not to mention, it’s free. The only major issue that I have is the difficulty of developing a graphical application. All the gui toolkits seem to have a large learning curve and don’t have a good development environment.

I know about the introspection dir() command and how powerful it is in ‘standard’ Python. I tried it but it returned an empty list. Apparently dir() is not supported in Jython, always returning an empty list. I read up on it on the Jython web site FAQ (http://www.jython.org/Project/userfaq.html) where this is addressed, even though I’m not sure I fully understand the answer. I think it boils down to the fact that Jython is a thin veil over Java and there is no way of doing the same introspection of Java as there is of ‘standard’ Python.

Carl, it would be nice if you would give a brief explanation of what you were thinking about when you referred to introspection.

I also want to delve into the subject of leveraging Java from Jython. Could you give some examples, or refer me to some, that would get me started on the basics. I have found some documentation on the web that helped explain classes such as java.awt.color but am having trouble understanding how exactly to use Java classes from Jython. I’ve even ordered the book “Python Programming with the Java™ Class Libraries: A Tutorial for Building Web and Enterprise Applications with Jython” in order to try to learn more.

I’ve also been experimenting to determine which Jython modules are available for import. So far I’ve been successful in importing (but not necessarily using) the following modules (from list at http://docs.python.org/lib/modindex.html):

binascii
calendar
operator
bisect
colorsys
copy

I know the following modules won’t import:

functools
itertools
email
array
binhex
cmath
anydbm
bz2
cgi
collections
datetime

Is there a list of supported Jython modules?

Obviously, I’m a little curious. Sorry! :blush:

Thats called the TIAS method of teaching yourself computers. "Try It And See" - its very effective!

Awesome! Python is a very elegant language, wonderful for an introductory course for programming.

dir() does work in Jython, but only on actual Python objects. Much of what you deal with in FactoryPMI are actually Java objects, as you suspected. These Java objects don't define a Python-esque "dict" as their namespace, which is why dir() doesn't work. Their class object does, however. Try something like this:

data = event.source.parent.getComponent("Table").data print dir(data.__class__)

WARNING: You will find functions that you will be tempted to use, such as being able to actualy modify a data set directly. Don't use these. Bad things will happen. The tips on the rest fo the forum about dealing with datasets are the only safe way to do it. Don't call us about how you messed up a screen you worked on for hours because you used functions you weren't supposed to (unlikely, but I'm trying to make a point...)

Go get the book Jython Essentials published by O'Reilly. Using Java in Jython is really easy. (assuming you know the Java standard library) You'll find some examples throughout the forums. If you have a specific need, feel free to ask.

Look at this thread: inductiveautomation.com/foru ... php?t=2775

One more thing - I like to think of these forums as an extension of the documentation. A knowledge base, if you will. If you want to earn brownie points with us, use the forum (as opposed to a phone call or an email). That way, your question and its answer can benefit everyone.

Hope this helps™

Ugh oh…

[quote=“Carl.Gould”]
Hope this helps[size=200][/size][/quote]

Ok, I hear your warning about using methods listed with the dir() command, but which are off limits and which are appropriate to use? Is there a way to tell or is it “buyer beware”? How can you tell if a method is deprecated?

This probably doesn’t help™. :wink:

You can’t. If it’s not documented, ask first.