JPype: how to convert the boolean value from python to java - jpype

I want to run java class in my python code and I use the tool JPype.
I have a java method with a boolean argument.
It works in java code but when I call it in python, I get the error message:
RuntimeError: No matching overloads found. at src/native/common/jp_method.cpp:121
I even use the jpype wrapper JBoolean, but it still fails.
For example, the code in java is:
item.myMethod(true);
and I have tried to convert it in python as:
item.myMethod(1)
item.myMethod(True)
item.myMethod(jpype.JBoolean(True))
item.myMethod(jpype.JBoolean(1))
but all of above get the same error message.
Could anyone help me to convert the boolean argument from python to java??
thank you!!

Is the argument of your Java method defined as a boolean or a java.lang.Boolean?
If it is boolean, then all the possibilities you have tried should work (if not there might be something wrong with the way you import the Class in your Python code). However, if it's a java.lang.Boolean, then you have to call your method as such:
item.myMethod(jpype.java.lang.Boolean(True))

Related

SyntaxError: unexpected EOF while parsing on MRJob

I am working with MRJob through python and when I try to create a class, I get a synthax error. It's highlighting the colon. What am I missing? Here is my code:
class Bacon_count(MRJob):
You are defining an empty class, but in an unproper way. In Python an empty class should contain a pass statement, in your case:
class Bacon_count(MRJob):
pass
Also, take into account how to define single class methods in multiple cells, if that's what you are trying to do, as it is discussed here.

Python: run a class method that takes an argument in a new thread

I am trying to do the same thing as in this question: Run Class methods in threads (python), but the class method I want to invoke in a separate thread takes an extra argument, apart from self. A.Rodas's solution does not work: if I try Thread(target=self.class_method, args=(self, arg2)).start(), it says I have 3 arguments instead of 2, while if try args=(arg2), it is breaking my arg2 string into constituent elements and saying 334234 arguments! Any ideas? Thanks
You should do it like this:
threading.Thread(target=self.class_method, args=(arg2,)).start()
It is hard to tell from the format of your question, but I think the issue is that you shouldn't be including self in the args tuple.
i.e.
threading.Thread(target=self.class_method, args=(arg2)).start()

BigDecimal Calculation Error at Jython

I try to run
BigDecimal LOADPROCESSID = BigDecimal(461).Add(new BigDecimal(1))
at Jython.
However, I got error message
BigDecimal LOADPROCESSID = BigDecimal(461).Add(new BigDecimal(1))
^ SyntaxError: mismatched input 'LOADPROCESSID' expecting NEWLINE
I searched internet and find this code. However, I am not quite familiar with Jython. So I am not sure whether the code is correct for Jython. If not, how can I write this logic in Jython. Thanks.
In Jython you don't have to user 'new' to instanciate an object, neither you
have to declare the type, so simply do
import java.math.BigDecimal as bd
LOADPROCESSID = bd(461).add(bd(1))
When you will be more confortable whith Jython, you will be able to overload
the 'add' operator.
Regards

Calling PythonFunction's from a VB application hosting Iron Python

I'm a C++ programming who was tapped to write a small application in Visual Basic. The application hosts an IronPython runtime and I am attempting to define some function in python and then call them from VB. I have written a simple test function in python
def test():
print "Test was Called"
Then I use the iron python to create a ScriptSource from the python file. I am able to look up the "test" variable through object operations but I can not figure out how to call the object that. For example (in VB):
pyScope = engine.CreateScope()
pySource = engine.CreateSourceFromFile("C:\Some\File\path\test.py")
pySource.Execute(pyScope)
' Now I expect the function test() to be defined in pyScope
Dim tmp as Object
pyScope.TryGetVariable("test", tmp)
At this point in my code tmp is defined as an object of type PythonFunction. I can't figure out how to call this function.
tmp()
Is not valid VB syntax. I've gotten this far, now how do I perform this seemingly simple task?
Edit: By calling
pyEngine.Operations.Invoke(tmp)
I am able to call the function and I see the expected output at stdout. I am still under the impression that there is some function-pointer-like type that I can cast objects of type PythonFunction to which will let me invoke temp directly without calling to the Python engine.
Not sure this will work, but try casting it to an Action type:
DirectCast(tmp, Action)()
Based on the comment, try this:
engine.ObjectOperations.Invoke(tmp, Nothing)
VB in .NET 4 should have the same dynamic support as C#. According to http://msdn.microsoft.com/en-us/library/ee461504.aspx#Y5108 (near the bottom), you should be able to do:
Dim tmp As Object = scope.GetVariable("test")
... which is what you're already doing, so make sure you're targeting .NET 4.
If that doesn't work you should be able to cast it with the generic version of GetVariable:
Dim tmp As Action = scope.GetVariable(Of Action)("test")
Finally, you already discovered Invoke on ObjectOperations.
(You may need to tweak the syntax, since I don't know VB.)

AttributeError: 'long' object has no attribute '__tojava__'

I browsed the javadoc for PyInteger and found it supports api tojava. However, when I run the following script, error happens.
n=1
n.__tojava__(java.math.BigInteger)
AttributeError: 'long' object has no attribute '_tojava_'
I found only the api which is written with pure python could be called successfully. For other apis, there are always errors, saying "xxx object has no attribute 'xxx'". Is there any configuration I should do?
if all you want is a BigInteger, you an just construct an instance using the string constructor
x=java.math.BigInteger('1')
or in your case
x=java.math.BigInteger(str(n))