Expression to get value from dataset

Very simple question. I’m sure the answer is simple too.

Is there an expression function to read a value from a dataset? Something like getValueAt in Python.

Thanks.

You can use square brackets to read values from a dataset in expressions. The following will read the “Id” column of row 0.

{Root Container.myTable.data}[0, "Id"]

And yes, it also baffles me why this is possible in expressions but not in python…

Extra hint: to read the selected row, it’s best to wrap it in a try clause and give a default return value. That way, you don’t get errors when no row is selected. The following expression will give -1 when no row is selected.

try({Root Container.myTable.data}[{Root Container.myTable.selectedRow}, "Id"], -1)
7 Likes

Very good. Thank you!

I mean, it wouldn't be valid syntax in Python? Subscript keys can only be a single object. dataset[(0, "Id")] seems pretty silly to me. If you want more Pythonic access, use a pydataset - dataset[0]["id"]?

It is possible, but not default, for a python object’s __getitem__ and __setitem__ to handle a tuple. So a custom jython wrapper for Dataset could be registered to support such syntax. If such a wrapper implemented the complete sequence interface, the PyDataset type would no longer be necessary.

Yeah, but registering a wrapper for Dataset could instantly break basically every script anyone's ever written...high regression risk for relatively minor syntax gain (IMO).

FWIW, I have considered injecting such a wrapper in my Simulation Aids module, similar to the way I made PyComponentWrapper uniform across the Swing side of the platform. If I did so, all java objects that implement the dataset interface would suddenly have pythonic semantics everywhere in the entire platform.

1 Like

Only if you are already registering a custom wrapper for datasets. What I have in mind would preserve the automatic java-jython wrapper's functionality for each implementing class. The only exception would be where Dataset implementations also implemented python's sequence interface.

1 Like

Actually, it's the comma that makes the tuple, not the parentheses. So dataset[0, "Id"] is the same as dataset[(0, "Id")].

But even dataset[0]["Id"] would look nicer to me.

TIL you can do that in subscript expressions. Neat. Something like @pturmel suggested might happen (in the future) but not anytime soon.

Yes, that's what I meant.

Nah. But it is compatible with slice notation.

Tips:
Add a little air into your expressions to make them easier to read and format code on the forum using the </> button.

'Shipped Pallets data : ' 
+ {value} + ' Pallets, '
+ len({.../Table.props.data}["LOT"]) + ' LOT, '
+ len({.../Table.props.data}["OrderKey"]) + ' Order.'

will be much easier to read and debug and anyone maintaining the code after you will appreciate it. The expression language ignores line breaks and spaces outside of functions, etc.

1 Like