OpenMath / py-openmath

An OpenMath 2.0 implementation in Python
MIT License
15 stars 4 forks source link

Allow customizing the treatment of literals in conversion #13

Open florian-rabe opened 6 years ago

florian-rabe commented 6 years ago

The treatment of the OpenMath literals (integer, float, string, byte array) is currently hard-coded in the convert methods.

The current treatment is a good defualt, but it should be possible to customize it.

One option, considering issue #12, is to simply use methods for this that can be overridden by other classes.

defeo commented 6 years ago

You mean, like this:

>>> from openmath import *
>>> i = convert.to_openmath(1)
>>> i
OMInteger(integer=1, id=None)
>>> convert.to_python(i)
1
>>> convert.register_to_python(openmath.OMInteger, lambda x: "Hello")
>>> convert.to_python(i)
'Hello'
nthiery commented 6 years ago

Ah, right, it's already possible to customize it. The code itself could use it, replacing the last 2 lines of:

def to_python(omobj):
    """ Convert OpenMath object to Python """
    if omobj.__class__ in _conv_to_py:
        return _conv_to_py[omobj.__class__](omobj)
    elif isinstance(omobj, om.OMInteger):
        return omobj.integer

by a call to register_to_python at initialization time.