Skip to content

Commit 65e4a09

Browse files
rahulraghu94dhoomakethu
authored andcommitted
Adding repr to all clients (#296)
* Added REPR statements for all syncchronous clients * Added Repr tests and pytest cache to gitignore
1 parent 693783b commit 65e4a09

File tree

3 files changed

+40
-3
lines changed

3 files changed

+40
-3
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,3 +30,4 @@ test/__pycache__/
3030
/doc/sphinx/
3131
/doc/html/
3232
/doc/_build/
33+
.pytest_cache/

pymodbus/client/sync.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,13 @@ def __str__(self):
270270
"""
271271
return "ModbusTcpClient(%s:%s)" % (self.host, self.port)
272272

273+
def __repr__(self):
274+
return (
275+
"<{} at {} socket={self.socket}, ipaddr={self.host}, "
276+
"port={self.port}, timeout={self.timeout}>"
277+
).format(self.__class__.__name__, hex(id(self)), self=self)
278+
279+
273280

274281
# --------------------------------------------------------------------------- #
275282
# Modbus UDP Client Transport Implementation
@@ -360,6 +367,11 @@ def __str__(self):
360367
"""
361368
return "ModbusUdpClient(%s:%s)" % (self.host, self.port)
362369

370+
def __repr__(self):
371+
return (
372+
"<{} at {} socket={self.socket}, ipaddr={self.host}, "
373+
"port={self.port}, timeout={self.timeout}>"
374+
).format(self.__class__.__name__, hex(id(self)), self=self)
363375

364376
# --------------------------------------------------------------------------- #
365377
# Modbus Serial Client Transport Implementation
@@ -527,6 +539,12 @@ def __str__(self):
527539
"""
528540
return "ModbusSerialClient(%s baud[%s])" % (self.method, self.baudrate)
529541

542+
def __repr__(self):
543+
return (
544+
"<{} at {} socket={self.socket}, method={self.method}, "
545+
"timeout={self.timeout}>"
546+
).format(self.__class__.__name__, hex(id(self)), self=self)
547+
530548
# --------------------------------------------------------------------------- #
531549
# Exported symbols
532550
# --------------------------------------------------------------------------- #

test/test_client_sync.py

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,13 @@ def testUdpClientRecv(self):
149149
self.assertEqual(b'', client._recv(0))
150150
self.assertEqual(b'\x00' * 4, client._recv(4))
151151

152-
152+
def testUdpClientRepr(self):
153+
client = ModbusUdpClient()
154+
rep = "<{} at {} socket={}, ipaddr={}, port={}, timeout={}>".format(
155+
client.__class__.__name__, hex(id(client)), client.socket,
156+
client.host, client.port, client.timeout
157+
)
158+
self.assertEqual(repr(client), rep)
153159
# -----------------------------------------------------------------------#
154160
# Test TCP Client
155161
# -----------------------------------------------------------------------#
@@ -221,7 +227,13 @@ def testTcpClientRecv(self):
221227
client.socket.timeout = 0.1
222228
self.assertIn(b'\x00', client._recv(None))
223229

224-
230+
def testSerialClientRpr(self):
231+
client = ModbusTcpClient()
232+
rep = "<{} at {} socket={}, ipaddr={}, port={}, timeout={}>".format(
233+
client.__class__.__name__, hex(id(client)), client.socket,
234+
client.host, client.port, client.timeout
235+
)
236+
self.assertEqual(repr(client), rep)
225237

226238
# -----------------------------------------------------------------------#
227239
# Test Serial Client
@@ -327,7 +339,13 @@ def testSerialClientRecv(self):
327339
client.socket.timeout = 0
328340
self.assertEqual(b'', client._recv(0))
329341

330-
342+
def testSerialClientRepr(self):
343+
client = ModbusSerialClient()
344+
rep = "<{} at {} socket={}, method={}, timeout={}>".format(
345+
client.__class__.__name__, hex(id(client)), client.socket,
346+
client.method, client.timeout
347+
)
348+
self.assertEqual(repr(client), rep)
331349
# ---------------------------------------------------------------------------#
332350
# Main
333351
# ---------------------------------------------------------------------------#

0 commit comments

Comments
 (0)