From 1b0dccede9789c1b4750429fed6f8a404f1ca3d0 Mon Sep 17 00:00:00 2001 From: Michael Mouchous Date: Tue, 27 Feb 2024 12:05:31 +0100 Subject: [PATCH] Remove several linter warnings --- pystages/__init__.py | 13 +++++++++ pystages/cncrouter.py | 10 ++++--- pystages/corvus.py | 5 +++- pystages/gui/__init__.py | 3 ++ pystages/gui/__main__.py | 1 - pystages/m3fs.py | 4 ++- pystages/smc100.py | 59 ++++++++++++++++++++++------------------ pystages/tic.py | 25 +++++++++-------- pystages/vector.py | 4 +-- 9 files changed, 77 insertions(+), 47 deletions(-) diff --git a/pystages/__init__.py b/pystages/__init__.py index 713b0b9..1a80f2c 100644 --- a/pystages/__init__.py +++ b/pystages/__init__.py @@ -24,3 +24,16 @@ from .vector import Vector from .tic import Tic, TicDirection from .cncrouter import CNCRouter, CNCStatus + +__all__ = [ + "Stage", + "Corvus", + "M3FS", + "SMC100", + "Autofocus", + "Vector", + "Tic", + "TicDirection", + "CNCRouter", + "CNCStatus", +] diff --git a/pystages/cncrouter.py b/pystages/cncrouter.py index 4860f71..5ca9e16 100644 --- a/pystages/cncrouter.py +++ b/pystages/cncrouter.py @@ -224,9 +224,9 @@ def receive_lines(self, until: str = "ok") -> List[str]: list. """ lines = [] - while (l := self.serial.readline().strip().decode()) != until: - if len(l): - lines.append(l) + while (line := self.serial.readline().strip().decode()) != until: + if len(line): + lines.append(line) return lines def receive(self) -> str: @@ -276,7 +276,9 @@ def position(self) -> Vector: @position.setter def position(self, value: Vector): # To check dimension and range of the given value - super(__class__, self.__class__).position.fset(self, value) # type: ignore + pos_setter = Stage.position.fset + assert pos_setter is not None + pos_setter(self, value) command = f"G0 X{value.x}" if len(value) > 1: diff --git a/pystages/corvus.py b/pystages/corvus.py index 24d7c0a..d96df95 100644 --- a/pystages/corvus.py +++ b/pystages/corvus.py @@ -181,7 +181,10 @@ def position(self): @position.setter def position(self, value: Vector): # To check dimension and range of the given value - super(__class__, self.__class__).position.fset(self, value) # type: ignore + pos_setter = Stage.position.fset + assert pos_setter is not None + pos_setter(self, value) + self.send("3 setdim") self.send("{0} {1} {2} move".format(value.x, value.y, value.z)) diff --git a/pystages/gui/__init__.py b/pystages/gui/__init__.py index 17289c7..287320a 100644 --- a/pystages/gui/__init__.py +++ b/pystages/gui/__init__.py @@ -1 +1,4 @@ from . import gui + + +__all__ = ["gui"] diff --git a/pystages/gui/__main__.py b/pystages/gui/__main__.py index b5d4ecf..6dbad8a 100644 --- a/pystages/gui/__main__.py +++ b/pystages/gui/__main__.py @@ -2,7 +2,6 @@ import sys from PyQt6.QtGui import QIcon, QPalette, QColor from .util import resource_path -import sys from .gui import StageWindow app = QApplication(sys.argv) diff --git a/pystages/m3fs.py b/pystages/m3fs.py index 43a5ac2..10e1a4b 100644 --- a/pystages/m3fs.py +++ b/pystages/m3fs.py @@ -186,7 +186,9 @@ def position(self) -> Vector: @position.setter def position(self, value: Vector): # To check dimension and range of the given value - super(__class__, self.__class__).position.fset(self, value) # type: ignore + pos_setter = Stage.position.fset + assert pos_setter is not None + pos_setter(self, value) val = round(value.x / self.resolution_um).to_bytes(4, "big", signed=True) self.command(M3FSCommand.MOVE_TO_TARGET, hexlify(val).decode()) diff --git a/pystages/smc100.py b/pystages/smc100.py index cc74144..5623db4 100644 --- a/pystages/smc100.py +++ b/pystages/smc100.py @@ -18,11 +18,11 @@ # written by Olivier Hériveaux, Manuel San Pedro and Michaël Mouchous -import serial +import serial.serialutil import time from .vector import Vector from .exceptions import ProtocolError, ConnectionFailure -from enum import Enum, IntFlag +from enum import Enum, Flag from typing import Optional, List from .stage import Stage @@ -113,12 +113,12 @@ def response(self, address: Optional[int], command: str) -> str: """ query_string = f'{"" if address is None else address}{command}' res = self.receive() - if res[: len(query_string)] != query_string: + if res is None or res[: len(query_string)] != query_string: raise ProtocolError(query_string, res) return res[len(query_string) :] -class State(Enum): +class State(int, Enum): """ Possible controller states. The values in this enumeration corresponds to the values returned by the @@ -148,7 +148,7 @@ class State(Enum): JOGGING_FROM_DISABLE = 0x47 -class Error(IntFlag): +class Error(int, Flag): """ Information returned when querying positioner error. """ @@ -171,8 +171,8 @@ class ErrorAndState: Information returned when querying positioner error and controller state. """ - state = None - error = None + state = State.NOT_REFERENCED_FROM_RESET + error = Error.NO_ERROR @property def is_referenced(self) -> bool: @@ -183,41 +183,39 @@ def is_referenced(self) -> bool: raise RuntimeError("state not available") else: return not ( - (self.state.value >= State.NOT_REFERENCED_FROM_RESET.value) - and (self.state.value <= State.NOT_REFERENCED_FROM_JOGGING.value) + (self.state >= State.NOT_REFERENCED_FROM_RESET) + and (self.state <= State.NOT_REFERENCED_FROM_JOGGING) ) @property def is_ready(self) -> bool: """:return: True if state is one of READY_x states.""" - return (self.state.value >= State.READY_FROM_HOMING.value) and ( - self.state.value <= State.READY_FROM_JOGGING.value + return (self.state >= State.READY_FROM_HOMING) and ( + self.state <= State.READY_FROM_JOGGING ) @property def is_moving(self) -> bool: """:return: True if state is MOVING.""" - return self.state.value == State.MOVING.value + return self.state == State.MOVING @property def is_homing(self) -> bool: """:return: True if state is one of HOMING_x states.""" - return (self.state.value >= State.HOMING_RS232.value) and ( - self.state.value <= State.HOMING_SMCRC.value - ) + return (self.state >= State.HOMING_RS232) and (self.state <= State.HOMING_SMCRC) @property - def is_jogging(self): + def is_jogging(self) -> bool: """:return: True if state is one of JOGGING_x states.""" - return (self.state.value >= State.JOGGING_FROM_READY.value) and ( - self.state.value <= State.JOGGING_FROM_DISABLE.value + return (self.state >= State.JOGGING_FROM_READY) and ( + self.state <= State.JOGGING_FROM_DISABLE ) @property - def is_disabled(self): + def is_disabled(self) -> bool: """:return: True if state is one of DISABLE_x states.""" - return (self.state.value >= State.DISABLE_FROM_READY.value) and ( - self.state.value <= State.DISABLE_FROM_JOGGING.value + return (self.state >= State.DISABLE_FROM_READY) and ( + self.state <= State.DISABLE_FROM_JOGGING ) @@ -261,20 +259,24 @@ def position(self): # It is faster to send all the request and then get all the responses. # This reduces a lot the latency. for i, addr in enumerate(self.addresses): - r = self.link.query(addr, "TP", lazy_res=False) - val = float(r) + res = self.link.query(addr, "TP", lazy_res=False) + if res is None: + raise ProtocolError("TP") + val = float(res) result[i] = val return result @position.setter def position(self, value: Vector): # To check dimension and range of the given value - super(__class__, self.__class__).position.fset(self, value) + pos_setter = Stage.position.fset + assert pos_setter is not None + pos_setter(self, value) # Enable the motors self.is_disabled = False commands = [] - for position, addr in zip(value, self.addresses): + for position, addr in zip(value.data, self.addresses): commands.append(f"{addr}PA{position:.5f}") self.link.send(None, "\r\n".join(commands)) @@ -318,7 +320,7 @@ def get_error_and_state(self, addr: int): :return: Current error and state, in a ErrorAndState instance. """ res = self.link.query(addr, "TS") - if len(res) != 6: + if res is None or len(res) != 6: raise ProtocolError("TS", res) result = ErrorAndState() result.error = Error(int(res[:4], 16)) @@ -340,7 +342,10 @@ def controller_address(self, addr: int): """ Get controller's RS-485 address. int in [2, 31]. """ - return int(self.link.query(addr, "SA")) + res = self.link.query(addr, "SA") + if res is None: + raise ProtocolError("SA") + return int(res) def set_controller_address(self, addr: int, value: int): """ diff --git a/pystages/tic.py b/pystages/tic.py index 06cefc7..4400d35 100644 --- a/pystages/tic.py +++ b/pystages/tic.py @@ -26,7 +26,7 @@ from .exceptions import ConnectionFailure -class TicVariable(Enum): +class TicVariable(tuple[int, int, bool], Enum): """ Variables which can be read using GET_VARIABLE command. https://www.pololu.com/docs/0J71/7 @@ -74,7 +74,7 @@ class TicVariable(Enum): LAST_HP_DRIVER_ERRORS = (0xFF, 1, False) -class TicCommand(Enum): +class TicCommand(int, Enum): """ Command codes for Polulu Tic Stepper Motor Controller. https://www.pololu.com/docs/0J71/8 @@ -108,7 +108,7 @@ class TicCommand(Enum): START_BOOTLOADER = 0xFF -class TicDirection(Enum): +class TicDirection(int, Enum): """Possible directions for homing""" REVERSE = 0 @@ -140,7 +140,7 @@ def quick(self, command: TicCommand): :param command: Command. """ - self.dev.ctrl_transfer(0x40, command.value, 0, 0, 0) + self.dev.ctrl_transfer(0x40, command, 0, 0, 0) def write_7(self, command: TicCommand, data: int): """ @@ -149,7 +149,7 @@ def write_7(self, command: TicCommand, data: int): :param command: Command. :param data: Value to be written. """ - self.dev.ctrl_transfer(0x40, command.value, data, 0, 0) + self.dev.ctrl_transfer(0x40, command, data, 0, 0) def write_32(self, command: TicCommand, data: int): """ @@ -158,7 +158,7 @@ def write_32(self, command: TicCommand, data: int): :param command: Command code. :param data: Value to be written. """ - self.dev.ctrl_transfer(0x40, command.value, data & 0xFFFF, data >> 16, 0) + self.dev.ctrl_transfer(0x40, command, data & 0xFFFF, data >> 16, 0) def block_read(self, command: TicCommand, offset, length) -> bytes: """ @@ -168,7 +168,7 @@ def block_read(self, command: TicCommand, offset, length) -> bytes: :param offset: Data offset. :param length: Data length. """ - return bytes(self.dev.ctrl_transfer(0xC0, command.value, 0, offset, length)) + return bytes(self.dev.ctrl_transfer(0xC0, command, 0, offset, length)) def set_setting(self, command: TicCommand, data, offset): """ @@ -178,7 +178,7 @@ def set_setting(self, command: TicCommand, data, offset): :param data: Value to be written. :param offset: Write offset. """ - self.dev.ctrl_transfer(0x40, command.value, data, offset, 0) + self.dev.ctrl_transfer(0x40, command, data, offset, 0) def energize(self): self.quick(TicCommand.ENERGIZE) @@ -207,7 +207,7 @@ def go_home(self, direction: TicDirection, wait: bool = True): :param wait: If True, wait for homing procedure end. """ self.exit_safe_start() - self.write_7(TicCommand.GO_HOME, direction.value) + self.write_7(TicCommand.GO_HOME, direction) if wait: while self.get_variable(TicVariable.MISC_FLAGS) & (1 << 4): self.exit_safe_start() @@ -220,7 +220,7 @@ def set_target_velocity(self, velocity: int): self.write_32(TicCommand.SET_TARGET_VELOCITY, velocity) def get_variable(self, variable: TicVariable) -> int: - offset, length, signed = variable.value + offset, length, signed = variable return int.from_bytes( self.block_read(TicCommand.GET_VARIABLE, offset, length), "little", @@ -240,7 +240,10 @@ def position(self) -> Vector: @position.setter def position(self, value: Vector): # To check dimension and range of the given value - super(__class__, self.__class__).position.fset(self, value) # type: ignore + pos_setter = Stage.position.fset + assert pos_setter is not None + pos_setter(self, value) + self.target_position = value.x while self.position.x != value.x: sleep(self.poll_interval) diff --git a/pystages/vector.py b/pystages/vector.py index f97083f..546cfa5 100644 --- a/pystages/vector.py +++ b/pystages/vector.py @@ -163,7 +163,7 @@ def __mul__(self, other: Union["Vector", int, float]): result = Vector(dim=dim) if len(other) != dim: - raise ValueError(f"Incorrect vector size") + raise ValueError("Incorrect vector size") for i in range(dim): result[i] = self[i] * other[i] return result @@ -173,7 +173,7 @@ def __truediv__(self, other): return self * (1.0 / other) else: raise TypeError( - f"Incorrect type for second operand. int or float is expected." + "Incorrect type for second operand. int or float is expected." )