Search in sources :

Example 1 with PyArray

use of org.python.core.PyArray in project gda-core by openGDA.

the class GdaBuiltin method boxVarargs.

/**
 * If there are more arguments than expected, wrap all extra arguments into a single
 * array to represent the varargs argument of a Java method. The types of the extra
 * arguments are checked later so do not matter at this stage.
 * <br>
 * This is a modified version of the private function
 * {@code ReflectedArgs#ensureBNoxedVarargs}.
 * <br>
 * There is still some ambiguity if the type of the varargs is itself an iterable
 * and the final arg passed is a list. For now, this assumes that the final arg is
 * the wrapped vararg and returns it as reveived.
 *
 * @param args All the arguments passed to the function
 * @param length The number of arguments expected (including varargs array as 1)
 * @return An array of objects matching the number expected - may be the original array
 */
private PyObject[] boxVarargs(PyObject[] args, int length) {
    if (args.length == 0) {
        // if length is > 1, this will still fail later but not our problem
        return new PyObject[] { new PyList() };
    }
    PyObject lastArg = args[args.length - 1];
    if (args.length == length && lastArg instanceof PySequenceList || lastArg instanceof PyArray || lastArg instanceof PyXRange || lastArg instanceof PyIterator) {
        // will be boxed in an array once __tojava__ is called
        return args;
    }
    int positionals = length - 1;
    if (args.length < positionals) {
        return args;
    }
    var boxedArgs = new PyObject[length];
    System.arraycopy(args, 0, boxedArgs, 0, positionals);
    int others = args.length - positionals;
    var varargs = new PyObject[others];
    System.arraycopy(args, positionals, varargs, 0, others);
    boxedArgs[positionals] = new PyList(varargs);
    return boxedArgs;
}
Also used : PySequenceList(org.python.core.PySequenceList) PyXRange(org.python.core.PyXRange) PyIterator(org.python.core.PyIterator) PyArray(org.python.core.PyArray) PyList(org.python.core.PyList) PyObject(org.python.core.PyObject)

Example 2 with PyArray

use of org.python.core.PyArray 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)

Aggregations

PyArray (org.python.core.PyArray)2 PyObject (org.python.core.PyObject)2 PySequenceList (org.python.core.PySequenceList)2 BigInteger (java.math.BigInteger)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Complex (org.apache.commons.math3.complex.Complex)1 PyComplex (org.python.core.PyComplex)1 PyIterator (org.python.core.PyIterator)1 PyList (org.python.core.PyList)1 PyNone (org.python.core.PyNone)1 PyString (org.python.core.PyString)1 PyXRange (org.python.core.PyXRange)1