use of org.python.core.PyObject in project Pogamut3 by kefik.
the class PyDictionaryWrapper method getJavaObject.
@Override
public Object getJavaObject(Object pyObject) {
if (!(pyObject instanceof PyDictionary))
throw new IllegalArgumentException("pyObject is not instance of PyDictionary");
final PyDictionary pyDict = (PyDictionary) pyObject;
Map map = new HashMap();
PyList keys = pyDict.keys();
int count = keys.__len__();
PyObject pyKey, pyValue;
Object javaKey, javaValue;
for (int i = 0; i < count; ++i) {
pyKey = keys.__getitem__(i);
javaKey = PyObjectWrappersManager.getWrapper(pyKey.getClass()).getJavaObject(pyKey);
pyValue = pyDict.__getitem__(pyKey);
javaValue = PyObjectWrappersManager.getWrapper(pyValue.getClass()).getJavaObject(pyValue);
map.put(javaKey, javaValue);
}
return map;
}
use of org.python.core.PyObject in project Pogamut3 by kefik.
the class PyListWrapper method getChildren.
@Override
public ArrayList<PyObjectAdapter> getChildren(Object object) {
if (!(object instanceof PyList))
throw new IllegalArgumentException("object is not instance of PyList");
final PyList pyList = (PyList) object;
int count = (pyList.__len__());
ArrayList<PyObjectAdapter> list = new ArrayList<PyObjectAdapter>(count);
PyObject obj;
for (int i = 0; i < count; ++i) {
final int place = i;
obj = pyList.__finditem__(place);
list.add(new PyObjectAdapter(String.valueOf(place), new PyObjectPlace() {
private int myPlace = place;
@Override
public void set(PyObject newValue) {
pyList.__setitem__(myPlace, newValue);
}
@Override
public PyObject get() {
try {
return pyList.__finditem__(myPlace);
} catch (Exception e) {
return null;
}
}
}));
}
return list;
}
use of org.python.core.PyObject in project Pogamut3 by kefik.
the class PyListWrapper method getJavaObject.
@Override
public Object getJavaObject(Object pyObject) {
if (!(pyObject instanceof PyList))
throw new IllegalArgumentException("pyObject is not instance of PyList");
PyList pyList = (PyList) pyObject;
int count = (pyList.__len__());
ArrayList list = new ArrayList(count);
PyObject obj = null;
PyObjectWrapper wrapper = null;
for (int i = 0; i < count; ++i) {
obj = pyList.__getitem__(i);
wrapper = PyObjectWrappersManager.getWrapper(obj.getClass());
list.add(wrapper.getJavaObject(obj));
}
return list;
}
use of org.python.core.PyObject in project sponge by softelnet.
the class JythonKnowledgeBaseInterpreter method scanToAutoEnable.
@Override
public void scanToAutoEnable() {
PyScriptEngineScope scope = eval("globals()");
List<PyType> processorPyTypes = getProcessorClasses().values().stream().map(cls -> (PyType) Py.java2py(cls)).collect(Collectors.toList());
List<Object> autoEnabled = new ArrayList<>();
SpongeUtils.stream(((ScopeIterator) scope.__iter__()).iterator()).forEachOrdered(element -> {
String name = element.toString();
PyObject pyObject = scope.__finditem__(name);
// Java-based processor classes (extending PyJavaType) are not auto-enabled.
if (pyObject != null && pyObject instanceof PyType && !(pyObject instanceof PyJavaType)) {
PyType pyType = (PyType) pyObject;
boolean extendsProcessorType = processorPyTypes.stream().filter(processorClass -> !pyType.equals(processorClass) && pyType.isSubType(processorClass)).findFirst().isPresent();
if (extendsProcessorType && !isProcessorAbstract(name)) {
autoEnabled.add(name);
((JythonKnowledgeBaseEngineOperations) getEngineOperations()).enable(pyType);
}
}
});
if (logger.isDebugEnabled() && !autoEnabled.isEmpty()) {
logger.debug("Auto-enabling: {}", autoEnabled);
}
}
use of org.python.core.PyObject in project jans by JanssenProject.
the class CustomScriptManager method createExternalTypeFromStringWithPythonException.
public BaseExternalType createExternalTypeFromStringWithPythonException(CustomScript customScript, Map<String, SimpleCustomProperty> configurationAttributes) throws Exception {
String script = customScript.getScript();
String scriptName = StringHelper.toLowerCase(customScript.getName()) + ".py";
if (script == null) {
return null;
}
CustomScriptType customScriptType = customScript.getScriptType();
BaseExternalType externalType = null;
InputStream bis = null;
try {
bis = new ByteArrayInputStream(script.getBytes("UTF-8"));
externalType = pythonService.loadPythonScript(bis, scriptName, customScriptType.getPythonClass(), customScriptType.getCustomScriptType(), new PyObject[] { new PyLong(System.currentTimeMillis()) });
} catch (UnsupportedEncodingException e) {
log.error(String.format("%s. Script inum: %s", e.getMessage(), customScript.getInum()), e);
} finally {
IOUtils.closeQuietly(bis);
}
if (externalType == null) {
return null;
}
boolean initialized = false;
try {
if (externalType.getApiVersion() > 10) {
initialized = externalType.init(customScript, configurationAttributes);
} else {
initialized = externalType.init(configurationAttributes);
log.warn(" Update the script's init method to init(self, customScript, configurationAttributes)", customScript.getName());
}
} catch (Exception ex) {
log.error("Failed to initialize custom script: '{}'", ex, customScript.getName());
}
if (initialized) {
return externalType;
}
return null;
}
Aggregations