¿ªÔÆÌåÓý

Locked Scripting question - how to check if sensor or turnout name is valid


 

I've put together a script that takes a sensor or turnout name in from an external program and operates the given sensor or turnout.

This works fine when the sensor or turnout name is valid - I use provideSensor or ProvideTurnout on the passed name, then set the active state.

However, if the external program passes in an invalid sensor or turnout name, then my script crashes out with an invalid name exception when trying to do:

mysensor = sensors.provideSensor( passedSensorName )

or

myturnout = turnotus.provideSensor( passedTurnoutName )

Does anyone have a code snippet to show me how to check if passedSensorName or passedTurnoutName is a valid user name in the sensor or turnout table respectively, before I try the call to provide?

(I'm assuming that Jython still has no way of error trapping and handling in the script)

Thanks,

Pete


 

Pete,

This should catch the error:

try:
? ? sensors.provideSensor('somename')
except jmri.NamedBean.BadSystemNameException:
? ? print 'invalid name'

If you don¡¯t care about the exception type, you can use except:


Dave Sand


On Sep 5, 2019, at 4:18 PM, Pete Brownlow <merg@...> wrote:

I've put together a script that takes a sensor or turnout name in from an external program and operates the given sensor or turnout.

This works fine when the sensor or turnout name is valid - I use provideSensor or ProvideTurnout on the passed name, then set the active state.

However, if the external program passes in an invalid sensor or turnout name, then my script crashes out with an invalid name exception when trying to?do:

mysensor = sensors.provideSensor( passedSensorName )

or

myturnout = turnotus.provideSensor( passedTurnoutName )

Does anyone have a code snippet to show me how to check if passedSensorName or passedTurnoutName is a valid user name in the sensor or?turnout table respectively, before I try the call to provide??

(I'm assuming that Jython still has no way of error trapping and handling in the script)

Thanks,

Pete


 

You can catch and handle exceptions in Jython scripts:



Bob

On Sep 5, 2019, at 2:18 PM, Pete Brownlow <merg@...> wrote:

I've put together a script that takes a sensor or turnout name in from an external program and operates the given sensor or turnout.

This works fine when the sensor or turnout name is valid - I use provideSensor or ProvideTurnout on the passed name, then set the active state.

However, if the external program passes in an invalid sensor or turnout name, then my script crashes out with an invalid name exception when trying to do:

mysensor = sensors.provideSensor( passedSensorName )

or

myturnout = turnotus.provideSensor( passedTurnoutName )

Does anyone have a code snippet to show me how to check if passedSensorName or passedTurnoutName is a valid user name in the sensor or turnout table respectively, before I try the call to provide?

(I'm assuming that Jython still has no way of error trapping and handling in the script)
--
Bob Jacobsen
rgj1927@...


 

By chance is there an "ifExist" function available and a table of existing sensor/turnout names that it could test?


On Thu, Sep 5, 2019, 14:19 Pete Brownlow <merg@...> wrote:
I've put together a script that takes a sensor or turnout name in from an external program and operates the given sensor or turnout.

This works fine when the sensor or turnout name is valid - I use provideSensor or ProvideTurnout on the passed name, then set the active state.

However, if the external program passes in an invalid sensor or turnout name, then my script crashes out with an invalid name exception when trying to do:

mysensor = sensors.provideSensor( passedSensorName )

or

myturnout = turnotus.provideSensor( passedTurnoutName )

Does anyone have a code snippet to show me how to check if passedSensorName or passedTurnoutName is a valid user name in the sensor or turnout table respectively, before I try the call to provide?

(I'm assuming that Jython still has no way of error trapping and handling in the script)

Thanks,

Pete


 

Michael,

Instead of provideSensor() use getSensor() and check for None

chkSensor = sensors.getSensor(¡®BadName¡¯)
If chkSensor is None:
? ? print ¡®it does not exist¡¯

The provideXXX() methods return the item if the name matches a user name or a system name. ?If neither exists, it tries to create a new item using the name as a system name and returns it. ?If the name is not a valid system name or cannot be made to look like one, the name exception occurs.


Dave Sand


On Sep 5, 2019, at 6:55 PM, Michael Piazza via Groups.Io <mpiazza2007@...> wrote:

By chance is there an "ifExist" function available and a table of existing sensor/turnout names that it could test?

On Thu, Sep 5, 2019, 14:19 Pete Brownlow <merg@...> wrote:
I've put together a script that takes a sensor or turnout name in from an external program and operates the given sensor or turnout.

This works fine when the sensor or turnout name is valid - I use provideSensor or ProvideTurnout on the passed name, then set the active state.

However, if the external program passes in an invalid sensor or turnout name, then my script crashes out with an invalid name exception when trying?to do:

mysensor = sensors.provideSensor( passedSensorName )

or

myturnout = turnotus.provideSensor( passedTurnoutName )

Does anyone have a code snippet to show me how to check if passedSensorName or passedTurnoutName is a valid user name in the sensor or?turnout table respectively, before I try the call to provide??

(I'm assuming that Jython still has no way of error trapping and handling in the script)

Thanks,

Pete



Randall Wood
 

There is also the method validateSystemNameFormat that can be used:

name = [...get from external program...] try: sensors.validateSystemNameFormat(name) except jmri.NamedBean.BadSystemNameException: print 'invalid name'

Which will check if the name is potentially valid (i.e. could be used), but will not create a Sensor. Note that this method does not check if the sensor name in question is really associated with a sensor, but merely checks if the name could be used.


 

Thanks all, lots of good ways of doing it there.

I don't know how I missed that Jython has Try-Except - that will be very useful in other situations as well.?? Has it always had that capability?