use of org.python.core.PyObject in project myrobotlab by MyRobotLab.
the class Python method createPythonInterpreter.
/**
*/
public void createPythonInterpreter() {
// TODO: If the username on windows contains non-ascii characters
// the Jython interpreter will blow up.
// The APPDATA environment variable contains the username.
// as a result, jython sees the non ascii chars and it causes a utf-8
// decoding error.
// overriding of the APPDATA environment variable is done in the agent
// as a work around.
// work around for 2.7.0
// http://bugs.jython.org/issue2355
// ??? - do we need to extract {jar}/Lib/site.py ???
Properties props = new Properties();
/*
* Used to prevent: console: Failed to install '':
* java.nio.charset.UnsupportedCharsetException: cp0.
*/
props.put("python.console.encoding", "UTF-8");
/*
* don't respect java accessibility, so that we can access protected
* members on subclasses
*/
props.put("python.security.respectJavaAccessibility", "false");
props.put("python.import.site", "false");
Properties preprops = System.getProperties();
PythonInterpreter.initialize(preprops, props, new String[0]);
interp = new PythonInterpreter();
PySystemState sys = Py.getSystemState();
if (modulesDir != null) {
sys.path.append(new PyString(modulesDir));
}
log.info("Python System Path: {}", sys.path);
String selfReferenceScript = "from org.myrobotlab.service import Runtime\n" + "from org.myrobotlab.service import Python\n" + String.format("%s = Runtime.getService(\"%s\")\n\n", getSafeReferenceName(getName()), getName()) + "Runtime = Runtime.getInstance()\n\n" + String.format("myService = Runtime.getService(\"%s\")\n", getName());
PyObject compiled = getCompiledMethod("initializePython", selfReferenceScript, interp);
interp.exec(compiled);
}
use of org.python.core.PyObject in project scisoft-core by DawnScience.
the class PythonUtils method convertPySlicesToSlice.
/**
* Convert an array of python slice objects to a slice array
*
* @param indexes
* @param shape
* @return slice array
*/
private static SliceData convertPySlicesToSlice(final PyObject indexes, final int[] shape) {
PyObject[] indices = indexes instanceof PySequenceList ? ((PySequenceList) indexes).getArray() : new PyObject[] { indexes };
int orank = shape.length;
// count new axes
int na = 0;
// count collapsed dimensions
int nc = 0;
// count slices or ellipses
int ns = 0;
boolean hasEllipsis = false;
for (int j = 0; j < indices.length; j++) {
PyObject index = indices[j];
if (index instanceof PyNone) {
na++;
} else if (index instanceof PyInteger) {
nc++;
} else if (index instanceof PySlice) {
ns++;
} else if (index instanceof PyEllipsis) {
if (hasEllipsis) {
throw new IllegalArgumentException("Only one ellipsis is allowed");
}
hasEllipsis = true;
ns++;
} else {
throw new IllegalArgumentException("Unsupported object in index: " + index);
}
}
// number of spare dimensions
int spare = orank - nc - ns;
SliceND slice = new SliceND(shape);
hasEllipsis = false;
// flag which dimensions are sliced
boolean[] sdim = new boolean[orank];
// new axes
int[] axes = new int[na];
int i = 0;
// new axes
int a = 0;
// collapsed dimensions
int c = 0;
for (int j = 0; i < orank && j < indices.length; j++) {
PyObject index = indices[j];
if (index instanceof PyEllipsis) {
sdim[i++] = true;
if (!hasEllipsis) {
// pad out with full slices on first ellipsis
hasEllipsis = true;
for (int k = 0; k < spare; k++) {
sdim[i++] = true;
}
}
} else if (index instanceof PyInteger) {
int n = ((PyInteger) index).getValue();
if (n < -shape[i] || n >= shape[i]) {
throw new PyException(Py.IndexError);
}
if (n < 0) {
n += shape[i];
}
// nb specifying indexes whilst using slices will reduce rank
sdim[i] = false;
slice.setSlice(i++, n, n + 1, 1);
c++;
} else if (index instanceof PySlice) {
PySlice pyslice = (PySlice) index;
sdim[i] = true;
Slice nslice = convertToSlice(pyslice);
slice.setSlice(i++, nslice.getStart(), nslice.getStop(), nslice.getStep());
} else if (index instanceof PyNone) {
// newaxis
axes[a++] = (hasEllipsis ? j + spare : j) - c;
} else {
throw new IllegalArgumentException("Unexpected item in indexing");
}
}
assert nc == c;
while (i < orank) {
sdim[i++] = true;
}
while (a < na) {
axes[a] = i - c + a;
a++;
}
int[] sShape = slice.getShape();
int[] newShape = new int[orank - nc];
i = 0;
for (int j = 0; i < orank; i++) {
if (sdim[i]) {
newShape[j++] = sShape[i];
}
}
if (na > 0) {
int[] oldShape = newShape;
newShape = new int[newShape.length + na];
i = 0;
for (int k = 0, j = 0; i < newShape.length; i++) {
if (k < na && i == axes[k]) {
k++;
newShape[i] = 1;
} else {
newShape[i] = oldShape[j++];
}
}
}
SliceData sd = new SliceData();
sd.slice = slice;
sd.shape = newShape;
return sd;
}
use of org.python.core.PyObject in project scisoft-core by DawnScience.
the class PythonUtils method convertToJava.
/**
* Convert tuples/lists of tuples/lists to Java lists of lists. Also convert complex numbers to Apache Commons
* version and Jython strings to strings
*
* @param obj
* @return converted object
*/
public static Object convertToJava(Object obj) {
if (obj == null || obj instanceof PyNone)
return null;
if (obj instanceof PySequenceList) {
obj = ((PySequenceList) obj).toArray();
}
if (obj instanceof PyArray) {
obj = ((PyArray) obj).getArray();
}
if (obj instanceof List<?>) {
@SuppressWarnings("unchecked") List<Object> jl = (List<Object>) obj;
int l = jl.size();
for (int i = 0; i < l; i++) {
Object lo = jl.get(i);
if (lo instanceof PyObject) {
jl.set(i, convertToJava(lo));
}
}
return obj;
}
if (obj.getClass().isArray()) {
int l = Array.getLength(obj);
for (int i = 0; i < l; i++) {
Object lo = Array.get(obj, i);
if (lo instanceof PyObject) {
Array.set(obj, i, convertToJava(lo));
}
}
return obj;
}
// NB PyLong gets converted to BigInteger automatically so pass it through
if (obj instanceof BigInteger || !(obj instanceof PyObject)) {
return obj;
}
if (obj instanceof PyComplex) {
PyComplex z = (PyComplex) obj;
return new Complex(z.real, z.imag);
} else if (obj instanceof PyString) {
return obj.toString();
}
return ((PyObject) obj).__tojava__(Object.class);
}
use of org.python.core.PyObject in project zenoss-zep by zenoss.
the class TriggerPlugin method eventSatisfiesRule.
protected boolean eventSatisfiesRule(RuleContext ruleContext, String triggerUuid, String ruleSource) {
PyObject result;
try {
// check to see if the cache is full and log an error if so
if (this.cacheIsFull(this.triggerRuleCache)) {
++cacheSizeWarningCounter;
if (cacheSizeWarningCounter % 100 == 0) {
logger.error("Trigger rule cache is full ({}); consider reconfiguring zeneventserver, making it larger", this.getTriggerRuleCacheSize());
cacheSizeWarningCounter = 0;
}
}
// use rule to build and evaluate a Python lambda expression
TriggerRuleCache cacheItem = triggerRuleCache.get(triggerUuid);
PyFunction fn = null;
if (cacheItem == null || !cacheItem.getRuleSource().equals(ruleSource)) {
try {
fn = (PyFunction) this.pythonHelper.getPythonInterpreter().eval("lambda evt, dev, elem, sub_elem, zp_det : " + ruleSource);
} catch (PySyntaxError e) {
String fmt = Py.formatException(e.type, e.value);
logger.warn("syntax error exception raised while compiling rule: {}, {}", ruleSource, fmt);
}
// Cache result of trigger evaluation (even if it failed to compile). This will prevent trying to
// recompile the same invalid rule over and over again.
triggerRuleCache.put(triggerUuid, new TriggerRuleCache(ruleSource, fn));
} else {
fn = cacheItem.getPyFunction();
}
if (fn == null) {
logger.debug("Invalid rule source: {}", ruleSource);
return false;
}
// evaluate the rule function
PyObject[] args = { ruleContext.event, ruleContext.device, ruleContext.element, ruleContext.subElement, ruleContext.zpDetails };
result = fn.__call__(args);
} catch (PySyntaxError pysynerr) {
// evaluating rule raised an exception - treat as "False" eval
String fmt = Py.formatException(pysynerr.type, pysynerr.value);
logger.warn("syntax error exception raised while compiling rule: {}, {}", ruleSource, fmt);
result = new PyInteger(0);
} catch (PyException pyexc) {
// and an eval of False is fine. Otherwise we should log in case there's a real issue.
if (!pyexc.match(Py.AttributeError)) {
String fmt = Py.formatException(pyexc.type, pyexc.value);
logger.warn("exception raised while evaluating rule: {}, {}", ruleSource, fmt);
} else if (logger.isDebugEnabled()) {
String fmt = Py.formatException(pyexc.type, pyexc.value);
logger.debug("AttributeError raised while evaluating rule: {}, {}", ruleSource, fmt);
}
result = new PyInteger(0);
}
// object-as-bool evaluator
return result.__nonzero__();
}
use of org.python.core.PyObject in project cloud-slang by CloudSlang.
the class ScriptEvaluator method getSensitive.
@Deprecated
private boolean getSensitive(Map<String, Serializable> executionResultContext, boolean systemPropertiesInContext) {
if (systemPropertiesInContext) {
Map<String, Serializable> context = new HashMap<>(executionResultContext);
PyObject rawSystemProperties = (PyObject) context.remove(SYSTEM_PROPERTIES_MAP);
@SuppressWarnings("unchecked") Map<String, Value> systemProperties = Py.tojava(rawSystemProperties, Map.class);
@SuppressWarnings("unchecked") Collection<Serializable> systemPropertyValues = (Collection) systemProperties.values();
return checkSensitivity(systemPropertyValues) || checkSensitivity(context.values());
} else {
return (checkSensitivity(executionResultContext.values()));
}
}
Aggregations