11from __future__ import absolute_import , print_function , unicode_literals
22
3- from itertools import chain
3+ from itertools import chain , starmap
44
55from wolframclient .serializers .base import FormatSerializer
66from wolframclient .serializers .utils import py_encode_decimal , safe_len
2020)
2121from wolframclient .utils import six
2222from wolframclient .utils .api import zlib
23- from wolframclient .utils .encoding import force_bytes , force_text
23+ from wolframclient .utils .encoding import concatenate_bytes , force_bytes , force_text
24+ from wolframclient .utils .functional import partition
25+
26+
27+ def serialize_rule (key , value , sep = (WXF_CONSTANTS .Rule ,)):
28+ return chain (sep , key , value )
2429
2530
2631def get_length (iterable , length = None ):
@@ -37,6 +42,16 @@ def get_length(iterable, length=None):
3742 return iterable , len (iterable )
3843
3944
45+ def compress (data ):
46+
47+ compressor = zlib .compressobj ()
48+
49+ for token in map (compressor .compress , map (concatenate_bytes , partition (data , 100 ))):
50+ yield token
51+
52+ yield compressor .flush ()
53+
54+
4055class WXFSerializer (FormatSerializer ):
4156 """ Serialize python objects to WXF. """
4257
@@ -46,25 +61,14 @@ def __init__(self, normalizer=None, compress=False, **opts):
4661
4762 def generate_bytes (self , data ):
4863
49- yield WXF_VERSION
50-
5164 if self .compress :
52- yield WXF_HEADER_COMPRESS
5365
54- yield WXF_HEADER_SEPARATOR
66+ return chain (
67+ (WXF_VERSION , WXF_HEADER_COMPRESS , WXF_HEADER_SEPARATOR ),
68+ compress (self .encode (data )),
69+ )
5570
56- if self .compress :
57- compressor = zlib .compressobj ()
58- if six .PY2 :
59- for payload in self .encode (data ):
60- yield compressor .compress (six .binary_type (payload ))
61- else :
62- for payload in self .encode (data ):
63- yield compressor .compress (payload )
64- yield compressor .flush ()
65- else :
66- for payload in self .encode (data ):
67- yield payload
71+ return chain ((WXF_VERSION , WXF_HEADER_SEPARATOR ), self .encode (data ))
6872
6973 def serialize_symbol (self , name ):
7074 yield WXF_CONSTANTS .Symbol
@@ -116,12 +120,9 @@ def serialize_string(self, string):
116120
117121 def serialize_bytes (self , bytes , as_byte_array = not six .PY2 ):
118122 if as_byte_array :
119- yield WXF_CONSTANTS .BinaryString
120- yield varint_bytes (len (bytes ))
121- yield bytes
123+ return (WXF_CONSTANTS .BinaryString , varint_bytes (len (bytes )), bytes )
122124 else :
123- for token in self .serialize_string (force_text (bytes , encoding = "iso8859-1" )):
124- yield token
125+ return self .serialize_string (force_text (bytes , encoding = "iso8859-1" ))
125126
126127 def serialize_mapping (self , keyvalue , ** opts ):
127128 # the normalizer is always sending an generator key, value
@@ -130,9 +131,7 @@ def serialize_mapping(self, keyvalue, **opts):
130131
131132 return chain (
132133 (WXF_CONSTANTS .Association , varint_bytes (length )),
133- chain .from_iterable (
134- chain ((WXF_CONSTANTS .Rule ,), key , value ) for key , value in iterable
135- ),
134+ chain .from_iterable (starmap (serialize_rule , iterable )),
136135 )
137136
138137 def serialize_numeric_array (self , data , dimensions , wl_type ):
0 commit comments