wolframclient.deserializers package¶
Subpackages¶
- wolframclient.deserializers.wxf package
- Submodules
- wolframclient.deserializers.wxf.wxfconsumer module
WXFConsumerWXFConsumer.BIGREAL_REWXFConsumer.BUILTIN_SYMBOLWXFConsumer.build_function()WXFConsumer.consume_association()WXFConsumer.consume_bigint()WXFConsumer.consume_bigreal()WXFConsumer.consume_binary_string()WXFConsumer.consume_function()WXFConsumer.consume_integer16()WXFConsumer.consume_integer32()WXFConsumer.consume_integer64()WXFConsumer.consume_integer8()WXFConsumer.consume_numeric_array()WXFConsumer.consume_packed_array()WXFConsumer.consume_real64()WXFConsumer.consume_rule()WXFConsumer.consume_rule_delayed()WXFConsumer.consume_string()WXFConsumer.consume_symbol()WXFConsumer.next_expression()
WXFConsumerNumpy
- wolframclient.deserializers.wxf.wxfparser module
WXFParserWXFParser.next_token()WXFParser.parse_array()WXFParser.parse_header()WXFParser.token_for_association()WXFParser.token_for_binary_string()WXFParser.token_for_function()WXFParser.token_for_integer16()WXFParser.token_for_integer32()WXFParser.token_for_integer64()WXFParser.token_for_integer8()WXFParser.token_for_numeric_array()WXFParser.token_for_packed_array()WXFParser.token_for_real64()WXFParser.token_for_rule()WXFParser.token_for_string()WXFParser.tokens()
WXFTokenencode_buffer()encode_default()parse_varint()
- Module contents
Module contents¶
- class wolframclient.deserializers.WXFConsumer[source]¶
Bases:
objectMap WXF types to Python object generating functions.
This class exposes a comprehensive list of methods consuming WXF types. Subclasses can override these members to implement custom parsing logic.
Example implementing a consumer that maps any function with head DirectedInfinity to float(‘inf’):
class ExampleConsumer(WXFConsumer): Infinity = wl.DirectedInfinity def build_function(self, head, arg_list, **kwargs): if head == self.Infinity: return float('inf') else: super().build_function(head, args_list, **kwargs)
Test the new consumer:
>>> wxf = export({'-inf': wl.DirectedInfinity(-1), '+inf': wl.DirectedInfinity(1)}, target_format='wxf') >>> binary_deserialize(wxf, consumer=ExampleConsumer()) {'-inf': inf, '+inf': inf}
Compare with default result:
>>> binary_deserialize(wxf) {'-inf': DirectedInfinity[-1], '+inf': DirectedInfinity[1]}
Once initialized, the entry point of a consumer is the method
next_expression(). It takes a token generator and returns a Python object. This method is particularly useful when building nested expressions, e.g:build_function(),consume_association(), etc, in order to fetch sub-expressions.- BIGREAL_RE = re.compile('([^`]+)(`[0-9.]+){0,1}(\\*\\^){0,1}(-?[0-9]+){0,1}')¶
- BUILTIN_SYMBOL = {'False': False, 'Indeterminate': nan, 'None': None, 'Null': None, 'True': True}¶
See documentation of
encode_none()for more information about the mapping of None and Null.
- build_function(head, arg_list, **kwargs)[source]¶
Create a Python object from head and args.
This function can be conveniently overloaded to create specific Python objects from various heads. e.g: DateObject, Complex, etc.
- consume_association(current_token, tokens, dict_class=<class 'wolframclient.utils.datastructures.immutabledict'>, **kwargs)[source]¶
Consume a
WXFTokenof type association.By default, return a
dictmade from the rules. The named option dict_class can be set to any type in which case an instance ofdict_classis returned.
- consume_bigreal(current_token, tokens, **kwargs)[source]¶
Parse a WXF big real as a WXF serializable big real.
There is not such thing as a big real, in Wolfram Language notation, in Python. This wrapper ensures round tripping of big reals without the need of ToExpression. Introducing ToExpression would imply to marshall the big real data to avoid malicious code from being introduced in place of an actual real.
- consume_binary_string(current_token, tokens, **kwargs)[source]¶
Consume a
WXFTokenof type binary string as a string of bytes.
- consume_function(current_token, tokens, **kwargs)[source]¶
Consume a
WXFTokenof type function.Return a
listif the head is symbol List, otherwise returns the result ofbuild_function()applied to the head and arguments.Usually custom parsing rules target Functions, but not List. To do so, it is recommended to override
build_function().
- consume_numeric_array(current_token, tokens, **kwargs)[source]¶
Consume a
WXFTokenof type raw array.This method return
list, and made the assumption that system is little endian.
- consume_packed_array(current_token, tokens, **kwargs)[source]¶
Consume a
WXFTokenof type packed array.This method return
list, and made the assumption that system is little endian.
- consume_rule_delayed(current_token, tokens, **kwargs)[source]¶
Consume a
WXFTokenof type rule as a tuple
- consume_string(current_token, tokens, **kwargs)[source]¶
Consume a
WXFTokenof type string as a string of unicode utf8 encoded.
- class wolframclient.deserializers.WXFConsumerNumpy[source]¶
Bases:
WXFConsumerA WXF consumer that maps WXF array types to NumPy arrays.
- WXF_TYPE_TO_DTYPE = {b'\x00': 'int8', b'\x01': 'int16', b'\x02': 'int32', b'\x03': 'int64', b'\x10': 'uint8', b'\x11': 'uint16', b'\x12': 'uint32', b'\x13': 'uint64', b'"': 'float32', b'#': 'float64', b'3': 'complex64', b'4': 'complex128'}¶
- class wolframclient.deserializers.WXFToken(wxf_type)[source]¶
Bases:
objectRepresent a WXF element, often referred as WXF tokens.
- array_type¶
- data¶
- property dimensions¶
- property element_count¶
- length¶
- wxf_type¶
- wolframclient.deserializers.binary_deserialize(wxf_input, consumer=None, **kwargs)[source]¶
Deserialize binary data and return a Python object.
Serialize a Python object to WXF:
>>> wxf = export({'key' : [1,2,3]}, target_format='wxf')
Retrieve the input object:
>>> binary_deserialize(wxf) {'key': [1, 2, 3]}
A stream of
WXFTokenis generated from the WXF input by a instance ofWXFParser.The consumer must be an instance of
WXFConsumer. If none is provided,WXFConsumerNumpyis used. To disable NumPy array support, useWXFConsumer.Named parameters are passed to the consumer. They can be any valid parameter of
next_expression(), namely:dict_class: map WXF Association to dict_class in place of a regular
dict