gluonts.core.serde package

gluonts.core.serde.encode(v: Any) → Any[source]

Transforms a value v as a serializable intermediate representation (for example, named tuples are encoded as dictionaries). The intermediate representation is then recursively traversed and serialized either as Python code or as JSON string.

This function is decorated with singledispatch() and can be specialized by clients for families of types that are not supported by the basic implementation (explained below).

Examples

The conversion logic implemented by the basic implementation is used as a fallback and is best explained by a series of examples.

Lists (as lists).

>>> encode([1, 2.0, '3'])
[1, 2.0, '3']

Dictionaries (as dictionaries).

>>> encode({'a': 1, 'b': 2.0, 'c': '3'})
{'a': 1, 'b': 2.0, 'c': '3'}

Named tuples (as dictionaries with a '__kind__': <Kind.Instance: 'instance'> member).

>>> from pprint import pprint
>>> from typing import NamedTuple
>>> class ComplexNumber(NamedTuple):
...     x: float = 0.0
...     y: float = 0.0
>>> pprint(encode(ComplexNumber(4.0, 2.0)))
{'__kind__': <Kind.Instance: 'instance'>,
 'class': 'gluonts.core.serde._base.ComplexNumber',
 'kwargs': {'x': 4.0, 'y': 2.0}}

Classes with a validated() initializer (as dictionaries with a '__kind__': <Kind.Instance: 'instance'> member).

>>> from gluonts.core.component import validated
>>> class ComplexNumber:
...     @validated()
...     def __init__(self, x: float = 0.0, y: float = 0.0) -> None:
...         self.x = x
...         self.y = y
>>> pprint(encode(ComplexNumber(4.0, 2.0)))
{'__kind__': <Kind.Instance: 'instance'>,
 'args': [],
 'class': 'gluonts.core.serde._base.ComplexNumber',
 'kwargs': {'x': 4.0, 'y': 2.0}}

Classes with a __getnewargs_ex__ magic method (as dictionaries with a '__kind__': <Kind.Instance: 'instance'> member).

>>> from gluonts.core.component import validated
>>> class ComplexNumber:
...     def __init__(self, x: float = 0.0, y: float = 0.0) -> None:
...         self.x = x
...         self.y = y
...     def __getnewargs_ex__(self):
...         return [], {'x': self.x, 'y': self.y}
>>> pprint(encode(ComplexNumber(4.0, 2.0)))
{'__kind__': <Kind.Instance: 'instance'>,
 'args': [],
 'class': 'gluonts.core.serde._base.ComplexNumber',
 'kwargs': {'x': 4.0, 'y': 2.0}}

Types (as dictionaries with a '__kind__': <Kind.Type: 'type'> member).

>>> encode(ComplexNumber)
{'__kind__': <Kind.Type: 'type'>,
 'class': 'gluonts.core.serde._base.ComplexNumber'}
Parameters

v – The value to be encoded.

Returns

An encoding of v that can be serialized to Python code or JSON string.

Return type

Any

See also

decode()

Inverse function.

dump_json()

Serializes an object to a JSON string.

dump_code()

Serializes an object to a Python code string.

gluonts.core.serde.decode(r: Any) → Any[source]

Decodes a value from an intermediate representation r.

Parameters

r – An intermediate representation to be decoded.

Returns

A Python data structure corresponding to the decoded version of r.

Return type

Any

See also

encode()

Inverse function.

gluonts.core.serde.dump_code(o: Any) → str[source]

Serializes an object to a Python code string.

Parameters

o – The object to serialize.

Returns

A string representing the object as Python code.

Return type

str

See also

load_code()

Inverse function.

gluonts.core.serde.load_code(s)[source]
gluonts.core.serde.dump_json(o: Any, indent: Optional[int] = None) → str[source]

Serializes an object to a JSON string.

Parameters
  • o – The object to serialize.

  • indent – An optional number of spaced to use as an indent.

Returns

A string representing the object in JSON format.

Return type

str

See also

load_json()

Inverse function.

gluonts.core.serde.load_json(s: str) → Any[source]

Deserializes an object from a JSON string.

Parameters

s – A string representing the object in JSON format.

Returns

The deserialized object.

Return type

Any

See also

dump_json()

Inverse function.

class gluonts.core.serde.Stateful[source]

Bases: object

class gluonts.core.serde.Stateless[source]

Bases: object