BioImager  3.9.1
A .NET microscopy imaging library. Supports various microscopes by using imported libraries & GUI automation. Supported libraries include PriorĀ® & ZeissĀ® & all devices supported by Micromanager 2.0 and python-microscope.
Loading...
Searching...
No Matches
microscope.abc._Setting Class Reference

Public Member Functions

None __init__ (self, str name, str dtype, typing.Optional[typing.Callable[[], typing.Any]] get_func, typing.Optional[typing.Callable[[typing.Any], None]] set_func=None, typing.Any values=None, typing.Optional[typing.Callable[[], bool]] readonly=None)
 
def describe (self)
 
def get (self)
 
bool readonly (self)
 
None set (self, value)
 
def values (self)
 

Public Attributes

 name
 
 dtype
 

Detailed Description

Create a setting.

Args:
    name: the setting's name.
    dtype: a data type from `"int"`, `"float"`, `"bool"`,
        `"enum"`, or `"str"` (see `DTYPES`).
    get_func: a function to get the current value.
    set_func: a function to set the value.
    values: a description of allowed values dependent on dtype, or
        function that returns a description.
    readonly: an optional function to indicate if the setting is
        readonly.  A setting may be readonly temporarily, so this
        function will return `True` or `False` to indicate its
        current state.  If set to no `None` (default), then its
        value will be dependent on the value of `set_func`.

A client needs some way of knowing a setting name and data type,
retrieving the current value and, if settable, a way to retrieve
allowable values, and set the value.

Setters and getters accept or return:

* the setting value for int, float, bool and str;
* the setting index into a list, dict or Enum type for enum.

.. todo::

    refactor into subclasses to avoid if isinstance .. elif
    .. else.  Settings classes should be private: devices should
    use a factory method rather than instantiate settings
    directly; most already use add_setting for this.

Definition at line 64 of file abc.py.

Constructor & Destructor Documentation

◆ __init__()

None microscope.abc._Setting.__init__ (   self,
str  name,
str  dtype,
typing.Optional[typing.Callable[[], typing.Any]]  get_func,
typing.Optional[typing.Callable[[typing.Any], None]]   set_func = None,
typing.Any   values = None,
typing.Optional[typing.Callable[[], bool]]   readonly = None 
)

Definition at line 99 of file abc.py.

107 ) -> None:
108 self.name = name
109 if dtype not in DTYPES:
110 raise ValueError("Unsupported dtype.")
111 elif not (isinstance(values, DTYPES[dtype]) or callable(values)):
112 raise TypeError(
113 "Invalid values type for %s '%s': expected function or %s"
114 % (dtype, name, DTYPES[dtype])
115 )
116 self.dtype = dtype
117 self._get = get_func
118 self._values = values
119 self._last_written = None
120 if self._get is not None:
121 self._set = set_func
122 else:
123 # Cache last written value for write-only settings.
124 def w(value):
125 self._last_written = value
126 set_func(value)
127
128 self._set = w
129
130 if readonly is None:
131 if self._set is None:
132 self._readonly = lambda: True
133 else:
134 self._readonly = lambda: False
135 else:
136 if self._set is None:
137 raise ValueError(
138 "`readonly` is not `None` but `set_func` is `None`"
139 )
140 else:
141 self._readonly = readonly
142

Member Function Documentation

◆ describe()

def microscope.abc._Setting.describe (   self)

Definition at line 143 of file abc.py.

143 def describe(self):
144 return {
145 "type": self.dtype,
146 "values": self.values(),
147 "readonly": self.readonly(),
148 "cached": self._last_written is not None,
149 }
150

◆ get()

def microscope.abc._Setting.get (   self)

Definition at line 151 of file abc.py.

151 def get(self):
152 if self._get is not None:
153 value = self._get()
154 else:
155 value = self._last_written
156 if isinstance(self._values, EnumMeta):
157 return self._values(value).value
158 else:
159 return value
160

◆ readonly()

bool microscope.abc._Setting.readonly (   self)

Definition at line 161 of file abc.py.

161 def readonly(self) -> bool:
162 return self._readonly()
163

◆ set()

None microscope.abc._Setting.set (   self,
  value 
)
Set a setting.

Definition at line 164 of file abc.py.

164 def set(self, value) -> None:
165 """Set a setting."""
166 if self._set is None:
167 raise NotImplementedError()
168 # TODO further validation.
169 if isinstance(self._values, EnumMeta):
170 value = self._values(value)
171 self._set(value)
172

References microscope.abc._Setting._set, microscope.abc._Setting._values, microscope.abc._Setting.dtype, and microscope.cameras.pvcam.PVParam.dtype.

◆ values()

def microscope.abc._Setting.values (   self)

Definition at line 173 of file abc.py.

173 def values(self):
174 if isinstance(self._values, EnumMeta):
175 return [(v.value, v.name) for v in self._values]
176 values = _call_if_callable(self._values)
177 if values is not None:
178 if self.dtype == "enum":
179 if isinstance(values, dict):
180 return list(values.items())
181 else:
182 # self._values is a list or tuple
183 return list(enumerate(values))
184 elif self._values is not None:
185 return values
186
187

Referenced by microscope.cameras.pvcam.PVEnumParam.set_value().

Member Data Documentation

◆ dtype

microscope.abc._Setting.dtype

◆ name


The documentation for this class was generated from the following file: