All Downloads are FREE. Search and download functionalities are using the official Maven repository.

water.udf.JythonCFuncLoader Maven / Gradle / Ivy

There is a newer version: 3.46.0.6
Show newest version
package water.udf;

import org.python.core.PySystemState;

/**
 * Custom function loader, which can instantiate
 * functions written in Python.
 *
 * The provider internally uses Jython.
 *
 * Note: Jython caches the loaded python programs. That means
 * changing underlying function definition (i.e, Python code) is not
 * reflected!
 */
public class JythonCFuncLoader extends CFuncLoader {

  @Override
  public String getLang() {
    return "python";
  }

  @Override
  public  F load(String jfuncName, Class targetKlazz, ClassLoader classLoader) {
    int idxLastDot = jfuncName.lastIndexOf('.');
    String module = jfuncName.substring(0, idxLastDot);
    String clsName = jfuncName.substring(idxLastDot+1);

    PySystemState pySystemState = new PySystemState();
    ClassLoader savedCtxCl = Thread.currentThread().getContextClassLoader();
    try {
      Thread.currentThread().setContextClassLoader(classLoader);
      return new JythonObjectFactory(pySystemState, targetKlazz, module, clsName).createObject();
    } finally {
      Thread.currentThread().setContextClassLoader(savedCtxCl);
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy