1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360
|
```{eval-rst}
.. currentmodule:: tango
```
(pytango-data-types)=
# Data types
This chapter describes the mapping of data types between Python and Tango.
Tango has more data types than Python which is more dynamic. The input and
output values of the commands are translated according to the array below.
Note that the numpy type is used for the input arguments.
Also, it is recommended to use numpy arrays of the appropiate type for output
arguments as well, as they tend to be much more efficient.
**For scalar types (SCALAR)**
```{eval-rst}
+-------------------------+---------------------------------------------------------------------------+
| Tango data type | Python data type |
+=========================+===========================================================================+
| DEV_VOID | No data |
+-------------------------+---------------------------------------------------------------------------+
| DEV_BOOLEAN | :py:obj:`bool` |
+-------------------------+---------------------------------------------------------------------------+
| DEV_SHORT | :py:obj:`int` |
+-------------------------+---------------------------------------------------------------------------+
| DEV_LONG | :py:obj:`int` |
+-------------------------+---------------------------------------------------------------------------+
| DEV_LONG64 | :py:obj:`int` |
+-------------------------+---------------------------------------------------------------------------+
| DEV_FLOAT | :py:obj:`float` |
+-------------------------+---------------------------------------------------------------------------+
| DEV_DOUBLE | :py:obj:`float` |
+-------------------------+---------------------------------------------------------------------------+
| DEV_USHORT | :py:obj:`int` |
+-------------------------+---------------------------------------------------------------------------+
| DEV_ULONG | :py:obj:`int` |
+-------------------------+---------------------------------------------------------------------------+
| DEV_ULONG64 | :py:obj:`int` |
+-------------------------+---------------------------------------------------------------------------+
| DEV_STRING | :py:obj:`str` (decoded with *latin-1*, aka *ISO-8859-1*) |
+-------------------------+---------------------------------------------------------------------------+
| | sequence of two elements: |
| DEV_ENCODED | |
| | 0. :py:obj:`str` (decoded with *latin-1*, aka *ISO-8859-1*) |
| (*New in PyTango 8.0*) | 1. :py:obj:`bytes` (for any value of *extract_as*, except String. |
| | In this case it is :py:obj:`str` (decoded with default python |
| | encoding *utf-8*)) |
+-------------------------+---------------------------------------------------------------------------+
| | * :py:obj:`int` (for value) |
| | * :py:class:`list` <:py:obj:`str`> (for enum_labels) |
| DEV_ENUM | |
| | Note: |
| (*New in PyTango 9.0*) | Direct attribute access via DeviceProxy will return |
| | :py:obj:`enum.IntEnum`. |
+-------------------------+---------------------------------------------------------------------------+
```
**For array types (SPECTRUM/IMAGE)**
```{eval-rst}
+-------------------------+-----------------+-------------------------------------------------------------------------+
| Tango data type | ExtractAs | Python data type |
+=========================+=================+=========================================================================+
| DEVVAR_CHARARRAY | Numpy | :py:class:`numpy.ndarray` (dtype= :py:obj:`numpy.uint8`) |
| +-----------------+-------------------------------------------------------------------------+
| | Bytes | :py:obj:`bytes` |
| +-----------------+-------------------------------------------------------------------------+
| | ByteArray | :py:obj:`bytearray` |
| +-----------------+-------------------------------------------------------------------------+
| | String | :py:obj:`str` |
| | | |
| | | (decoded with *latin-1*, aka *ISO-8859-1*) |
| +-----------------+-------------------------------------------------------------------------+
| | List | :py:class:`list` <:py:obj:`int`> |
| +-----------------+-------------------------------------------------------------------------+
| | Tuple | :py:class:`tuple` <:py:obj:`int`> |
+-------------------------+-----------------+-------------------------------------------------------------------------+
| DEVVAR_SHORTARRAY | Numpy | :py:class:`numpy.ndarray` (dtype= :py:obj:`numpy.uint16`) |
| +-----------------+-------------------------------------------------------------------------+
| (DEV_SHORT + SPECTRUM) | Bytes | :py:obj:`bytes` |
| +-----------------+-------------------------------------------------------------------------+
| (DEV_SHORT + IMAGE) | ByteArray | :py:obj:`bytearray` |
| +-----------------+-------------------------------------------------------------------------+
| | String | :py:obj:`str` |
| | | |
| | | (decoded with *latin-1*, aka *ISO-8859-1*) |
| +-----------------+-------------------------------------------------------------------------+
| | List | :py:class:`list` <:py:obj:`int`> |
| +-----------------+-------------------------------------------------------------------------+
| | Tuple | :py:class:`tuple` <:py:obj:`int`> |
+-------------------------+-----------------+-------------------------------------------------------------------------+
| DEVVAR_LONGARRAY | Numpy | :py:class:`numpy.ndarray` (dtype= :py:obj:`numpy.uint32`) |
| +-----------------+-------------------------------------------------------------------------+
| (DEV_LONG + SPECTRUM) | Bytes | :py:obj:`bytes` |
| +-----------------+-------------------------------------------------------------------------+
| (DEV_LONG + IMAGE) | ByteArray | :py:obj:`bytearray` |
| +-----------------+-------------------------------------------------------------------------+
| | String | :py:obj:`str` |
| | | |
| | | (decoded with *latin-1*, aka *ISO-8859-1*) |
| +-----------------+-------------------------------------------------------------------------+
| | List | :py:class:`list` <:py:obj:`int`> |
| +-----------------+-------------------------------------------------------------------------+
| | Tuple | :py:class:`tuple` <:py:obj:`int`> |
+-------------------------+-----------------+-------------------------------------------------------------------------+
| DEVVAR_LONG64ARRAY | Numpy | :py:class:`numpy.ndarray` (dtype= :py:obj:`numpy.uint64`) |
| +-----------------+-------------------------------------------------------------------------+
| (DEV_LONG64 + SPECTRUM) | Bytes | :py:obj:`bytes` |
| +-----------------+-------------------------------------------------------------------------+
| (DEV_LONG64 + IMAGE) | ByteArray | :py:obj:`bytearray` |
| +-----------------+-------------------------------------------------------------------------+
| | String | :py:obj:`str` |
| | | |
| | | (decoded with *latin-1*, aka *ISO-8859-1*) |
| +-----------------+-------------------------------------------------------------------------+
| | List | :py:class:`list` <:py:obj:`int`> |
| +-----------------+-------------------------------------------------------------------------+
| | Tuple | :py:class:`tuple` <:py:obj:`int`> |
+-------------------------+-----------------+-------------------------------------------------------------------------+
| DEVVAR_FLOATARRAY | Numpy | :py:class:`numpy.ndarray` (dtype= :py:obj:`numpy.float32`) |
| +-----------------+-------------------------------------------------------------------------+
| (DEV_FLOAT + SPECTRUM) | Bytes | :py:obj:`bytes` |
| +-----------------+-------------------------------------------------------------------------+
| (DEV_FLOAT + IMAGE) | ByteArray | :py:obj:`bytearray` |
| +-----------------+-------------------------------------------------------------------------+
| | String | :py:obj:`str` |
| | | |
| | | (decoded with *latin-1*, aka *ISO-8859-1*) |
| +-----------------+-------------------------------------------------------------------------+
| | List | :py:class:`list` <:py:obj:`float`> |
| +-----------------+-------------------------------------------------------------------------+
| | Tuple | :py:class:`tuple` <:py:obj:`float`> |
+-------------------------+-----------------+-------------------------------------------------------------------------+
| DEVVAR_DOUBLEARRAY | Numpy | :py:class:`numpy.ndarray` (dtype= :py:obj:`numpy.float64`) |
| +-----------------+-------------------------------------------------------------------------+
| (DEV_DOUBLE + SPECTRUM) | Bytes | :py:obj:`bytes` |
| +-----------------+-------------------------------------------------------------------------+
| (DEV_DOUBLE + IMAGE) | ByteArray | :py:obj:`bytearray` |
| +-----------------+-------------------------------------------------------------------------+
| | String | :py:obj:`str` |
| | | |
| | | (decoded with *latin-1*, aka *ISO-8859-1*) |
| +-----------------+-------------------------------------------------------------------------+
| | List | :py:class:`list` <:py:obj:`float`> |
| +-----------------+-------------------------------------------------------------------------+
| | Tuple | :py:class:`tuple` <:py:obj:`float`> |
+-------------------------+-----------------+-------------------------------------------------------------------------+
| DEVVAR_USHORTARRAY | Numpy | :py:class:`numpy.ndarray` (dtype= :py:obj:`numpy.uint16`) |
| +-----------------+-------------------------------------------------------------------------+
| (DEV_USHORT + SPECTRUM) | Bytes | :py:obj:`bytes` |
| +-----------------+-------------------------------------------------------------------------+
| (DEV_USHORT + IMAGE) | ByteArray | :py:obj:`bytearray` |
| +-----------------+-------------------------------------------------------------------------+
| | String | :py:obj:`str` |
| | | |
| | | (decoded with *latin-1*, aka *ISO-8859-1*) |
| +-----------------+-------------------------------------------------------------------------+
| | List | :py:class:`list` <:py:obj:`int`> |
| +-----------------+-------------------------------------------------------------------------+
| | Tuple | :py:class:`tuple` <:py:obj:`int`> |
+-------------------------+-----------------+-------------------------------------------------------------------------+
| DEVVAR_ULONGARRAY | Numpy | :py:class:`numpy.ndarray` (dtype= :py:obj:`numpy.uint32`) |
| +-----------------+-------------------------------------------------------------------------+
| (DEV_ULONG + SPECTRUM) | Bytes | :py:obj:`bytes` |
| +-----------------+-------------------------------------------------------------------------+
| (DEV_ULONG + IMAGE) | ByteArray | :py:obj:`bytearray` |
| +-----------------+-------------------------------------------------------------------------+
| | String | :py:obj:`str` |
| | | |
| | | (decoded with *latin-1*, aka *ISO-8859-1*) |
| +-----------------+-------------------------------------------------------------------------+
| | List | :py:class:`list` <:py:obj:`int`> |
| +-----------------+-------------------------------------------------------------------------+
| | Tuple | :py:class:`tuple` <:py:obj:`int`> |
+-------------------------+-----------------+-------------------------------------------------------------------------+
| DEVVAR_ULONG64ARRAY | Numpy | :py:class:`numpy.ndarray` (dtype= :py:obj:`numpy.uint64`) |
| +-----------------+-------------------------------------------------------------------------+
| (DEV_ULONG64 + SPECTRUM)| Bytes | :py:obj:`bytes` |
| +-----------------+-------------------------------------------------------------------------+
| (DEV_ULONG64 + IMAGE) | ByteArray | :py:obj:`bytearray` |
| +-----------------+-------------------------------------------------------------------------+
| | String | :py:obj:`str` |
| | | |
| | | (decoded with *latin-1*, aka *ISO-8859-1*) |
| +-----------------+-------------------------------------------------------------------------+
| | List | :py:class:`list` <:py:obj:`int`> |
| +-----------------+-------------------------------------------------------------------------+
| | Tuple | :py:class:`tuple` <:py:obj:`int`> |
+-------------------------+-----------------+-------------------------------------------------------------------------+
| DEVVAR_STRINGARRAY | | sequence<:py:obj:`str`> |
| | | |
| (DEV_STRING + SPECTRUM) | | (decoded with *latin-1*, aka *ISO-8859-1*) |
| | | |
| (DEV_STRING + IMAGE) | | |
+-------------------------+-----------------+-------------------------------------------------------------------------+
| | | sequence of two elements: |
| DEV_LONGSTRINGARRAY | | |
| | | 0. :py:class:`numpy.ndarray` (dtype= :py:obj:`numpy.int32`) or |
| | | sequence<:py:obj:`int`> |
| | | 1. sequence<:py:obj:`str`> (decoded with *latin-1*, aka *ISO-8859-1*) |
+-------------------------+-----------------+-------------------------------------------------------------------------+
| | | sequence of two elements: |
| DEV_DOUBLESTRINGARRAY | | |
| | | 0. :py:class:`numpy.ndarray` (dtype= :py:obj:`numpy.float64`) or |
| | | sequence<:py:obj:`int`> |
| | | 1. sequence<:py:obj:`str`> (decoded with *latin-1*, aka *ISO-8859-1*) |
+-------------------------+-----------------+-------------------------------------------------------------------------+
```
For SPECTRUM and IMAGES the actual sequence object used depends on the context
where the tango data is used.
1. for properties the sequence is always a {py:class}`list`. Example:
```pycon
>>> import tango
>>> db = tango.Database()
>>> s = db.get_property(["TangoSynchrotrons"])
>>> print type(s)
<type 'list'>
```
2. for attribute/command values:
- For non-string data types: {py:class}`numpy.ndarray`
- For string data types:
- {py:class}`tuple` \<{py:class}`str`> for clients reading attributes
- {py:class}`list` \<{py:class}`str`> in attribute write and command handlers within devices
(pytango-devenum-data-types)=
## DevEnum pythonic usage
When using regular tango DeviceProxy and AttributeProxy DevEnum is treated just
like in cpp tango (see [enumerated attributes](https://tango-controls.readthedocs.io/en/latest/How-To/development/cpp/how-to-enumerated-attribute.html#how-to-enumerated-attribute)
for more info). However, since PyTango >= 9.2.5 there is a more pythonic way of
using DevEnum data types if you use the {ref}`high level API <pytango-hlapi>`,
both in server and client side.
:::{note}
DevEnum is only support for device attributes, not for commands.
:::
In server side you can use python {py:obj}`enum.IntEnum` class to deal with
DevEnum attributes (here we use type hints, see {ref}`type-hint`, but we can also set
`dtype=Noon` when defining the attribute - see earlier versions of this documentation):
```{code-block} python
import time
from enum import IntEnum
from tango.server import Device, attribute, command
class Noon(IntEnum):
AM = 0 # DevEnum's must start at 0
PM = 1 # and increment by 1
class DisplayType(IntEnum):
ANALOG = 0 # DevEnum's must start at 0
DIGITAL = 1 # and increment by 1
class Clock(Device):
display_type = DisplayType.ANALOG
@attribute
def time(self) -> float:
return time.time()
@attribute(max_dim_x=9)
def gmtime(self) -> tuple[int]:
return time.gmtime()
@attribute
def noon(self) -> Noon:
time_struct = time.gmtime(time.time())
return Noon.AM if time_struct.tm_hour < 12 else Noon.PM
@attribute
def display(self) -> DisplayType:
return self.display_type
@display.setter
def display(self, display_type: int):
# note that we receive an integer, not an enum instance,
# so we have to convert that to an instance of our enum.
self.display_type = DisplayType(display_type)
@command(dtype_in=float, dtype_out=str)
def ctime(self, seconds):
"""
Convert a time in seconds since the Epoch to a string in local time.
This is equivalent to asctime(localtime(seconds)). When the time tuple
is not present, current time as returned by localtime() is used.
"""
return time.ctime(seconds)
@command
def mktime(self, tupl: tuple[int]) -> float:
return time.mktime(tuple(tupl))
if __name__ == "__main__":
Clock.run_server()
```
On the client side you can also use a pythonic approach for using DevEnum attributes:
```{code-block} python
import sys
import tango
if len(sys.argv) != 2:
print("must provide one and only one clock device name")
sys.exit(1)
clock = tango.DeviceProxy(sys.argv[1])
t = clock.time
gmt = clock.gmtime
noon = clock.noon
display = clock.display
print(t)
print(gmt)
print(noon, noon.name, noon.value)
if noon == noon.AM:
print("Good morning!")
print(clock.ctime(t))
print(clock.mktime(gmt))
print(display, display.name, display.value)
clock.display = display.ANALOG
clock.display = "DIGITAL" # you can use a valid string to set the value
print(clock.display, clock.display.name, clock.display.value)
display_type = type(display) # or even create your own IntEnum type
analog = display_type(0)
clock.display = analog
print(clock.display, clock.display.name, clock.display.value)
clock.display = clock.display.DIGITAL
print(clock.display, clock.display.name, clock.display.value)
```
Example output:
```console
$ python client.py test/clock/1
1699433430.714272
[2023 11 8 8 50 30 2 312 0]
0 AM 0
Good morning!
Wed Nov 8 09:50:30 2023
1699429830.0
0 ANALOG 0
1 DIGITAL 1
0 ANALOG 0
1 DIGITAL 1
```
|