Skip to content

Commit 862aff1

Browse files
committed
feat: add __repr__ and __eq__
1 parent 3e09134 commit 862aff1

File tree

1 file changed

+26
-2
lines changed

1 file changed

+26
-2
lines changed

spidev/_spi.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,5 +217,29 @@ def __str__(self) -> str:
217217
else:
218218
return f"{self.__class__.__name__}()"
219219

220-
# def __repr__(self):
221-
# args = [f'' for a in ('bus', 'device', 'path', )
220+
def __repr__(self) -> str:
221+
# SpiDev(bus=0, device=1, bits_per_word=8)
222+
args = ", ".join(
223+
f"{a}={getattr(self, a)}"
224+
for a in (
225+
"bus",
226+
"device",
227+
"path",
228+
"mode",
229+
"bits_per_word",
230+
"max_speed_hz",
231+
"read0",
232+
)
233+
if getattr(self, a) is not None
234+
)
235+
return f"{self.__class__.__name__}({args})"
236+
237+
def __eq__(self, other: Any) -> bool:
238+
if not isinstance(other, type(self)):
239+
return False
240+
241+
try:
242+
return self._resolve_path() == other._resolve_path()
243+
except ValueError:
244+
# return False if one of the instances is uninitiated
245+
return False

0 commit comments

Comments
 (0)