Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow customizing the treatment of literals in conversion #13

Open
florian-rabe opened this issue Feb 21, 2018 · 2 comments
Open

Allow customizing the treatment of literals in conversion #13

florian-rabe opened this issue Feb 21, 2018 · 2 comments

Comments

@florian-rabe
Copy link
Contributor

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
Copy link
Collaborator

defeo commented Feb 21, 2018

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
Copy link
Collaborator

nthiery commented May 18, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants