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