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 be instantiated directly:
string_instance = datatypes.STRING("message")
function

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

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(INT64)
<class 'diffusion.datatypes.simple.Int64DataType'>
>>> get(15)
<class 'diffusion.datatypes.complex.JsonDataType'>

Available Data Types

class

diffusion.datatypes.primitives.stringdatatype.StringDataType(value)

Bases
diffusion.datatypes.primitives.primitivedatatype.PrimitiveDataType diffusion.datatypes.foundation.abstract.AbstractDataType diffusion.datatypes.foundation.datatype.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) (any, optional) Convert a binary representation into the corresponding value.
  • encode(value) (bytes) Convert a value into the corresponding binary representation.
  • from_bytes(data) (AbstractDataType, optional) Convert a binary representation into the corresponding value.
  • read_value(stream) (AbstractDataType, 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_value(stream) (BytesIO) Write the value into a binary stream.
method

write_value(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.

Convenience method, not to be overridden

classmethod

read_value(stream)

Read the value from a binary stream.

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

An initialised instance of the DataType.

classmethod

from_bytes(data)

Convert a binary representation into the corresponding value.

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

An initialised instance of the DataType.

classmethod

encode(value)

Convert a value into the corresponding binary representation.

Parameters
  • value (any) Native value to be serialised
Returns (bytes)

Serialised binary representation of the value.

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, optional)

Deserialised value.

method

validate()

Check the current value for correctness.

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

diffusion.datatypes.primitives.int64datatype.Int64DataType(value)

Bases
diffusion.datatypes.primitives.primitivedatatype.PrimitiveDataType diffusion.datatypes.foundation.abstract.AbstractDataType diffusion.datatypes.foundation.datatype.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) (any, optional) Convert a binary representation into the corresponding value.
  • encode(value) (bytes) Convert a value into the corresponding binary representation.
  • from_bytes(data) (AbstractDataType, optional) Convert a binary representation into the corresponding value.
  • read_value(stream) (AbstractDataType, 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_value(stream) (BytesIO) Write the value into a binary stream.
method

write_value(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.

Convenience method, not to be overridden

classmethod

read_value(stream)

Read the value from a binary stream.

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

An initialised instance of the DataType.

classmethod

from_bytes(data)

Convert a binary representation into the corresponding value.

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

An initialised instance of the DataType.

classmethod

encode(value)

Convert a value into the corresponding binary representation.

Parameters
  • value (any) Native value to be serialised
Returns (bytes)

Serialised binary representation of the value.

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, optional)

Deserialised value.

method

validate()

Check the current value for correctness.

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

diffusion.datatypes.primitives.doubledatatype.DoubleDataType(value)

Bases
diffusion.datatypes.primitives.primitivedatatype.PrimitiveDataType diffusion.datatypes.foundation.abstract.AbstractDataType diffusion.datatypes.foundation.datatype.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) (any, optional) Convert a binary representation into the corresponding value.
  • encode(value) (bytes) Convert a value into the corresponding binary representation.
  • from_bytes(data) (AbstractDataType, optional) Convert a binary representation into the corresponding value.
  • read_value(stream) (AbstractDataType, 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_value(stream) (BytesIO) Write the value into a binary stream.
method

write_value(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.

Convenience method, not to be overridden

classmethod

read_value(stream)

Read the value from a binary stream.

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

An initialised instance of the DataType.

classmethod

from_bytes(data)

Convert a binary representation into the corresponding value.

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

An initialised instance of the DataType.

classmethod

encode(value)

Convert a value into the corresponding binary representation.

Parameters
  • value (any) Native value to be serialised
Returns (bytes)

Serialised binary representation of the value.

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, optional)

Deserialised value.

method

validate()

Check the current value for correctness.

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

diffusion.datatypes.primitives.binarydatatype.BinaryDataType(value)

Bases
diffusion.datatypes.primitives.primitivedatatype.PrimitiveDataType diffusion.datatypes.foundation.abstract.AbstractDataType diffusion.datatypes.foundation.datatype.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.
  • encode(value) (bytes) Convert the value into the binary representation.
  • from_bytes(data) (AbstractDataType, optional) Convert a binary representation into the corresponding value.
  • read_value(stream) (AbstractDataType, 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_value(stream) (BytesIO) Write the value into a binary stream.
method

write_value(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.

Convenience method, not to be overridden

classmethod

read_value(stream)

Read the value from a binary stream.

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

An initialised instance of the DataType.

classmethod

from_bytes(data)

Convert a binary representation into the corresponding value.

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

An initialised instance of the DataType.

method

set_from_bytes(data)

Convert bytes and set the corresponding value on the instance.

classmethod

encode(value) → bytes

Convert the value into the binary representation.

Parameters
  • value (any) Native value to be serialised
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.JsonDataType(value)

Bases
diffusion.datatypes.foundation.abstract.AbstractDataType diffusion.datatypes.foundation.datatype.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.
  • encode(value) (bytes) Convert a value into the corresponding binary representation.
  • from_bytes(data) (AbstractDataType, optional) Convert a binary representation into the corresponding value.
  • read_value(stream) (AbstractDataType, 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_value(stream) (BytesIO) Write the value into a binary stream.
method

write_value(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.

Convenience method, not to be overridden

classmethod

read_value(stream)

Read the value from a binary stream.

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

An initialised instance of the DataType.

classmethod

from_bytes(data)

Convert a binary representation into the corresponding value.

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

An initialised instance of the DataType.

classmethod

encode(value)

Convert a value into the corresponding binary representation.

Parameters
  • value (any) Native value to be serialised
Returns (bytes)

Serialised binary representation of the value.

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

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.