Python Api for deleting/removing dynamic attributes

Hi All,

How can we delete a dynamic attribute in a DS written in python? Is there any api in PyTango?
I need to create an attribute on a particular condition and delete it as the condition goes away. I could create dynamic attributes on the conditions but I did not find a way to delete them. Kindly help!
Regards,
TCS_GMRT
There is a device.remove_attribute method, although there might be some limitations I'm not aware of.

Cheers,
/Vincent
Hi Vincent,

Thank you so much. I could delete the dynamic attribute whenever my processing was done.
However, I am facing problem in updating/writing the attribute during processing.
I have created an attribute as follows:

def create(srcid):
attr = PyTango.Attr(srcid, PyTango.DevString, PyTango.READ_WRITE)
self.add_attribute(attr, self.read_general, self.write_general)

def write_general(self, attr):
name = attr.get_name()
data = attr.get_write_value()
self.info_stream("Writing into attribute %s the value %s", name, data)

def read_general(self, attr):
self.info_stream("Reading attribute %s", attr.get_name())
attr.set_value('99.99')

I could create the attribute with initial value '99.99'. I created device proxy of this device and used write_attribute(srcid,'False') to write it. When I am reading the attribute after the write action, I am still getting value as '99.99'

Please tell me if I am doing anything wrong.
Regards,
TCS_GMRT
I am not sure I understand what you're trying to achieve, could you provide a Minimal, Complete, and Verifiable example?
Hi Vincent,

I have a tango command 'launch' that creates a different process to execute scripts. It takes 'name' and 'script_id' as arguments. I want to create a dynamic attribute named after respective script_id whenever 'launch' command is called. I followed http://www.tango-controls.org/community/forum/c/development/python/pytango-dynamic-attributes/ to make one as follows:

class MyDevice(Device_4_impl):

    def launch(self, name, script_id):
        attr = PyTango.Attr(script_id, PyTango.DevString, PyTango.READ_WRITE)
        self.add_attribute(attr, self.read_general, self.write_general)
        """ code to create a process to execute script """

    def write_general(self, attr):
        name = attr.get_name()
        data = attr.get_write_value()
        self.info_stream("Writing into attribute %s the value %s", name, data)

    def read_general(self, attr):
        self.info_stream("Reading attribute %s", attr.get_name())
        attr.set_value('99.99')

So, whenever I call 'launch' command on my/test/device (device on instance of MyDevice), a dynamic attribute is created at my/test/device with name that was passed as script_id argument. It's values is also initialized to '99.99'. It further creates a process and executes a script. The script also receives script_id as its argument through sys.argv.

I want to pass various values to this dynamic attribute through the script. So I wrote in my script test.py:

import PyTango
import sys

script_id = sys.argv[1]
dev_proxy = PyTango.DeviceProxy('my/test/device')
dev_proxy.write_Attribute(script_id, 'code11')

I do not get any error or exception but the updated value is not reflected on jive. I also added dev_proxy.read_attribute(script_id) before and after writing attribute in the script. Both values return '99.99'

Please tell me if I am doing anything wrong. I need to update this dynamic attribute value throughout the life of the script.
Regards,
TCS_GMRT
Edited 5 years ago
Hi TCS_GMRT,

Dynamic tango attribute behaviour works just like any other standard tango attribute.

In your case it seems that you are looking for a pure software attribute semantics. I propose you initialize the attribute value when you create the dynamic attribute with a default value.
When a client does a write, the value it should be updated and when a client asks to read the same attribute it should simply return the last written value (or the default if there was no write before).

Example:



class MyDevice(Device_4_impl):

    def init_device(self):
        # … 
        self._dynamic_attrs = {}

    def launch(self, name, script_id):
        attr = PyTango.Attr(script_id, PyTango.DevString, PyTango.READ_WRITE)
        self.add_attribute(attr, self.read_general, self.write_general)
        self._dynamic_attrs[script_id] = '99.99'
        """ code to create a process to execute script """

    def write_general(self, attr):
        name = attr.get_name()
        data = attr.get_write_value()
        self._dynamic_attrs[name] = data
        self.info_stream("Writing into attribute %s the value %s", name, data)

    def read_general(self, attr):
        name = attr.get_name()
        self.info_stream("Reading attribute %s", name)
        attr.set_value(self._dynamic_attrs[name])

Hope it helps
Tiago
Edited 5 years ago
Thank you so much Tiago! It worked!
Regards,
TCS_GMRT
 
Register or login to create to post a reply.