Table Of Contents
Table Of Contents

gluonts.core.serde module

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_binary(o: Any) → bytes[source]

Serializes an object o to binary format.

Parameters:o – The object to serialize.
Returns:A sequence of bytes representing the serialized object.
Return type:bytes

See also

load_binary()
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.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.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']

Tuples (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__': '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__': 'instance',
 'class': 'gluonts.core.serde.ComplexNumber',
 'kwargs': {'x': 4.0, 'y': 2.0}}

Classes with a validated() initializer (as dictionaries with a '__kind__': '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__': 'instance',
 'args': [],
 'class': 'gluonts.core.serde.ComplexNumber',
 'kwargs': {'x': 4.0, 'y': 2.0}}

Classes with a __getnewargs_ex__ magic method (as dictionaries with a '__kind__': '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__': 'instance',
 'args': [],
 'class': 'gluonts.core.serde.ComplexNumber',
 'kwargs': {'x': 4.0, 'y': 2.0}}

Types (as dictionaries with a '__kind__': 'type' member).

>>> encode(ComplexNumber)
{'__kind__': 'type', 'class': 'gluonts.core.serde.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.encode_mx_context(v: mxnet.context.Context) → Any[source]

Specializes encode() for invocations where v is an instance of the Context class.

gluonts.core.serde.encode_mx_ndarray(v: mxnet.ndarray.ndarray.NDArray) → Any[source]
gluonts.core.serde.encode_np_dtype(v: numpy.dtype) → Any[source]

Specializes encode() for invocations where v is an instance of the Context class.

gluonts.core.serde.encode_np_ndarray(v: numpy.ndarray) → Any[source]

Specializes encode() for invocations where v is an instance of the Context class.

gluonts.core.serde.encode_path(v: pathlib.Path) → Any[source]

Specializes encode() for invocations where v is an instance of the Path class.

gluonts.core.serde.encode_pydantic_model(v: pydantic.main.BaseModel) → Any[source]

Specializes encode() for invocations where v is an instance of the BaseModel class.

gluonts.core.serde.load_binary(b: bytes) → Any[source]

Deserializes an object from binary format.

Parameters:b – A sequence of bytes representing the serialized object.
Returns:The deserialized object.
Return type:Any

See also

dump_binary()
Inverse function.
gluonts.core.serde.load_code(c: str) → Any[source]

Deserializes an object from a Python code string.

Parameters:c – A string representing the object as Python code.
Returns:The deserialized object.
Return type:Any

See also

dump_code()
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.