Search in sources :

Example 66 with PyObject

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);
}
Also used : PySystemState(org.python.core.PySystemState) PyString(org.python.core.PyString) PythonInterpreter(org.python.util.PythonInterpreter) PyString(org.python.core.PyString) Properties(java.util.Properties) PyObject(org.python.core.PyObject)

Example 67 with PyObject

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;
}
Also used : PySequenceList(org.python.core.PySequenceList) PyInteger(org.python.core.PyInteger) SliceND(org.eclipse.january.dataset.SliceND) PyNone(org.python.core.PyNone) PyException(org.python.core.PyException) Slice(org.eclipse.january.dataset.Slice) PySlice(org.python.core.PySlice) PySlice(org.python.core.PySlice) PyObject(org.python.core.PyObject) PyEllipsis(org.python.core.PyEllipsis)

Example 68 with PyObject

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);
}
Also used : PySequenceList(org.python.core.PySequenceList) PyArray(org.python.core.PyArray) Complex(org.apache.commons.math3.complex.Complex) PyComplex(org.python.core.PyComplex) PyNone(org.python.core.PyNone) PyString(org.python.core.PyString) PyComplex(org.python.core.PyComplex) BigInteger(java.math.BigInteger) ArrayList(java.util.ArrayList) List(java.util.List) PySequenceList(org.python.core.PySequenceList) PyObject(org.python.core.PyObject) PyObject(org.python.core.PyObject)

Example 69 with PyObject

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__();
}
Also used : PyFunction(org.python.core.PyFunction) PySyntaxError(org.python.core.PySyntaxError) PyException(org.python.core.PyException) PyString(org.python.core.PyString) PyInteger(org.python.core.PyInteger) PyObject(org.python.core.PyObject)

Example 70 with PyObject

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()));
    }
}
Also used : Serializable(java.io.Serializable) HashMap(java.util.HashMap) PyObjectValue(io.cloudslang.lang.entities.bindings.values.PyObjectValue) Value(io.cloudslang.lang.entities.bindings.values.Value) Collection(java.util.Collection) PyObject(org.python.core.PyObject)

Aggregations

PyObject (org.python.core.PyObject)80 PyString (org.python.core.PyString)24 PyList (org.python.core.PyList)16 ArrayList (java.util.ArrayList)15 HashMap (java.util.HashMap)12 PyException (org.python.core.PyException)12 IOException (java.io.IOException)9 Serializable (java.io.Serializable)9 PyInteger (org.python.core.PyInteger)8 Test (org.junit.Test)7 PyStringMap (org.python.core.PyStringMap)7 PythonInterpreter (org.python.util.PythonInterpreter)7 UnsupportedEncodingException (java.io.UnsupportedEncodingException)6 Map (java.util.Map)6 PyDictionary (org.python.core.PyDictionary)6 PyFloat (org.python.core.PyFloat)6 List (java.util.List)5 PyLong (org.python.core.PyLong)5 PyNone (org.python.core.PyNone)5 PyTuple (org.python.core.PyTuple)5