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")
diffusion.datatypes.
get
(
data_type
)
→ type of DataType
Helper function to retrieve a datatype based on its name or a DataTypes
value.
data_type
(Union(int, str, type of datatype, nonetype)) — Either a string that corresponds to thetype_name
attribute of aDataType
subclass, or an integer that corresponds to thetype_code
of aDataType
subclass. It also accepts an actualDataType
subclass, which is returned unchanged.
`UnknownDataTypeError`
— If the corresponding data type was not found.
>>> get('string')
<class 'diffusion.datatypes.simple.StringDataType'>
>>> get(INT64)
<class 'diffusion.datatypes.simple.Int64DataType'>
>>> get(15)
<class 'diffusion.datatypes.complex.JsonDataType'>
Available Data Types¶
diffusion.datatypes.primitives.
StringDataType
(
value
)
String data type.
The string value is serialized as CBOR-format binary.
serialised_value
(dict) — Return the sequence of values ready to be serialised.
It is assumed that the serialisation will use theserialised-value
serialiser.value
— Current value of the instance.
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.
read
(
stream
)
Read the value from a binary stream.
stream
(BytesIO) — Binary stream containing the serialised data.
An initialised instance of the DataType.
write
(
stream
)
→ BytesIO
Write the value into a binary stream.
stream
(BytesIO) — Binary stream to serialise the value into.
to_bytes
(
)
→ bytes
Convert the value into the binary representation.
from_bytes
(
data
)
Convert a binary representation into the corresponding value.
data
(bytes) — Serialised binary representation of the value.
An initialised instance of the DataType.
set_from_bytes
(
data
)
Convert bytes and set the corresponding value on the instance.
validate
(
)
Check the current value for correctness.
`InvalidDataError`
— If the value is invalid.
decode
(
data
)
Convert a binary representation into the corresponding value.
data
(bytes) — Serialised binary representation of the value.
Deserialised value.
diffusion.datatypes.primitives.
Int64DataType
(
value
)
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.
serialised_value
(dict) — Return the sequence of values ready to be serialised.
It is assumed that the serialisation will use theserialised-value
serialiser.value
— Current value of the instance.
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.
read
(
stream
)
Read the value from a binary stream.
stream
(BytesIO) — Binary stream containing the serialised data.
An initialised instance of the DataType.
write
(
stream
)
→ BytesIO
Write the value into a binary stream.
stream
(BytesIO) — Binary stream to serialise the value into.
to_bytes
(
)
→ bytes
Convert the value into the binary representation.
from_bytes
(
data
)
Convert a binary representation into the corresponding value.
data
(bytes) — Serialised binary representation of the value.
An initialised instance of the DataType.
set_from_bytes
(
data
)
Convert bytes and set the corresponding value on the instance.
decode
(
data
)
Convert a binary representation into the corresponding value.
data
(bytes) — Serialised binary representation of the value.
Deserialised value.
validate
(
)
Check the current value for correctness.
`InvalidDataError`
— If the value is invalid.
diffusion.datatypes.primitives.
DoubleDataType
(
value
)
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.
serialised_value
(dict) — Return the sequence of values ready to be serialised.
It is assumed that the serialisation will use theserialised-value
serialiser.value
— Current value of the instance.
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.
read
(
stream
)
Read the value from a binary stream.
stream
(BytesIO) — Binary stream containing the serialised data.
An initialised instance of the DataType.
write
(
stream
)
→ BytesIO
Write the value into a binary stream.
stream
(BytesIO) — Binary stream to serialise the value into.
to_bytes
(
)
→ bytes
Convert the value into the binary representation.
from_bytes
(
data
)
Convert a binary representation into the corresponding value.
data
(bytes) — Serialised binary representation of the value.
An initialised instance of the DataType.
set_from_bytes
(
data
)
Convert bytes and set the corresponding value on the instance.
decode
(
data
)
Convert a binary representation into the corresponding value.
data
(bytes) — Serialised binary representation of the value.
Deserialised value.
validate
(
)
Check the current value for correctness.
`InvalidDataError`
— If the value is invalid.
diffusion.datatypes.primitives.
BinaryDataType
(
value
)
Data type that supports arbitrary binary data.
serialised_value
(dict) — Return the sequence of values ready to be serialised.
It is assumed that the serialisation will use theserialised-value
serialiser.value
— Current value of the instance.
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.
read
(
stream
)
Read the value from a binary stream.
stream
(BytesIO) — Binary stream containing the serialised data.
An initialised instance of the DataType.
write
(
stream
)
→ BytesIO
Write the value into a binary stream.
stream
(BytesIO) — Binary stream to serialise the value into.
from_bytes
(
data
)
Convert a binary representation into the corresponding value.
data
(bytes) — Serialised binary representation of the value.
An initialised instance of the DataType.
set_from_bytes
(
data
)
Convert bytes and set the corresponding value on the instance.
to_bytes
(
)
→ bytes
Convert the value into the binary representation.
decode
(
data
)
Convert a binary representation into the corresponding value.
data
(bytes) — Serialised binary representation of the value.
Deserialised value.
validate
(
)
Check the current value for correctness.
`InvalidDataError`
— If the value is invalid.
diffusion.datatypes.complex.
JsonDataType
(
value
)
JSON data type implementation.
serialised_value
(dict) — Return the sequence of values ready to be serialised.
It is assumed that the serialisation will use theserialised-value
serialiser.value
— Current value of the instance.
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.
read
(
stream
)
Read the value from a binary stream.
stream
(BytesIO) — Binary stream containing the serialised data.
An initialised instance of the DataType.
write
(
stream
)
→ BytesIO
Write the value into a binary stream.
stream
(BytesIO) — Binary stream to serialise the value into.
to_bytes
(
)
→ bytes
Convert the value into the binary representation.
from_bytes
(
data
)
Convert a binary representation into the corresponding value.
data
(bytes) — Serialised binary representation of the value.
An initialised instance of the DataType.
set_from_bytes
(
data
)
Convert bytes and set the corresponding value on the instance.
decode
(
data
)
Convert a binary representation into the corresponding value.
data
(bytes) — Serialised binary representation of the value.
Deserialised value.
validate
(
)
Check the current value for correctness.
`InvalidDataError`
— If the value is invalid.