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)

Tango data type

Python data type

DEV_VOID

No data

DEV_BOOLEAN

bool

DEV_SHORT

int

DEV_LONG

int

DEV_LONG64

int

DEV_FLOAT

float

DEV_DOUBLE

float

DEV_USHORT

int

DEV_ULONG

int

DEV_ULONG64

int

DEV_STRING

str (decoded with latin-1, aka ISO-8859-1)

DEV_ENCODED

(New in PyTango 8.0)

sequence of two elements:

  1. str (decoded with latin-1, aka ISO-8859-1)

  2. bytes (for any value of extract_as, except String. In this case it is str (decoded with default python encoding utf-8))

DEV_ENUM

(New in PyTango 9.0)

  • int (for value)

  • list <str> (for enum_labels)

Note: Direct attribute access via DeviceProxy will return enum.IntEnum.

For array types (SPECTRUM/IMAGE)

Tango data type

ExtractAs

Python data type

DEVVAR_CHARARRAY

Numpy

numpy.ndarray (dtype= numpy.uint8)

Bytes

bytes

ByteArray

bytearray

String

str

(decoded with latin-1, aka ISO-8859-1)

List

list <int>

Tuple

tuple <int>

DEVVAR_SHORTARRAY

(DEV_SHORT + SPECTRUM)

(DEV_SHORT + IMAGE)

Numpy

numpy.ndarray (dtype= numpy.uint16)

Bytes

bytes

ByteArray

bytearray

String

str

(decoded with latin-1, aka ISO-8859-1)

List

list <int>

Tuple

tuple <int>

DEVVAR_LONGARRAY

(DEV_LONG + SPECTRUM)

(DEV_LONG + IMAGE)

Numpy

numpy.ndarray (dtype= numpy.uint32)

Bytes

bytes

ByteArray

bytearray

String

str

(decoded with latin-1, aka ISO-8859-1)

List

list <int>

Tuple

tuple <int>

DEVVAR_LONG64ARRAY

(DEV_LONG64 + SPECTRUM)

(DEV_LONG64 + IMAGE)

Numpy

numpy.ndarray (dtype= numpy.uint64)

Bytes

bytes

ByteArray

bytearray

String

str

(decoded with latin-1, aka ISO-8859-1)

List

list <int>

Tuple

tuple <int>

DEVVAR_FLOATARRAY

(DEV_FLOAT + SPECTRUM)

(DEV_FLOAT + IMAGE)

Numpy

numpy.ndarray (dtype= numpy.float32)

Bytes

bytes

ByteArray

bytearray

String

str

(decoded with latin-1, aka ISO-8859-1)

List

list <float>

Tuple

tuple <float>

DEVVAR_DOUBLEARRAY

(DEV_DOUBLE + SPECTRUM)

(DEV_DOUBLE + IMAGE)

Numpy

numpy.ndarray (dtype= numpy.float64)

Bytes

bytes

ByteArray

bytearray

String

str

(decoded with latin-1, aka ISO-8859-1)

List

list <float>

Tuple

tuple <float>

DEVVAR_USHORTARRAY

(DEV_USHORT + SPECTRUM)

(DEV_USHORT + IMAGE)

Numpy

numpy.ndarray (dtype= numpy.uint16)

Bytes

bytes

ByteArray

bytearray

String

str

(decoded with latin-1, aka ISO-8859-1)

List

list <int>

Tuple

tuple <int>

DEVVAR_ULONGARRAY

(DEV_ULONG + SPECTRUM)

(DEV_ULONG + IMAGE)

Numpy

numpy.ndarray (dtype= numpy.uint32)

Bytes

bytes

ByteArray

bytearray

String

str

(decoded with latin-1, aka ISO-8859-1)

List

list <int>

Tuple

tuple <int>

DEVVAR_ULONG64ARRAY

(DEV_ULONG64 + SPECTRUM)

(DEV_ULONG64 + IMAGE)

Numpy

numpy.ndarray (dtype= numpy.uint64)

Bytes

bytes

ByteArray

bytearray

String

str

(decoded with latin-1, aka ISO-8859-1)

List

list <int>

Tuple

tuple <int>

DEVVAR_STRINGARRAY

(DEV_STRING + SPECTRUM)

(DEV_STRING + IMAGE)

sequence<str>

(decoded with latin-1, aka ISO-8859-1)

DEV_LONGSTRINGARRAY

sequence of two elements:

  1. numpy.ndarray (dtype= numpy.int32) or sequence<int>

  2. sequence<str> (decoded with latin-1, aka ISO-8859-1)

DEV_DOUBLESTRINGARRAY

sequence of two elements:

  1. numpy.ndarray (dtype= numpy.float64) or sequence<int>

  2. sequence<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 list. Example:

    >>> 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: numpy.ndarray

    • For string data types:

      • tuple <str> for clients reading attributes

      • list <str> in attribute write and command handlers within devices

DevEnum pythonic usage

When using regular tango DeviceProxy and AttributeProxy DevEnum is treated just like in cpp tango (see enumerated attributes for more info). However, since PyTango >= 9.2.5 there is a more pythonic way of using DevEnum data types if you use the high level API, both in server and client side.

Note

DevEnum is only support for device attributes, not for commands.

In server side you can use python enum.IntEnum class to deal with DevEnum attributes (here we use type hints, see Use Python type hints when declaring a device, but we can also set dtype=Noon when defining the attribute - see earlier versions of this documentation):

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:

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:

$ 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