Skip to content

Instantly share code, notes, and snippets.

@yssharma
Created February 9, 2015 17:32
Show Gist options
  • Save yssharma/bded704f4e5c992a4e66 to your computer and use it in GitHub Desktop.
Save yssharma/bded704f4e5c992a4e66 to your computer and use it in GitHub Desktop.
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.drill.exec.scripting.jython;
import org.apache.drill.exec.expr.DrillSimpleFunc;
import org.apache.drill.exec.record.RecordBatch;
import org.python.core.PyInstance;
import org.python.util.PythonInterpreter;
public class JythonSimpleFunction implements DrillSimpleFunc {
PythonInterpreter interpreter;
String scriptPath;
String funcName;
PyInstance instance;
public JythonSimpleFunction(String scriptPath, String funcName){
this.scriptPath = scriptPath;
this.funcName = funcName;
PythonInterpreter.initialize(System.getProperties(), System.getProperties(), new String[0]);
this.interpreter = new PythonInterpreter();
this.execfile(scriptPath);
instance = this.createClass(funcName, "None");
}
private void execfile(final String fileName) {
this.interpreter.execfile(fileName);
}
private PyInstance createClass( final String className, final String opts ) {
return (PyInstance) this.interpreter.eval(className + "(" + opts + ")");
}
@Override
public void setup(RecordBatch incoming) {
instance.invoke("setup");
}
@Override
public void eval() {
instance.invoke("eval");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment