use of org.python.core.PyInteger in project freemarker by apache.
the class JythonModelCache method create.
@Override
protected TemplateModel create(Object obj) {
boolean asHash = false;
boolean asSequence = false;
JythonVersionAdapter versionAdapter = JythonVersionAdapterHolder.INSTANCE;
if (versionAdapter.isPyInstance(obj)) {
Object jobj = versionAdapter.pyInstanceToJava(obj);
// FreeMarker-aware, Jython-wrapped Java objects are left intact
if (jobj instanceof TemplateModel) {
return (TemplateModel) jobj;
}
if (jobj instanceof Map) {
asHash = true;
}
if (jobj instanceof Date) {
return new DateModel((Date) jobj, BeansWrapper.getDefaultInstance());
} else if (jobj instanceof Collection) {
asSequence = true;
// doesn't support sets.
if (!(jobj instanceof List)) {
obj = new ArrayList((Collection) jobj);
}
}
}
// If it's not a PyObject, first make a PyObject out of it.
if (!(obj instanceof PyObject)) {
obj = Py.java2py(obj);
}
if (asHash || obj instanceof PyDictionary || obj instanceof PyStringMap) {
return JythonHashModel.FACTORY.create(obj, wrapper);
}
if (asSequence || obj instanceof PySequence) {
return JythonSequenceModel.FACTORY.create(obj, wrapper);
}
if (obj instanceof PyInteger || obj instanceof PyLong || obj instanceof PyFloat) {
return JythonNumberModel.FACTORY.create(obj, wrapper);
}
if (obj instanceof PyNone) {
return null;
}
return JythonModel.FACTORY.create(obj, wrapper);
}
use of org.python.core.PyInteger in project apex-malhar by apache.
the class PythonOperatorTest method testJavaOperator.
@SuppressWarnings({ "rawtypes", "unchecked" })
@Test
public void testJavaOperator() {
PythonOperator oper = new PythonOperator();
String setupScript = "import operator\n";
setupScript += "def square():\n";
setupScript += " return val*val\n\n";
oper.addSetupScript(setupScript);
oper.setScript("square()");
oper.setPassThru(true);
CollectorTestSink sink = new CollectorTestSink();
oper.result.setSink(sink);
HashMap<String, Object> tuple = new HashMap<String, Object>();
tuple.put("val", new Integer(2));
oper.setup(null);
oper.beginWindow(0);
oper.inBindings.process(tuple);
oper.endWindow();
Assert.assertEquals("number emitted tuples", 1, sink.collectedTuples.size());
for (Object o : sink.collectedTuples) {
PyInteger val = (PyInteger) o;
Assert.assertEquals("emitted should be 4", new Integer(4), (Integer) val.__tojava__(Integer.class));
}
}
Aggregations