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
$\begingroup$
$\endgroup$
3
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
-
$\begingroup$ Could you add an example for working with a complicated data type like graphs or images please? $\endgroup$– M.R.Commented Dec 13, 2019 at 17:21
-
$\begingroup$ @well, which lib, expect matplotlib, it not use class $\endgroup$– AsterCommented Dec 16, 2019 at 3:48
-
$\begingroup$ Nice and concise answer -- thanks! $\endgroup$ Commented Nov 20, 2021 at 17:52
$\begingroup$
$\endgroup$
Developers say they provide an interface: Extending Serialization Writing an Encoder
Using as follows: