SiLA Data Types¶
This section describes how SiLA data types are mapped to Python types.
Basic types¶
Integer:
int
, aValueError
will be raised for values < -2^63 or > 2^63-1Real:
float
Boolean:
bool
String:
string
, aValueError
will be raised if the length exceeds 2^21Binary:
bytes
, for values > 2 MB both sides of the communication must support SiLA Binary Transfer (which this SDK does)- Date:
SilaDateType
, aNamedTuple
class with the fieldsdate
andtimezone
(date
,timezone
) Note: Other timezone implementations might also work, as long as they map unambiguously to a UTC offset
- Date:
- Time:
time
, aValueError
will be raised if thetzinfo
attribute isNone
Note: Other timezone implementations might also work, as long as they map unambiguously to a UTC offset
- Time:
Timestamp:
datetime
, aValueError
will be raised if thetzinfo
attribute isNone
Any:
SilaAnyType
, aNamedTuple
class with the fieldstype_xml
andvalue
(str
,Any
)
List¶
The List type is mapped to a Python list
.
Constrained¶
The Constrained type is handled like its base type.
When a SiLA Server implemented using this SDK receives a value of a Constrained type as a command parameter,
it will validate the value and raise a ValidationError
if the value does not satisfy all constraints.
Structure¶
Structures are mapped implicitly to named tuples.
These are currently not supported by the code generator, so no appropriate NamedTuple
classes are generated yet.
The SDK will interpret plain tuple
objects as structures, if it has the proper number and types of items.
Example¶
A data type Point2D
is defined like this:
<DataType>
<Structure>
<Element>
<Identifier>X</Identifier>
<DisplayName>X</DisplayName>
<Description>The X coordinate</Description>
<DataType>
<Basic>Integer</Basic>
</DataType>
</Element>
<Element>
<Identifier>Y</Identifier>
<DisplayName>Y</DisplayName>
<Description>The Y coordinate</Description>
<DataType>
<Basic>Integer</Basic>
</DataType>
</Element>
</Structure>
</DataType>
The implicitly created class is:
from typing import NamedTuple
class Point2D_Struct(NamedTuple):
X: int
Y: int
When you receives a Point2D
object, you can access the structure elements like this:
>>> point = client.CoordinateFeature.Create2DPoint(1, 2) # returns Point2D(1, 2)
>>> # tuple indexing
>>> x = point[0]
>>> y = point[1]
>>> # attributes
>>> x = point.X
>>> y = point.Y
>>> # iterable unpacking
>>> x, y = point
When expecting a Structure, the SDK will accept a plain tuple
containing two int
objects: (1, 2)
.