Data Types

Data types are containers for specific formats of of topic data and messages sent between Diffusion clients and servers.

Usage

from diffusion import datatypes

# the next two lines are equivalent
string_type = datatypes.get("STRING")
string_type = datatypes.STRING

string_instance = string_type("message")

# a data type can also be instantiated directly:
string_instance = datatypes.STRING("message")
function

diffusion.datatypes.get(data_type) → type of DataType

Helper function to retrieve a datatype based on its name or a DataTypes value.

Parameters
  • data_type (Union(int, str, type of datatype, nonetype)) Either a string that corresponds to the type_name attribute of a DataType subclass, or an integer that corresponds to the type_code of a DataType subclass. It also accepts an actual DataType subclass, which is returned unchanged.
Raises
  • `UnknownDataTypeError` If the corresponding data type was not found.
Examples
>>> get('string')
<class 'diffusion.datatypes.simple.StringDataType'>
>>> get(_dt_module.INT64)
<class 'diffusion.datatypes.simple.Int64DataType'>
>>> get(15)
<class 'diffusion.datatypes.complex.JsonDataType'>

Available Data Types

class

diffusion.datatypes.simple.StringDataType(value)

Bases
diffusion.datatypes.abstract.DataType

String data type.

The string value is serialized as CBOR-format binary.

Attributes
  • serialised_value (dict) Return the sequence of values ready to be serialised.
    It is assumed that the serialisation will use the serialised-value serialiser.
  • value Current value of the instance.
Methods
  • decode(data) (str, optional) Convert a binary representation into the corresponding value.
  • from_bytes(data) (DataType, optional) Convert a binary representation into the corresponding value.
  • read(stream) (DataType, optional) Read the value from a binary stream.
  • set_from_bytes(data) Convert bytes and set the corresponding value on the instance.
  • to_bytes() (bytes) Convert the value into the binary representation.
  • validate() Check the current value for correctness.
  • write(stream) (BytesIO) Write the value into a binary stream.
classmethod

read(stream)

Read the value from a binary stream.

Parameters
  • stream (BytesIO) Binary stream containing the serialised data.
Returns (DataType, optional)

An initialised instance of the DataType.

method

write(stream) → BytesIO

Write the value into a binary stream.

Parameters
  • stream (BytesIO) Binary stream to serialise the value into.
method

to_bytes() → bytes

Convert the value into the binary representation.

classmethod

from_bytes(data)

Convert a binary representation into the corresponding value.

Parameters
  • data (bytes) Serialised binary representation of the value.
Returns (DataType, optional)

An initialised instance of the DataType.

method

set_from_bytes(data)

Convert bytes and set the corresponding value on the instance.

method

validate()

Check the current value for correctness.

Raises
  • `InvalidDataError` If the value is invalid.
classmethod

decode(data)

Convert a binary representation into the corresponding value.

Parameters
  • data (bytes) Serialised binary representation of the value.
Returns (str, optional)

Deserialised value.

class

diffusion.datatypes.simple.Int64DataType(value)

Bases
diffusion.datatypes.abstract.DataType

Data type that supports 64-bit, signed integer values.

The integer value is serialized as CBOR-format binary. A serialized value can be read as JSON instance.

Attributes
  • serialised_value (dict) Return the sequence of values ready to be serialised.
    It is assumed that the serialisation will use the serialised-value serialiser.
  • value Current value of the instance.
Methods
  • decode(data) (int, optional) Convert a binary representation into the corresponding value.
  • from_bytes(data) (DataType, optional) Convert a binary representation into the corresponding value.
  • read(stream) (DataType, optional) Read the value from a binary stream.
  • set_from_bytes(data) Convert bytes and set the corresponding value on the instance.
  • to_bytes() (bytes) Convert the value into the binary representation.
  • validate() Check the current value for correctness.
  • write(stream) (BytesIO) Write the value into a binary stream.
classmethod

read(stream)

Read the value from a binary stream.

Parameters
  • stream (BytesIO) Binary stream containing the serialised data.
Returns (DataType, optional)

An initialised instance of the DataType.

method

write(stream) → BytesIO

Write the value into a binary stream.

Parameters
  • stream (BytesIO) Binary stream to serialise the value into.
method

to_bytes() → bytes

Convert the value into the binary representation.

classmethod

from_bytes(data)

Convert a binary representation into the corresponding value.

Parameters
  • data (bytes) Serialised binary representation of the value.
Returns (DataType, optional)

An initialised instance of the DataType.

method

set_from_bytes(data)

Convert bytes and set the corresponding value on the instance.

classmethod

decode(data)

Convert a binary representation into the corresponding value.

Parameters
  • data (bytes) Serialised binary representation of the value.
Returns (int, optional)

Deserialised value.

method

validate()

Check the current value for correctness.

Raises
  • `InvalidDataError` If the value is invalid.
class

diffusion.datatypes.simple.DoubleDataType(value)

Bases
diffusion.datatypes.abstract.DataType

Data type that supports double-precision floating point numbers.

(Eight-byte IEEE 754)

The integer value is serialized as CBOR-format binary. A serialized value can be read as a JSON instance.

Attributes
  • serialised_value (dict) Return the sequence of values ready to be serialised.
    It is assumed that the serialisation will use the serialised-value serialiser.
  • value Current value of the instance.
Methods
  • decode(data) (float, optional) Convert a binary representation into the corresponding value.
  • from_bytes(data) (DataType, optional) Convert a binary representation into the corresponding value.
  • read(stream) (DataType, optional) Read the value from a binary stream.
  • set_from_bytes(data) Convert bytes and set the corresponding value on the instance.
  • to_bytes() (bytes) Convert the value into the binary representation.
  • validate() Check the current value for correctness.
  • write(stream) (BytesIO) Write the value into a binary stream.
classmethod

read(stream)

Read the value from a binary stream.

Parameters
  • stream (BytesIO) Binary stream containing the serialised data.
Returns (DataType, optional)

An initialised instance of the DataType.

method

write(stream) → BytesIO

Write the value into a binary stream.

Parameters
  • stream (BytesIO) Binary stream to serialise the value into.
method

to_bytes() → bytes

Convert the value into the binary representation.

classmethod

from_bytes(data)

Convert a binary representation into the corresponding value.

Parameters
  • data (bytes) Serialised binary representation of the value.
Returns (DataType, optional)

An initialised instance of the DataType.

method

set_from_bytes(data)

Convert bytes and set the corresponding value on the instance.

classmethod

decode(data)

Convert a binary representation into the corresponding value.

Parameters
  • data (bytes) Serialised binary representation of the value.
Returns (float, optional)

Deserialised value.

method

validate()

Check the current value for correctness.

Raises
  • `InvalidDataError` If the value is invalid.
class

diffusion.datatypes.simple.BinaryDataType(value)

Bases
diffusion.datatypes.abstract.DataType

Data type that supports arbitrary binary data.

Attributes
  • serialised_value (dict) Return the sequence of values ready to be serialised.
    It is assumed that the serialisation will use the serialised-value serialiser.
  • value Current value of the instance.
Methods
  • decode(data) (bytes) Convert a binary representation into the corresponding value.
  • from_bytes(data) (DataType, optional) Convert a binary representation into the corresponding value.
  • read(stream) (DataType, optional) Read the value from a binary stream.
  • set_from_bytes(data) Convert bytes and set the corresponding value on the instance.
  • to_bytes() (bytes) Convert the value into the binary representation.
  • validate() Check the current value for correctness.
  • write(stream) (BytesIO) Write the value into a binary stream.
classmethod

read(stream)

Read the value from a binary stream.

Parameters
  • stream (BytesIO) Binary stream containing the serialised data.
Returns (DataType, optional)

An initialised instance of the DataType.

method

write(stream) → BytesIO

Write the value into a binary stream.

Parameters
  • stream (BytesIO) Binary stream to serialise the value into.
classmethod

from_bytes(data)

Convert a binary representation into the corresponding value.

Parameters
  • data (bytes) Serialised binary representation of the value.
Returns (DataType, optional)

An initialised instance of the DataType.

method

set_from_bytes(data)

Convert bytes and set the corresponding value on the instance.

method

to_bytes() → bytes

Convert the value into the binary representation.

classmethod

decode(data)

Convert a binary representation into the corresponding value.

Parameters
  • data (bytes) Serialised binary representation of the value.
Returns (bytes)

Deserialised value.

method

validate()

Check the current value for correctness.

Raises
  • `InvalidDataError` If the value is invalid.
class

diffusion.datatypes.complex.JsonDataType(value)

Bases
diffusion.datatypes.abstract.DataType

JSON data type implementation.

Attributes
  • serialised_value (dict) Return the sequence of values ready to be serialised.
    It is assumed that the serialisation will use the serialised-value serialiser.
  • value Current value of the instance.
Methods
  • decode(data) (any) Convert a binary representation into the corresponding value.
  • from_bytes(data) (DataType, optional) Convert a binary representation into the corresponding value.
  • read(stream) (DataType, optional) Read the value from a binary stream.
  • set_from_bytes(data) Convert bytes and set the corresponding value on the instance.
  • to_bytes() (bytes) Convert the value into the binary representation.
  • validate() Check the current value for correctness.
  • write(stream) (BytesIO) Write the value into a binary stream.
classmethod

read(stream)

Read the value from a binary stream.

Parameters
  • stream (BytesIO) Binary stream containing the serialised data.
Returns (DataType, optional)

An initialised instance of the DataType.

method

write(stream) → BytesIO

Write the value into a binary stream.

Parameters
  • stream (BytesIO) Binary stream to serialise the value into.
method

to_bytes() → bytes

Convert the value into the binary representation.

classmethod

from_bytes(data)

Convert a binary representation into the corresponding value.

Parameters
  • data (bytes) Serialised binary representation of the value.
Returns (DataType, optional)

An initialised instance of the DataType.

method

set_from_bytes(data)

Convert bytes and set the corresponding value on the instance.

classmethod

decode(data)

Convert a binary representation into the corresponding value.

Parameters
  • data (bytes) Serialised binary representation of the value.
Returns (any)

Deserialised value.

method

validate()

Check the current value for correctness.

Raises
  • `InvalidDataError` If the value is invalid.