8
$\begingroup$

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?

enter image description here

$\endgroup$
9
+25
$\begingroup$

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)

enter image description here

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)

enter image description here

Here are some special binding packages, which can also be automatically converted: WolframResearch/WolframClientForPython

$\endgroup$
  • $\begingroup$ Could you add an example for working with a complicated data type like graphs or images please? $\endgroup$ – M.R. Dec 13 '19 at 17:21
  • $\begingroup$ @well, which lib, expect matplotlib, it not use class $\endgroup$ – GalAster Dec 16 '19 at 3:48
4
$\begingroup$

Developers say they provide an interface: Extending Serialization Writing an Encoder

Using as follows:

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.