diff --git a/devices/util/attribute_wrapper.py b/devices/util/attribute_wrapper.py index 37f7962b766aef6773289fced903a0c2c15db4f1..5953a1c8cb4e62d34df0be81e6cc4ae5cf29eca9 100644 --- a/devices/util/attribute_wrapper.py +++ b/devices/util/attribute_wrapper.py @@ -14,18 +14,20 @@ class attribute_wrapper(attribute): Wraps all the attributes in a wrapper class to manage most of the redundant code behind the scenes """ - def __init__(self, comms_annotation=None, datatype=None, dims=None, access=AttrWriteType.READ, init_value=None, **kwargs): + def __init__(self, comms_annotation=None, datatype=None, dims=(1,), access=AttrWriteType.READ, init_value=None, **kwargs): """ wraps around the tango Attribute class. Provides an easier interface for 1d or 2d arrays. Also provides a way to abstract managing the communications interface. comms_annotation: data passed along to the attribute. can be given any form of data. handling is up to client implementation datatype: any numpy datatype - dims: dimensions of the attribute as a tuple, or None for a scalar. + dims: dimensions of the attribute as a tuple, or (1,) for a scalar. init_value: value """ - # ensure the type is a numpy array + # ensure the type is a numpy array. + # see also https://pytango.readthedocs.io/en/stable/server_api/server.html?highlight=devlong#module-tango.server for + # more details about type conversion Python/numpy -> PyTango if "numpy" not in str(datatype) and datatype != str: raise TypeError("Attribute needs to be a Tango-supported numpy or str type, but has type \"%s\"" % (datatype,)) @@ -33,7 +35,7 @@ class attribute_wrapper(attribute): self.numpy_type = datatype # tango changes our attribute to their representation (E.g numpy.int64 becomes "DevLong64") self.init_value = init_value - max_dim_y = 0 + is_scalar = dims == (1,) # tango doesn't recognise numpy.str_, for consistencies sake we convert it here and hide this from the top level # NOTE: discuss, idk if this is an important detail somewhere else @@ -41,8 +43,12 @@ class attribute_wrapper(attribute): datatype = str # check if not scalar - if isinstance(dims, tuple): - + if is_scalar: + # scalar, just set the single dimension. + # Tango defines a scalar as having dimensions (1,0), see https://pytango.readthedocs.io/en/stable/server_api/attribute.html + max_dim_x = 1 + max_dim_y = 0 + else: # get first dimension max_dim_x = dims[0] @@ -54,9 +60,8 @@ class attribute_wrapper(attribute): max_dim_y = dims[1] # wrap the datatype tuple in another tuple for 2d arrays/images datatype = (datatype,) - else: - # scalar, just set the single dimension - max_dim_x = 1 + else: + max_dim_y = 0 if access == AttrWriteType.READ_WRITE: """ if the attribute is of READ_WRITE type, assign the RW and write function to it"""