Attribute's fget don't accept functools.partial
Created by: stanislaw47
Hi, I've been using PyTango for a while and I've found an error. In my device, there's a list. I want my attributes to read values from that list. Since there's a lot of them, I've decided to use functools.partial
and fget
parameter to write less code. Example:
from tango.server import Device, attribute
from functools import partial
class MyDevice(Device):
my_list = [5, 10]
def read_from_list(self, index):
return self.my_list[index]
read_first_attr = partial(read_from_list, index=0)
FirstAttribute = attribute(
dtype=float,
fget="read_first_attr"
)
read_second_attr = partial(read_from_list, index=1)
SecondAttribute = attribute(
dtype=float,
fget="read_second_attr"
)
However, when I try to do that, I receive this execption:
cf71aecf86b9:python -u /opt/project/adam/example.py
Traceback (most recent call last):
File "/opt/project/adam/example.py", line 5, in <module>
class MyDevice(Device):
File "/usr/lib64/python2.7/site-packages/tango/server.py", line 534, in __new__
_init_tango_device_klass(cls, dct)
File "/usr/lib64/python2.7/site-packages/tango/server.py", line 475, in _init_tango_device_klass
tango_device_klass, attrs=attrs)
File "/usr/lib64/python2.7/site-packages/tango/server.py", line 437, in __create_tango_deviceclass_klass
__patch_attr_methods(tango_device_klass, attr_obj)
File "/usr/lib64/python2.7/site-packages/tango/server.py", line 213, in __patch_attr_methods
__patch_read_method(tango_device_klass, attribute)
File "/usr/lib64/python2.7/site-packages/tango/server.py", line 151, in __patch_read_method
read_attr = _get_wrapped_read_method(attribute, read_method)
File "/usr/lib64/python2.7/site-packages/tango/server.py", line 98, in _get_wrapped_read_method
read_args = inspect.getargspec(read_method)
File "/usr/lib64/python2.7/inspect.py", line 815, in getargspec
raise TypeError('{!r} is not a Python function'.format(func))
TypeError: <functools.partial object at 0x2d41628> is not a Python function
Process finished with exit code 1
It looks like functools.partial
is not a function and thus, it's not possible to assign it to fget
. Of course, I can workaround this by defining function just to read that one attribute... but this is what I want to avoid. It would be more pythonic to be able to do this, since there would be no need to type bunch of excessive code.
Edited by Anton Joubert