Skip to content

Commit af706d0

Browse files
committed
fix: resolve issues identified by pylint
1 parent feb9653 commit af706d0

File tree

3 files changed

+23
-14
lines changed

3 files changed

+23
-14
lines changed

feeph/i2c/burst_handler.py

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,7 @@ def read_register(self, register: int, byte_count: int = 1, max_tries: int = 5)
7777
except RuntimeError as e:
7878
LH.warning("[%s] Unable to read register 0x%02X (%i/%i): %s", __name__, register, cur_try, max_tries, e)
7979
time.sleep(0.001)
80-
else:
81-
raise RuntimeError(f"Unable to read register 0x{register:02X} after {cur_try} attempts. Giving up.")
80+
raise RuntimeError(f"Unable to read register 0x{register:02X} after {cur_try} attempts. Giving up.")
8281

8382
def write_register(self, register: int, value: int, byte_count: int = 1, max_tries: int = 3):
8483
"""
@@ -91,10 +90,9 @@ def write_register(self, register: int, value: int, byte_count: int = 1, max_tri
9190
"""
9291
_validate_register_address(register)
9392
ba = convert_uint_to_bytearry(value, byte_count)
94-
buf = bytearray(1 + len(ba))
95-
buf[0] = register
96-
for i in range(len(ba)):
97-
buf[1+i] = ba[i]
93+
buf = bytearray([register])
94+
for byte in ba:
95+
buf.append(byte)
9896
for cur_try in range(1, 1 + max_tries):
9997
try:
10098
self._i2c_bus.writeto(address=self._i2c_adr, buffer=buf)
@@ -106,8 +104,7 @@ def write_register(self, register: int, value: int, byte_count: int = 1, max_tri
106104
except RuntimeError as e:
107105
LH.warning("[%s] Unable to read register 0x%02X (%i/%i): %s", __name__, register, cur_try, max_tries, e)
108106
time.sleep(0.1)
109-
else:
110-
raise RuntimeError(f"Unable to read register 0x{register:02X} after {cur_try} attempts. Giving up.")
107+
raise RuntimeError(f"Unable to read register 0x{register:02X} after {cur_try} attempts. Giving up.")
111108

112109
# it is unclear if it's possible to have a multi-byte state registers
113110
# (a register write looks exactly like a multi-byte state write)
@@ -135,8 +132,7 @@ def get_state(self, byte_count: int = 1, max_tries: int = 5) -> int:
135132
except RuntimeError as e:
136133
LH.warning("[%s] Unable to read state (%i/%i): %s", __name__, cur_try, max_tries, e)
137134
time.sleep(0.001)
138-
else:
139-
raise RuntimeError(f"Unable to read state after {cur_try} attempts. Giving up.")
135+
raise RuntimeError(f"Unable to read state after {cur_try} attempts. Giving up.")
140136

141137
def set_state(self, value: int, byte_count: int = 1, max_tries: int = 3):
142138
"""
@@ -158,10 +154,9 @@ def set_state(self, value: int, byte_count: int = 1, max_tries: int = 3):
158154
LH.warning("[%s] Failed to write state (%i/%i): %s", __name__, cur_try, max_tries, e)
159155
time.sleep(0.1)
160156
except RuntimeError as e:
161-
LH.warning("[%s] Unable to write state 0x%02X (%i/%i): %s", __name__, cur_try, max_tries, e)
157+
LH.warning("[%s] Unable to write state (%i/%i): %s", __name__, cur_try, max_tries, e)
162158
time.sleep(0.1)
163-
else:
164-
raise RuntimeError(f"Unable to write state after {cur_try} attempts. Giving up.")
159+
raise RuntimeError(f"Unable to write state after {cur_try} attempts. Giving up.")
165160

166161

167162
class BurstHandler:
@@ -182,6 +177,8 @@ def __init__(self, i2c_bus: busio.I2C, i2c_adr: int, timeout_ms: int | None = 50
182177
self._timeout_ms = timeout_ms
183178
else:
184179
raise ValueError("Provided timeout is not a positive integer or 'None'!")
180+
# register '_timestart_ns' - we will populate it later on
181+
self._timestart_ns = 0
185182

186183
def __enter__(self) -> BurstHandle:
187184
"""

feeph/i2c/emulation.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ class EmulatedI2C(busio.I2C):
2121
(e.g. duplicated registers with multiple addresses)
2222
"""
2323

24+
# We intentionally do not call super().init() because we don't want to
25+
# call any of the hardware initialization code. We are only interested
26+
# in being able to claim we are an instance of 'busio.I2C' in case
27+
# something else tries to validate that or has logic tied to it.
28+
# pylint: disable=super-init-not-called
2429
def __init__(self, state: dict[int, dict[int, int]], lock_chance: int = 100):
2530
"""
2631
initialize a simulated I2C bus
@@ -39,7 +44,7 @@ def __init__(self, state: dict[int, dict[int, int]], lock_chance: int = 100):
3944

4045
def try_lock(self) -> bool:
4146
# may randomly fail to acquire a lock
42-
return (random.randint(0, 100) < self._lock_chance)
47+
return random.randint(0, 100) < self._lock_chance
4348

4449
def unlock(self):
4550
pass
@@ -48,6 +53,8 @@ def unlock(self):
4853
# being read/written since the register address must be positive in the
4954
# range 0 ≤ x ≤ 255.
5055

56+
# replicate the signature of busio.I2C
57+
# pylint: disable=too-many-arguments
5158
def readfrom_into(self, address, buffer, *, start=0, end=None, stop=True):
5259
"""
5360
read device state
@@ -59,9 +66,12 @@ def readfrom_into(self, address, buffer, *, start=0, end=None, stop=True):
5966
value = self._state[i2c_device_address][i2c_device_register]
6067
ba = convert_uint_to_bytearry(value, len(buffer))
6168
# copy computed result to output parameter
69+
# pylint: disable=consider-using-enumerate
6270
for i in range(len(buffer)):
6371
buffer[i] = ba[i]
6472

73+
# replicate the signature of busio.I2C
74+
# pylint: disable=too-many-arguments
6575
def writeto(self, address: int, buffer: bytearray, *, start=0, end=None):
6676
"""
6777
write device state or register
@@ -93,5 +103,6 @@ def writeto_then_readfrom(self, address: int, buffer_out: bytearray, buffer_in:
93103
value = self._state[i2c_device_address][i2c_device_register]
94104
ba = convert_uint_to_bytearry(value, len(buffer_in))
95105
# copy computed result to output parameter
106+
# pylint: disable=consider-using-enumerate
96107
for i in range(len(buffer_in)):
97108
buffer_in[i] = ba[i]

tests/test_burst_handler.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import feeph.i2c as sut # sytem under test
99

1010

11+
# pylint: disable=protected-access
1112
class TestBurstHandler(unittest.TestCase):
1213

1314
def test_read_device_register(self):

0 commit comments

Comments
 (0)