Skip to content

Commit 83f1256

Browse files
committed
microscope.devices: fix setting of metaclass ABCMeta for Python 3.
In Python 2, one would set a metaclass via the __metaclass__ attribute but since Python 3 this done via a keyword argument in the list of base classes. This change does it correctly for Python 3. Setting __metaclass__ does nothing on Python 3.
1 parent 401754d commit 83f1256

File tree

1 file changed

+8
-23
lines changed

1 file changed

+8
-23
lines changed

microscope/devices.py

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def device(cls, host, port, conf={}, uid=None):
172172
return dict(cls=cls, host=host, port=int(port), uid=uid, conf=conf)
173173

174174

175-
class FloatingDeviceMixin:
175+
class FloatingDeviceMixin(metaclass=abc.ABCMeta):
176176
"""A mixin for devices that 'float'.
177177
178178
Some SDKs handling multiple devices do not allow for explicit
@@ -181,22 +181,19 @@ class FloatingDeviceMixin:
181181
a mixin which identifies a subclass as floating, and enforces
182182
the implementation of a 'get_id' method.
183183
"""
184-
__metaclass__ = abc.ABCMeta
185-
186184
@abc.abstractmethod
187185
def get_id(self):
188186
"""Return a unique hardware identifier, such as a serial number."""
189187
pass
190188

191189

192-
class Device:
190+
class Device(metaclass=abc.ABCMeta):
193191
"""A base device class. All devices should subclass this class.
194192
195193
Args:
196194
index (int): the index of the device on a shared library.
197195
This argument is added by the deviceserver.
198196
"""
199-
__metaclass__ = abc.ABCMeta
200197

201198
def __init__(self, index=None):
202199
self.enabled = None
@@ -375,7 +372,7 @@ def wrapper(self, *args, **kwargs):
375372
return wrapper
376373

377374

378-
class DataDevice(Device):
375+
class DataDevice(Device, metaclass=abc.ABCMeta):
379376
"""A data capture device.
380377
381378
This class handles a thread to fetch data from a device and dispatch
@@ -390,8 +387,6 @@ class DataDevice(Device):
390387
Derived classes may override __init__, enable and disable, but must
391388
ensure to call this class's implementations as indicated in the docstrings.
392389
"""
393-
__metaclass__ = abc.ABCMeta
394-
395390
def __init__(self, buffer_length=0, **kwargs):
396391
"""Derived.__init__ must call this at some point."""
397392
super().__init__(**kwargs)
@@ -838,7 +833,7 @@ class TriggerMode(Enum):
838833
START = 4
839834

840835

841-
class TriggerTargetMixIn:
836+
class TriggerTargetMixIn(metaclass=abc.ABCMeta):
842837
"""MixIn for Device that may be the target of a hardware trigger.
843838
844839
Subclasses must set a `_trigger_type` and `_trigger_mode` property
@@ -851,8 +846,6 @@ class TriggerTargetMixIn:
851846
supported.
852847
853848
"""
854-
__metaclass__ = abc.ABCMeta
855-
856849
@property
857850
def trigger_mode(self):
858851
return self._trigger_mode
@@ -867,7 +860,7 @@ def set_trigger(self, ttype, tmode):
867860
pass
868861

869862

870-
class SerialDeviceMixIn:
863+
class SerialDeviceMixIn(metaclass=abc.ABCMeta):
871864
"""MixIn for devices that are controlled via serial.
872865
873866
Currently handles the flushing and locking of the comms channel
@@ -877,8 +870,6 @@ class SerialDeviceMixIn:
877870
TODO: add more logic to handle the code duplication of serial
878871
devices.
879872
"""
880-
__metaclass__ = abc.ABCMeta
881-
882873
def __init__(self, **kwargs):
883874
super().__init__(**kwargs)
884875
## TODO: We should probably construct the connection here but
@@ -926,7 +917,7 @@ def wrapper(self, *args, **kwargs):
926917
return wrapper
927918

928919

929-
class DeformableMirror(Device):
920+
class DeformableMirror(Device, metaclass=abc.ABCMeta):
930921
"""Base class for Deformable Mirrors.
931922
932923
There is no method to reset or clear a deformable mirror. While
@@ -944,8 +935,6 @@ class DeformableMirror(Device):
944935
destroying and re-constructing the DeformableMirror object
945936
provides the most obvious solution.
946937
"""
947-
__metaclass__ = abc.ABCMeta
948-
949938
@abc.abstractmethod
950939
def __init__(self, **kwargs) -> None:
951940
"""Constructor.
@@ -1025,9 +1014,7 @@ def _on_shutdown(self) -> None:
10251014
pass
10261015

10271016

1028-
class LaserDevice(Device):
1029-
__metaclass__ = abc.ABCMeta
1030-
1017+
class LaserDevice(Device, metaclass=abc.ABCMeta):
10311018
@abc.abstractmethod
10321019
def __init__(self, **kwargs):
10331020
super().__init__(**kwargs)
@@ -1086,9 +1073,7 @@ def set_power_mw(self, mw):
10861073
self._set_power_mw(mw)
10871074

10881075

1089-
class FilterWheelBase(Device):
1090-
__metaclass__ = abc.ABCMeta
1091-
1076+
class FilterWheelBase(Device, metaclass=abc.ABCMeta):
10921077
def __init__(self, filters=[], positions=0, **kwargs):
10931078
super().__init__(**kwargs)
10941079
if isinstance(filters, dict):

0 commit comments

Comments
 (0)