Skip to content

Commit

Permalink
Remove several linter warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
mmouchous-ledger committed Feb 27, 2024
1 parent fa42a9a commit 1b0dcce
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 47 deletions.
13 changes: 13 additions & 0 deletions pystages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
]
10 changes: 6 additions & 4 deletions pystages/cncrouter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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:
Expand Down
5 changes: 4 additions & 1 deletion pystages/corvus.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))

Expand Down
3 changes: 3 additions & 0 deletions pystages/gui/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
from . import gui


__all__ = ["gui"]
1 change: 0 additions & 1 deletion pystages/gui/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion pystages/m3fs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
59 changes: 32 additions & 27 deletions pystages/smc100.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -148,7 +148,7 @@ class State(Enum):
JOGGING_FROM_DISABLE = 0x47


class Error(IntFlag):
class Error(int, Flag):
"""
Information returned when querying positioner error.
"""
Expand All @@ -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:
Expand All @@ -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
)


Expand Down Expand Up @@ -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))

Expand Down Expand Up @@ -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))
Expand All @@ -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):
"""
Expand Down
25 changes: 14 additions & 11 deletions pystages/tic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -108,7 +108,7 @@ class TicCommand(Enum):
START_BOOTLOADER = 0xFF


class TicDirection(Enum):
class TicDirection(int, Enum):
"""Possible directions for homing"""

REVERSE = 0
Expand Down Expand Up @@ -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):
"""
Expand All @@ -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):
"""
Expand All @@ -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:
"""
Expand All @@ -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):
"""
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand All @@ -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",
Expand All @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions pystages/vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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."
)


Expand Down

0 comments on commit 1b0dcce

Please sign in to comment.