wolframclient.serializers package

Submodules

wolframclient.serializers.base module

class wolframclient.serializers.base.FormatSerializer(normalizer=None, encoder=None, allow_external_objects=False, target_kernel_version=None, **kwargs)[source]

Bases: wolframclient.serializers.encoder.Encoder

export(data, stream=None)[source]
generate_bytes(data)[source]
serialize_association(mappable, **opts)[source]
serialize_bytes(bytes, as_byte_array=True)[source]
serialize_complex(o)[source]
serialize_decimal(obj)[source]
serialize_external_object(obj)[source]
serialize_float(obj)[source]
serialize_fraction(o)[source]
serialize_function(head, args, **opts)[source]
serialize_input_form(string)[source]
serialize_int(obj)[source]
serialize_iterable(iterable, **opts)[source]
serialize_mapping(mappable, **opts)[source]
serialize_numeric_array(data, shape, wl_type)[source]
serialize_packed_array(data, shape, wl_type)[source]
serialize_rule(lhs, rhs)[source]
serialize_rule_delayed(lhs, rhs)[source]
serialize_string(obj)[source]
serialize_symbol(symbol)[source]
serialize_tzinfo(tzinfo, date=None, name_match=re.compile('^([A-Za-z]+/[A-Za-z]+?|UTC)$'))[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().

chain_normalizer(func, encoder)[source]
get_property(key, d=None)[source]

Return the value of the named parameter passed during initialization.

Set d to the default value if key was not present.

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 using export():

>>> 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]]'
to_wl()[source]

Return the serialized form of a given Python class.

The returned value must be a combination of serializable types.

wolframclient.serializers.utils module

wolframclient.serializers.utils.py_encode_decimal(number, prec=28)[source]
wolframclient.serializers.utils.py_encode_text(s)[source]
wolframclient.serializers.utils.replace(match)[source]
wolframclient.serializers.utils.safe_len(obj)[source]

wolframclient.serializers.wl module

class wolframclient.serializers.wl.WLSerializer(normalizer=None, indent=None, **opts)[source]

Bases: wolframclient.serializers.base.FormatSerializer

generate_bytes(data)[source]
serialize_association(mapping, **opts)[source]
serialize_bytes(bytes, as_byte_array=True)[source]
serialize_decimal(number)[source]
serialize_float(number)[source]
serialize_function(head, args, **opts)[source]
serialize_input_form(string)[source]
serialize_int(number)[source]
serialize_iterable(iterable, **opts)[source]
serialize_mapping(mapping, **opts)[source]
serialize_rule(lhs, rhs)[source]
serialize_rule_delayed(lhs, rhs)[source]
serialize_string(string)[source]
serialize_symbol(name)[source]
wolframclient.serializers.wl.yield_with_separators(iterable, separator=b', ', first=None, last=None)[source]

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.

generate_bytes(data)[source]
serialize_bytes(bytes, as_byte_array=True)[source]
serialize_decimal(number)[source]
serialize_float(number)[source]
serialize_function(head, args, **opts)[source]
serialize_int(number)[source]
serialize_mapping(keyvalue, **opts)[source]
serialize_numeric_array(data, dimensions, wl_type)[source]
serialize_packed_array(data, dimensions, wl_type)[source]
serialize_string(string)[source]
serialize_symbol(name)[source]
wolframclient.serializers.wxf.get_length(iterable, length=None)[source]

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 or io.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 using export():

>>> 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]]'
to_wl()[source]

Return the serialized form of a given Python class.

The returned value must be a combination of serializable types.