10
$\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$
0

2 Answers 2

11
+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$
3
  • $\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$
    – Aster
    Commented Dec 16, 2019 at 3:48
  • $\begingroup$ Nice and concise answer -- thanks! $\endgroup$ Commented Nov 20, 2021 at 17:52
6
$\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 and acknowledge you have read our privacy policy.

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