scijava/scyjava

Conversion of BigDecimal to Python fails

Closed this issue · 3 comments

>>> import scyjava
>>> BigDecimal = scyjava.jimport('java.math.BigDecimal')
>>> bd = BigDecimal("1.98734907821093790182734")
>>> bd.toString()
'1.98734907821093790182734'
>>> scyjava.to_python(bd)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../lib/python3.8/site-packages/scyjava/__init__.py", line 700, in to_python
    raise exc
  File ".../lib/python3.8/site-packages/scyjava/__init__.py", line 697, in to_python
    return _convert(data, py_converters)
  File ".../lib/python3.8/site-packages/scyjava/__init__.py", line 266, in _convert
    return prioritized.converter(obj)
  File ".../lib/python3.8/site-packages/scyjava/__init__.py", line 804, in <lambda>
    converter=lambda obj: float(obj.toString),
TypeError: float() argument must be a string or a number, not '_jpype._JMethod'

Seems like missing parathesis. toString()

@Thrameos Yep, thanks! Casting the java.lang.String through str was also needed, to avoid:

TypeError: float() argument must be a string or a number, not 'java.lang.String'

MCVE:

import jpype
jpype.startJVM()
import jpype.imports
from java.lang import String
s = String("1.23456789")
float(s)

But it's just Python working as intended, right? Nothing that can be improved on the JPype side, since the float constructor doesn't take the liberty of stringifying non-strings.

Yes. Python developers haven't really addressed my request to make str an abstraction so that Java Strings can be considered a string. Thus working as best we can given the constraints. Not sure why Java and Python chose to make string types concrete and mostly final, but that is what we have.