wolframclient.serializers package¶
Subpackages¶
- wolframclient.serializers.encoders package
- Submodules
- wolframclient.serializers.encoders.builtin module
- wolframclient.serializers.encoders.datetime module
- wolframclient.serializers.encoders.decimal module
- wolframclient.serializers.encoders.fractions module
- wolframclient.serializers.encoders.numpy module
- wolframclient.serializers.encoders.pandas module
- wolframclient.serializers.encoders.pil module
- Module contents
- wolframclient.serializers.wxfencoder package
- Submodules
- wolframclient.serializers.wxfencoder.constants module
- wolframclient.serializers.wxfencoder.serializer module
- wolframclient.serializers.wxfencoder.streaming module
- wolframclient.serializers.wxfencoder.utils module
- wolframclient.serializers.wxfencoder.wxfencoder module
- wolframclient.serializers.wxfencoder.wxfexpr module
- wolframclient.serializers.wxfencoder.wxfexprprovider module
- wolframclient.serializers.wxfencoder.wxfnumpyencoder module
- Module contents
Submodules¶
wolframclient.serializers.base module¶
-
class
wolframclient.serializers.base.
FormatSerializer
(normalizer=None, encoder=None, allow_external_objects=False, target_kernel_version=None, **kwargs)[source]¶
-
wolframclient.serializers.base.
concatenate_bytes
()¶ Concatenate any number of bytes objects.
The bytes whose method is called is inserted in between each pair.
The result is returned as a new bytes object.
Example: b’.’.join([b’ab’, b’pq’, b’rs’]) -> b’ab.pq.rs’.
wolframclient.serializers.encoder module¶
-
class
wolframclient.serializers.encoder.
Encoder
(normalizer=None, encoder=None, allow_external_objects=False, target_kernel_version=None, **kwargs)[source]¶ Bases:
object
A generic class exposing an
encode()
method applying an optional normalizer function, followed the most relevant encoding available for a given type.Arbitrary named parameters passed during initialization are later accessible with
get_property()
.
wolframclient.serializers.serializable module¶
-
class
wolframclient.serializers.serializable.
WLSerializable
[source]¶ Bases:
object
A class that can be serialized using
export()
.Custom serialization of a class is done by subclassing this class:
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.MyWolframFunction(*self.arguments)
Serialize
MyPythonClass
usingexport()
:>>> export(MyPythonClass('foo', 'bar')) b'MyWolframFunction["foo", "bar"]'
Serialization is applied recursively; arguments are also serialized:
>>> export(MyPythonClass(1, 2, MyPythonClass(2, 3))) 'MyWolframFunction[1, 2, MyWolframFunction[2, 3]]'
wolframclient.serializers.utils module¶
wolframclient.serializers.wl module¶
wolframclient.serializers.wxf module¶
-
class
wolframclient.serializers.wxf.
WXFSerializer
(normalizer=None, compress=False, **opts)[source]¶ Bases:
wolframclient.serializers.base.FormatSerializer
Serialize python objects to WXF.
Module contents¶
-
wolframclient.serializers.
export
(data, stream=None, target_format='wl', **options)[source]¶ Serialize input data to a target format.
Input data can be any supported Python type, including
list
,dict
or any serializable Python object.Serializable python objects are class extending
WLSerializable
and types declared in an encoder.The default format is InputForm string:
>>> export(wl.Range(3)) b'Range[3]'
Specify WXF format by setting target_format:
>>> export([1,2,3], target_format='wxf') b'8:fsListCCC'
Note
WXF is a binary format for serializing Wolfram Language expression. Consult the format specifications for in depth format description.
WXF byte arrays are deserialized with
binary_deserialize()
:>>> wxf = export([1,2,3], target_format='wxf') >>> binary_deserialize(wxf) [1, 2, 3]
If stream is specified with a string, it is interpreted as a file path and the serialized form is written directly to the specified file. The file is opened and closed automatically:
>>> export([1, 2, 3], stream='file.wl') 'file.wl'
If stream is specified with an output stream, the serialization bytes are written to it.
Any object that implements a write method, e.g.
file
,io.BytesIO
orio.StringIO
, is a valid value for the stream named parameter:>>> with open('file.wl', 'wb') as f: ... export([1, 2, 3], stream=f) ... <open file 'file.wl', mode 'wb' at 0x10a4f01e0>
-
class
wolframclient.serializers.
WLSerializable
[source]¶ Bases:
object
A class that can be serialized using
export()
.Custom serialization of a class is done by subclassing this class:
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.MyWolframFunction(*self.arguments)
Serialize
MyPythonClass
usingexport()
:>>> export(MyPythonClass('foo', 'bar')) b'MyWolframFunction["foo", "bar"]'
Serialization is applied recursively; arguments are also serialized:
>>> export(MyPythonClass(1, 2, MyPythonClass(2, 3))) 'MyWolframFunction[1, 2, MyWolframFunction[2, 3]]'