sandialabs / pyscan

Scientific Measurement Toolbox
https://pyscan.readthedocs.io/en/latest/
MIT License
4 stars 3 forks source link

Properties that are only get or set need to be availble in InstrumentDriver.add_device_property #130

Closed ammounce closed 3 months ago

ammounce commented 3 months ago

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).

mplilly4395 commented 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.