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 AbstractDataType
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.
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
)
(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.
write_value
(
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.
Convenience method, not to be overridden
read_value
(
stream
)
Read the value from a binary stream.
stream
(BytesIO) — Binary stream containing the serialised data.
An initialised instance of the DataType.
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.
encode
(
value
)
Convert a value into the corresponding binary representation.
value
(any) — Native value to be serialised
Serialised binary representation of the value.
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.int64datatype.
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
)
(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.
write_value
(
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.
Convenience method, not to be overridden
read_value
(
stream
)
Read the value from a binary stream.
stream
(BytesIO) — Binary stream containing the serialised data.
An initialised instance of the DataType.
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.
encode
(
value
)
Convert a value into the corresponding binary representation.
value
(any) — Native value to be serialised
Serialised binary representation of the value.
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.
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
)
(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.
write_value
(
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.
Convenience method, not to be overridden
read_value
(
stream
)
Read the value from a binary stream.
stream
(BytesIO) — Binary stream containing the serialised data.
An initialised instance of the DataType.
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.
encode
(
value
)
Convert a value into the corresponding binary representation.
value
(any) — Native value to be serialised
Serialised binary representation of the value.
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.
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.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.
write_value
(
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.
Convenience method, not to be overridden
read_value
(
stream
)
Read the value from a binary stream.
stream
(BytesIO) — Binary stream containing the serialised data.
An initialised instance of the DataType.
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.
encode
(
value
)
→ bytes
Convert the value into the binary representation.
value
(any) — Native value to be serialised
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.
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.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.
write_value
(
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.
Convenience method, not to be overridden
read_value
(
stream
)
Read the value from a binary stream.
stream
(BytesIO) — Binary stream containing the serialised data.
An initialised instance of the DataType.
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.
encode
(
value
)
Convert a value into the corresponding binary representation.
value
(any) — Native value to be serialised
Serialised binary representation of the value.
decode
(
data
)
Convert a binary representation into the corresponding value.
data
(bytes) — Serialised binary representation of the value.
Deserialised value.
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.