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;
}
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);
}
Aggregations