Seems like Mathematica can only import basic types (lists, numpy objects, etc). Is there a way to tell Mathematica how to understand arbitrary python objects?
2 Answers
A class who inherits the WLSerializable
class and overrides the to_wl
method can be automatically converted.
from wolframclient.serializers.serializable import WLSerializable
from wolframclient.language import wl
from wolframclient.serializers import export
class MyPythonClass(WLSerializable):
def __init__(self, *arguments):
self.arguments = arguments
def to_wl(self):
return wl.YourWolframFunction(*self.arguments)
So we can use a wrap type to implement automatic conversion:
from sympy import Rational
class myRational(WLSerializable, Rational):
def to_wl(self):
return wl.Divide(self.p, self.q)
Here are some special binding packages, which can also be automatically converted: WolframResearch/WolframClientForPython
Developers say they provide an interface: Extending Serialization Writing an Encoder
Using as follows: