Using the library in Jython 2.5

In case anyone else runs into an operator.methodcaller issue, I’ve attached a quick fix.

Here’s the story:
When trying to load a third party python library into Ignition, the Jython compiler was dying on a call in (which I also added from the Cheese Shop). The problem was that operator didn’t have methodcaller in it. Some searching showed that Jython 2.5 had a bug causing this, and a patch was submitted with a fix. Unfortunately, patching Jython is a bit out of my league.

But monkey patching isn’t. Since I didn’t know what the code should be, I went to the source on implementing python in python: PyPy. A quick search of the PyPy source code yeilded the following block of code

class methodcaller(object):
    def __init__(self, method_name, *args, **kwargs):
        self._method_name = method_name
        self._args = args
        self._kwargs = kwargs

    def __call__(self, obj):
        return getattr(obj, self._method_name)(*self._args, **self._kwargs)

So that code, immediately followed by

operator.methodcaller = methodcaller

and plopped directly after the imports in patched it.

This has the nice feature that a lot of super handy Python libraries use, but there’s nothing too complicated going on here to make it work. Cheers! (29.5 KB)

Cool, thanks!