openmath/helpers.py
def interpretAsOpenMath(x):
    """tries to convert a Python object into an OpenMath object
    this is not a replacement for using a Converter for exporting Python objects
    instead, it is used conveniently building OM objects in DSL embedded in Python
    inparticular, it converts Python functions into OMBinding objects using lambdaOM as the binder"""
    
    if hasattr(x, "_ishelper") and x._ishelper:
        # wrapped things in this class -> unwrap
        return x._toOM()
    
    elif isinstance(x, om.OMAny):
        # already OM
        return x
    
    elif isinstance(x, six.integer_types):
        # integers -> OMI
        return om.OMInteger(x)
    
    elif isinstance(x, float):
        # floats -> OMF
        return om.OMFloat(x)

    elif isinstance(x, six.string_types):
        # strings -> OMSTR
        return om.OMString(x)
    
    elif isinstance(x, WrappedHelper):
        # wrapper -> wrapped object
        return x.toOM()
    
    elif inspect.isfunction(x):
        # function -> OMBIND(lambda,...)
        
        # get all the parameters of the function
        paramMap = inspect.signature(x).parameters
        params = [v for k, v in six.iteritems(paramMap)]
        
        # make sure that all of them are positional
        posArgKinds = [inspect.Parameter.POSITIONAL_ONLY, inspect.Parameter.POSITIONAL_OR_KEYWORD]
        if not all([p.kind in posArgKinds for p in params]):
            raise CannotInterpretAsOpenMath("no sequence arguments allowed")
        
        # call the function with appropriate OMVariables
        paramsOM = [om.OMVariable(name=p.name) for p in params]
        bodyOM = interpretAsOpenMath(x(*paramsOM))

        return OMBinding(om.OMSymbol(name="lambda", cd="python", cdbase="http://python.org"), paramsOM, bodyOM)
    
    else:
        # fail
        raise CannotInterpretAsOpenMath("unknown kind of object: " + str(x))
def interpretAsOpenMath(x):
    if hasattr(x, "_ishelper") and x._ishelper:
        # wrapped things in this class -> unwrap
        return x._toOM()
    
    elif isinstance(x, om.OMAny):
        # already OM
        return x
    
    elif isinstance(x, six.integer_types):
        # integers -> OMI
        return om.OMInteger(x)
    
    elif isinstance(x, float):
        # floats -> OMF
        return om.OMFloat(x)

    elif isinstance(x, six.string_types):
        # strings -> OMSTR
        return om.OMString(x)
    
    elif isinstance(x, WrappedHelper):
        # wrapper -> wrapped object
        return x.toOM()
    
    elif inspect.isfunction(x):
        # function -> OMBIND(lambda,...)
        
        # get all the parameters of the function
        paramMap = inspect.signature(x).parameters
        params = [v for k, v in six.iteritems(paramMap)]
        
        # make sure that all of them are positional
        posArgKinds = [inspect.Parameter.POSITIONAL_ONLY, inspect.Parameter.POSITIONAL_OR_KEYWORD]
        if not all([p.kind in posArgKinds for p in params]):
            raise CannotInterpretAsOpenMath("no sequence arguments allowed")
        
        # call the function with appropriate OMVariables
        paramsOM = [om.OMVariable(name=p.name) for p in params]
        bodyOM = interpretAsOpenMath(x(*paramsOM))

        return OMBinding(om.OMSymbol(name="lambda", cd="python", cdbase="http://python.org"), paramsOM, bodyOM)
    
    else:
        # fail
        raise CannotInterpretAsOpenMath("unknown kind of object: " + str(x))
def interpretAsOpenMath ( x ) : if hasattr ( x , "_ishelper" ) and x . _ishelper : # wrapped things in this class -> unwrap return x . _toOM ( ) elif isinstance ( x , om . OMAny ) : # already OM return x elif isinstance ( x , six . integer_types ) : # integers -> OMI return om . OMInteger ( x ) elif isinstance ( x , float ) : # floats -> OMF return om . OMFloat ( x ) elif isinstance ( x , six . string_types ) : # strings -> OMSTR return om . OMString ( x ) elif isinstance ( x , WrappedHelper ) : # wrapper -> wrapped object return x . toOM ( ) elif inspect . isfunction ( x ) : # function -> OMBIND(lambda,...) # get all the parameters of the function paramMap = inspect . signature ( x ) . parameters params = [ v for k , v in six . iteritems ( paramMap ) ] # make sure that all of them are positional posArgKinds = [ inspect . Parameter . POSITIONAL_ONLY , inspect . Parameter . POSITIONAL_OR_KEYWORD ] if not all ( [ p . kind in posArgKinds for p in params ] ) : raise CannotInterpretAsOpenMath ( "no sequence arguments allowed" ) # call the function with appropriate OMVariables paramsOM = [ om . OMVariable ( name = p . name ) for p in params ] bodyOM = interpretAsOpenMath ( x ( * paramsOM ) ) return OMBinding ( om . OMSymbol ( name = "lambda" , cd = "python" , cdbase = "http://python.org" ) , paramsOM , bodyOM ) else : # fail raise CannotInterpretAsOpenMath ( "unknown kind of object: " + str ( x ) )
tries to convert a Python object into an OpenMath object
    this is not a replacement for using a Converter for exporting Python objects
    instead, it is used conveniently building OM objects in DSL embedded in Python
    inparticular, it converts Python functions into OMBinding objects using lambdaOM as the binder
tries to convert a Python object into an OpenMath object this is not a replacement for using a Converter for exporting Python objects instead it is used conveniently building OM objects in DSL embedded in Python inparticular it converts Python functions into OMBinding objects using lambdaOM as the binder