I have a few questions regarding the feasibility and the viability of a library/module that would be implemented in Python but operable in both Java and Python mode.
Do you know about such library ?
Could it be possible to distribute a Python library as a JAR file with Jython ? (as discussed here)
if so, would the performance of such library be significantly impaired compared to a native Java library ?
if not, would a Processing Python library targeted at Python mode users only be considered as a Processing library by the community ? (officially referenced but with a mention “Python mode only” for example)
I believe it would probably become something like this when accessed from the Java side:
import org.python.core.*;
public static class JythonClass {
public int val;
public JythonClass(int someValue) {
val = someValue;
}
PyInteger intAdd(int someValue) {
val += someValue;
return new PyInteger(val);
}
PyFloat floatAdd(int someValue) {
val += someValue;
return new PyFloat(val);
}
PyString strAdd(int someValue) {
val += someValue;
return new PyString(str(val));
}
PyList listAdd(int someValue) {
val += someValue;
PyList pl = new PyList();
pl.append(new PyInteger(val));
return pl;
}
That would demand more effort for the Java programmer to convert the returned datatype to another 1 more familiar: int intVal = jython.intAdd(50).asInt(); // explicit conversion method call
So a Java programmer would need to know that a call to asInt() is necessary in order to store method intAdd()'s returned value to a Java int variable: PyInteger (Jython API documentation)
As a workaround we can convert a Jython datatype to a more common Java 1 before returning a value from a method:
The class JythonClassWorkaround above would be much easier for a Java programmer IMO.
Probably it would be seen as something like this in Java:
import org.python.core.*;
public static class JythonClassWorkaround {
public int val;
public JythonClassWorkaround(int someValue) {
val = someValue;
}
Integer IntegerAdd(int someValue) {
val += someValue;
return new Integer(val);
}
Float FloatAdd(int someValue) {
val += someValue;
return new Float(val);
}
String StringAdd(int someValue) {
val += someValue;
return new String(str(val));
}
ArrayList<Integer> ArrayListAdd(int someValue) {
val += someValue;
ArrayList<Integer> al = new ArrayList<Integer>(1);
al.add(new Integer(val));
return al;
}
String toString() {
return new String(str(val));
}
}
Jython version would be the source code version for code revision.
While the converted Java version would be the library deployment solution.
Although any changes to the Jython version would need to be applied to the Java 1.
I can’t say much as it is nothing serious really. I was mostly being curious about the state of Python-based libraries for Processing.
However, if I ever take the leap I guess it would have to do with Computational Geometry. I think Processing is lacking major useful (and often simple) functions that even currently available libraries are missing. I would personally love to address this short-coming.
The target audience would be architecture students or design researchers. Python is a widespread language (along with C#) in this field of activity and I think it would make sense (at least from my point of view) to implement a module that would be easily portable to other Python-compatible and design-related environments.
It would also certainly be a great experience for a rookie like me to face the challenges of building my first library/module.
I try to do things to the best I can. If I ever code something that is meant to be used by others I would like it to be decently implemented, reasonably efficient and easily readable. I wish I could tell you I could do all of this in Java but the reality is that, at the present, it is way beyond my ability. I would prefer coming up with a toolkit whose impact is limited in terms of popularity (python-mode users only) but that does the job well rather than dashing off a weak and poorly-written piece of program for all.
This makes sense to me. In the digital humanities python is also far more popular than Java – if I could write python libraries that worked in both modes I certainly would.
I have a library right now that I use with Python mode and wish it could work with Java without a rewrite. However it is hard to find time when I am immersed in both – two years ago I was spending almost all my time on the Python side, this past year I am spending a lot more time on the Java side…
You still can code in Jython only and deploy the compiled library as a JAR file to the PDE.
However, as I had stated before, I’m afraid you may need to pay close attention to the datatype of any value you return to the user from Jython functions, in such a way that a Java user can easily store in a Java variable w/o so much hassle.