Closed ammounce closed 3 months ago
read-only properties can be created by only using fget and doc in the property_definition which is the argument to Property()
. If you try to set a read-only property, an AttributeError is raised with a message that "the property has no setter".
def add_device_ro_property(self, settings):
"""
Adds a read-only property to the class based on a settings dictionary
Parameters
----------
settings : dict
dict containing settings for a read-only property.
Returns
-------
None
"""
self["_{}_settings".format(settings["name"])] = settings
doc_string = "{} : {}".format(
settings["name"], settings["return_type"].__name__
)
property_definition = property(
fget=lambda obj: self.get_instrument_property(obj, settings), doc=doc_string
)
setattr(self.__class__, settings["name"], property_definition)
We can make a separate method for add_device_ro_property()
, or we can modify add_device_property
to make a read-only property if the write_string
key is missing from the settings dictionary.
Currently this is not possible, this also means that the dict keys of index_value, etc. could also not exist or specifically be None (for query only properties).