Calling java classes from Sage using Py4J

23 views
Skip to first unread message

Paul DesJardin

unread,
Aug 2, 2017, 3:16:54 PM8/2/17
to sage-support

Hi, I'm new to using Sage and trying to interface to java classes using py4j.  I've been successful using py4j using an anaconda installed version of python (on a mac) but I'm running into Attribute errors using Sage.  The one below is 'sage.rings.integer.Integer' object has no attribute '_get_object_id'.  I don't understand the reason for the differences and how to interpret the errors I'm receiving.  Any assistance or ideas of what is causing this would be very much appreciated.  Thank you, PD  




sage: from py4j.java_gateway import JavaGateway

sage: gateway = JavaGateway()

sage: random = gateway.jvm.java.util.Random()

sage: number1 = random.nextInt(10)

---------------------------------------------------------------------------

AttributeError                            Traceback (most recent call last)

<ipython-input-4-45af4cb476cf> in <module>()

----> 1 number1 = random.nextInt(Integer(10))


/Applications/SageMath/local/lib/python2.7/site-packages/py4j/java_gateway.pyc in __call__(self, *args)

   1149 

   1150     def __call__(self, *args):

-> 1151         args_command, temp_args = self._build_args(*args)

   1152 

   1153         command = proto.CALL_COMMAND_NAME +\


/Applications/SageMath/local/lib/python2.7/site-packages/py4j/java_gateway.pyc in _build_args(self, *args)

   1119 

   1120         args_command = "".join(

-> 1121             [get_command_part(arg, self.pool) for arg in new_args])

   1122 

   1123         return args_command, temp_args


/Applications/SageMath/local/lib/python2.7/site-packages/py4j/protocol.pyc in get_command_part(parameter, python_proxy_pool)

    288             command_part += ";" + interface

    289     else:

--> 290         command_part = REFERENCE_TYPE + parameter._get_object_id()

    291 

    292     command_part += "\n"


/Applications/SageMath/src/sage/structure/element.pyx in sage.structure.element.Element.__getattr__ (/Applications/SageMath/src/build/cythonized/sage/structure/element.c:4230)()

    457             AttributeError: 'LeftZeroSemigroup_with_category.element_class' object has no attribute 'blah_blah'

    458         """

--> 459         return self.getattr_from_category(name)

    460 

    461     cdef getattr_from_category(self, name):


/Applications/SageMath/src/sage/structure/element.pyx in sage.structure.element.Element.getattr_from_category (/Applications/SageMath/src/build/cythonized/sage/structure/element.c:4339)()

    470         else:

    471             cls = P._abstract_element_class

--> 472         return getattr_from_other_class(self, cls, name)

    473 

    474     def __dir__(self):


/Applications/SageMath/src/sage/structure/misc.pyx in sage.structure.misc.getattr_from_other_class (/Applications/SageMath/src/build/cythonized/sage/structure/misc.c:1933)()

    297         dummy_error_message.cls = type(self)

    298         dummy_error_message.name = name

--> 299         raise dummy_attribute_error

    300     attribute = <object>attr

    301     # Check for a descriptor (__get__ in Python)


AttributeError: 'sage.rings.integer.Integer' object has no attribute '_get_object_id'

John H Palmieri

unread,
Aug 2, 2017, 3:37:15 PM8/2/17
to sage-support
In Sage, "10" is a Sage integer, not a Python int. I am guessing that gateway.jvm.java.util.Random.nextInt expects a Python int for its input, and when it gets a Sage Integer, that causes a problem. What happens if you replace "random.nextInt(10)" with "random.nextInt(int(10))"?


Paul DesJardin

unread,
Aug 2, 2017, 5:06:56 PM8/2/17
to sage-support
That's it!  You're recommendation worked like a charm.  I certainly didn't appreciate the differences between Sage type and python number types.  Thank you John -  very much appreciated.  Paul

sage: from py4j.java_gateway import JavaGateway

sage: gateway = JavaGateway()

sage: random = gateway.jvm.java.util.Random()

sage: number1 = random.nextInt(int(10))

sage: number1

4

Reply all
Reply to author
Forward
0 new messages