polywrap_msgpack.sanitize module

This module contains the sanitize function that converts python values into msgpack compatible values.

polywrap_msgpack.sanitize.sanitize(value: Any) Any[source]

Sanitize the value into msgpack encoder compatible format.

Parameters:

value (Any) – any valid python value

Raises:

ValueError – when dict key isn’t string

Returns:

msgpack compatible sanitized value

Return type:

Any

Examples

>>> sanitize({"a": 1})
{'a': 1}
>>> sanitize({1, 2, 3})
[1, 2, 3]
>>> sanitize((1, 2, 3))
[1, 2, 3]
>>> sanitize([{1}, (2, 3), [4]])
[[1], [2, 3], [4]]
>>> class Foo: pass
>>> foo = Foo()
>>> foo.bar = 1
>>> sanitize(foo)
{'bar': 1}
>>> sanitize({1: 1})
Traceback (most recent call last):
...
ValueError: Dict key must be string, got 1 of type <class 'int'>
>>> sanitize(GenericMap({1: 2}))
Traceback (most recent call last):
...
ValueError: GenericMap key must be string, got 1 of type <class 'int'>