Adding a function to a Region Object

Asked by Ned

Hello,

At the moment I have a function called clickLog that clicks on a supplied image and wraps a Try Except around the click function and takes a screenshot if the image cannot be found.

I cannot use this with a region and so have to use region.click

I am just wondering if it would be possible to call region.clickLog, that is to make that function available to all Region objects? I understand that in the future there will be the use() method, but am just curious if the above would be worth pursuing in the meantime?

Thank you,
Ned

Question information

Language:
English Edit question
Status:
Solved
For:
SikuliX Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
Last query:
Last reply:
Revision history for this message
Best RaiMan (raimund-hocke) said :
#1

In Jython we do not have the possibility like in JRuby to dynamically add/modify functions/methods in existing classes, which would make your idea rather easy.

So the only possibility left with Jython is to use a wrapper Region class, that has the modified/added methods and internally delegates to the original Region class if needed.

class myRegion(Region):
    # define what is needed

The "problem" with Sikuli:
- you always have to use the myRegion class to create new Region objects, to get your features
- some Sikuli features, that internally create new Regions on the fly, might not reflect your Region version

The easiest way of course is to modify the already existing Region wrapper in Lib/Sikuli/Region.py

In version 1.1.0 you get the Lib folder automatically, when you select option 2 (it is contained in the jars sikulix and sikulixapi and is exported, to allow support for coding assistance in IDE's like Eclipse, NetBeans, ...).

To activate your modified Lib folder, you have to modify the sys.path at runtime accordingly.

Another option would be, to unjar sikulix.jar, replace the folder and build the jar again with the jar utility from the JDK.

Since these features (export and build a jar from scratch) are already contained in SikuliX for internal uses at setup and for other cases, you might as well use these features, to get the Lib folder from sikulix.jar and rebuilt sikulix.jar as needed.
Come back, if you are interested.

I just decided, to add the latter to sikulixsetup as an option.

Be aware: once you start to modify the existing SikuliX artefacts, you have to be very careful when upgrading to new SikuliX versions and might have the need to revise/reimplement your features.

Your clickLog feature:
- the use feature will not help with your clickLog function (see above)
- in version 1.2 I will completely revise the logging feature, add better FindFailed handling and provide the possibility to access the last used screenshot at time of search for logging or other purposes

Revision history for this message
Ned (nedleonard) said :
#2

After a sleep I realised that I should have just passed the sRegion into the clickLog wrapper.. Thanks for the reply! :)

Revision history for this message
Ned (nedleonard) said :
#3

Thanks RaiMan, that solved my question.

Revision history for this message
RaiMan (raimund-hocke) said :
#4

Uuuups, I did not dare to ask about your wrapper function, since I supposed, you already did it this way, before having a sleep ;-)