org.python.util.PythonInterpreter.compile()方法的使用及代码示例

x33g5p2x  于2022-01-26 转载在 其他  
字(5.0k)|赞(0)|评价(0)|浏览(160)

本文整理了Java中org.python.util.PythonInterpreter.compile()方法的一些代码示例,展示了PythonInterpreter.compile()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。PythonInterpreter.compile()方法的具体详情如下:
包路径:org.python.util.PythonInterpreter
类名称:PythonInterpreter
方法名:compile

PythonInterpreter.compile介绍

[英]Compiles a string of Python source as either an expression (if possible) or a module. Designed for use by a JSR 223 implementation: "the Scripting API does not distinguish between scripts which return values and those which do not, nor do they make the corresponding distinction between evaluating or executing objects." (SCR.4.2.1)
[中]将Python源代码的字符串编译为表达式(如果可能)或模块。设计供JSR 223实现使用:“脚本API不区分返回值的脚本和不返回值的脚本,也不区分评估或执行对象。”(第4.2.1条)

代码示例

代码示例来源:origin: org.python/jython

/**
 * Compiles a string of Python source as either an expression (if possible) or a module.
 *
 * Designed for use by a JSR 223 implementation: "the Scripting API does not distinguish between
 * scripts which return values and those which do not, nor do they make the corresponding
 * distinction between evaluating or executing objects." (SCR.4.2.1)
 */
public PyCode compile(String script) {
  return compile(script, "<script>");
}

代码示例来源:origin: usc-isi-i2/Web-Karma

private PyCode compile(PythonInterpreter interpreter, String statement) {
  return interpreter.compile(statement);
}

代码示例来源:origin: org.python/jython

public PyCode compile(Reader reader) {
  return compile(reader, "<script>");
}

代码示例来源:origin: langurmonkey/gaiasky

public PyCode compileJythonScript(String script) throws Exception {
  return interpreter.compile(script);
}

代码示例来源:origin: org.python/jython

public PyCode compile(String script, String filename) {
  return compile(new StringReader(script), filename);
}

代码示例来源:origin: langurmonkey/gaiasky

public PyCode compileJythonScript(File script) throws Exception {
  return interpreter.compile(new FileReader(script));
}

代码示例来源:origin: org.apache.apex/malhar-contrib

@Override
public void setup(OperatorContext context)
{
 for (String s : setupScripts) {
  interp.exec(s);
 }
 code = interp.compile(script);
}

代码示例来源:origin: org.python/jython

private PyCode compileScript(Reader reader, ScriptContext context) throws ScriptException {
  try {
    String filename = (String) context.getAttribute(ScriptEngine.FILENAME);
    if (filename == null) {
      return interp.compile(reader);
    } else {
      return interp.compile(reader, filename);
    }
  } catch (PyException pye) {
    throw scriptException(pye);
  }
}

代码示例来源:origin: org.fujion/fujion-script-jython

public ParsedScript(String source) {
  try (PythonInterpreter interp = new PythonInterpreter()) {
    script = interp.compile(source);
  }
}

代码示例来源:origin: org.python/jython

private PyCode compileScript(String script, ScriptContext context) throws ScriptException {
  try {
    String filename = (String) context.getAttribute(ScriptEngine.FILENAME);
    if (filename == null) {
      return interp.compile(script);
    } else {
      return interp.compile(script, filename);
    }
  } catch (PyException pye) {
    throw scriptException(pye);
  }
}

代码示例来源:origin: org.carewebframework/org.carewebframework.web.script.jython

public ParsedScript(String source) {
  try (PythonInterpreter interp = new PythonInterpreter()) {
    script = interp.compile(source);
  }
}

代码示例来源:origin: apache/apex-malhar

@Override
public void setup(OperatorContext context)
{
 for (String s : setupScripts) {
  interp.exec(s);
 }
 code = interp.compile(script);
}

代码示例来源:origin: stackoverflow.com

import org.python.core.PyException;
import org.python.core.PyCode;
import org.python.util.PythonInterpreter;

public class Main {
 public static void main(String[] args) throws PyException {

  PythonInterpreter interp = new PythonInterpreter();
  System.out.println("ā".codePointAt(0));    // outputs 257
  interp.exec("print ord('ā')");             // outputs 63

  String s = "print ord(u'ā')";
  PyCode code = interp.compile(s);
  interp.exec(code);                         // outputs 257
 }
}

代码示例来源:origin: co.cask.hydrator/core-plugins

private void init(@Nullable TransformContext context) {
 interpreter = new PythonInterpreter();
 interpreter.set(CONTEXT_NAME, new ScriptContext(
  logger, metrics,
  new LookupProvider() {
   @Override
   public <T> Lookup<T> provide(String s, Map<String, String> map) {
    throw new UnsupportedOperationException("lookup is currently not supported.");
   }
  },
  null,
  new JavaTypeConverters() {
   @Override
   public Object mapToJSObject(Map<?, ?> map) {
    return null;
   }
  },
  context == null ? null : context.getArguments()));
 // this is pretty ugly, but doing this so that we can pass the 'input' record into the transform function.
 // that is, we want people to implement
 // def transform(input, emitter, context): ...
 // rather than def transform(): ...  and have them access the input, emitter, and context via global variables
 String script = String.format("%s\ntransform(%s, %s, %s)",
                config.script, INPUT_STRUCTURED_RECORD_VARIABLE_NAME,
                EMITTER_VARIABLE_NAME, CONTEXT_NAME);
 compiledScript = interpreter.compile(script);
 if (config.schema != null) {
  schema = parseJson(config.schema);
 }
}

代码示例来源:origin: scijava/scijava-jupyter-kernel

/**
   * @param args the command line arguments
   */
  public static void main(String[] args) throws PyException {

    PythonInterpreter interp = new PythonInterpreter();

    Object result = interp.eval(interp.compile("p=999\n555")).__tojava__(Object.class);
    System.out.println(result);

    interp = new PythonInterpreter();

    result = interp.eval(interp.compile("555")).__tojava__(Object.class);
    System.out.println(result);
    
  }
}

相关文章