def test_dispatch(self): normalizer = Dispatch() class Person(object): pass class SportPlayer(Person): pass class FootballPlayer(SportPlayer): pass @normalizer.dispatch(object) def implementation(o): return o @normalizer.dispatch((int, float)) def implementation(o): return o * 2 @normalizer.dispatch(six.text_type) def implementation(o): return "Hello %s" % o @normalizer.dispatch(Person) def implementation(o): return "Hello person" @normalizer.dispatch(FootballPlayer) def implementation(o): return "Hello football player" self.assertEqual(normalizer("Ric"), "Hello Ric") self.assertEqual(normalizer(2), 4) self.assertEqual(normalizer(None), None) self.assertEqual(normalizer(Person()), "Hello person") self.assertEqual(normalizer(SportPlayer()), "Hello person") self.assertEqual(normalizer(FootballPlayer()), "Hello football player") normalizer.unregister(six.text_type) normalizer.register(lambda s: "Goodbye %s" % s, six.text_type) self.assertEqual(normalizer("Ric"), "Goodbye Ric") normalizer.unregister(object) normalizer.register(lambda s: 5, object) self.assertEqual(normalizer(None), 5)
def test_class_dispatch(self): normalizer = Dispatch() @normalizer.dispatch(object) def implementation(self, o): return o @normalizer.dispatch(six.text_type) def implementation(self, o): return "Hello %s" % o @normalizer.dispatch(int) def implementation(self, o): return o * 2 @normalizer.dispatch(int, replace_existing=True) def implementation(self, o): return o * 3 class Foo(object): attr = normalizer.as_method() self.assertEqual(Foo().attr("Ric"), "Hello Ric") self.assertEqual(Foo().attr(2), 6) self.assertEqual(Foo().attr(None), None) @normalizer.dispatch(int, keep_existing=True) def implementation(self, o): return o * 4 self.assertEqual(Foo().attr(2), 6) # inconsistent option values with self.assertRaises(ValueError): @normalizer.dispatch(int, replace_existing=True, keep_existing=True) def implementation(self, o): return o * 4 # already mapped. with self.assertRaises(TypeError): @normalizer.dispatch(int) def implementation(self, o): return o * 4
from __future__ import absolute_import, print_function, unicode_literals import math from wolframclient.language.expression import (WLFunction, WLInputExpression, WLSymbol) from wolframclient.serializers.serializable import WLSerializable from wolframclient.serializers.utils import safe_len from wolframclient.utils import six from wolframclient.utils.datastructures import Association from wolframclient.utils.dispatch import Dispatch from wolframclient.utils.encoding import force_bytes from wolframclient.utils.functional import map encoder = Dispatch() def _to_key_value(func, serializer, o): return func(((serializer.encode(key), serializer.encode(value)) for key, value in o.items()), length=safe_len(o)) @encoder.dispatch(bool) def encode_booleans(serializer, o): return serializer.serialize_symbol(force_bytes(o)) @encoder.dispatch(six.none_type) def encode_none(serializer, o):
from __future__ import absolute_import, print_function, unicode_literals from wolframclient.exception import WolframParserException from wolframclient.serializers.wxfencoder import constants from wolframclient.serializers.wxfencoder.serializer import ( WXF_HEADER_COMPRESS, WXF_HEADER_SEPARATOR, WXF_VERSION, SerializationContext, ) from wolframclient.serializers.wxfencoder.streaming import ExactSizeReader, ZipCompressedReader from wolframclient.utils import six from wolframclient.utils.dispatch import Dispatch wxf_input_to_buffer = Dispatch() @wxf_input_to_buffer.dispatch((six.binary_type, six.buffer_types)) def encode_buffer(wxf_input): return six.BytesIO(wxf_input) if six.PY2: @wxf_input_to_buffer.dispatch(memoryview, replace_existing=True) def encode_buffer(wxf_input): return six.BytesIO(wxf_input.tobytes()) @wxf_input_to_buffer.dispatch(object) def encode_default(wxf_input):
from __future__ import absolute_import, print_function, unicode_literals from wolframclient.utils import six from wolframclient.utils.dispatch import Dispatch from wolframclient.utils.functional import map force_text = Dispatch() @force_text.dispatch(six.buffer_types) def encode(s, *args, **opts): return force_text(force_bytes(s, *args, **opts), *args, **opts) @force_text.dispatch(six.text_type) def encode(s, encoding="utf-8", errors="strict"): return s @force_text.dispatch(six.binary_type, replace_existing=True) def encode(s, encoding="utf-8", errors="strict"): return s.decode(encoding, errors) if not six.PY2: @force_text.dispatch(object) def encode(s, encoding="utf-8", errors="strict"): return six.text_type(s) else: