Skip to content

Commit 91573b6

Browse files
Lint and cleanup /examples (#671)
lint /examples per PEP8 Co-authored-by: jan Iversen <[email protected]>
1 parent 00f7184 commit 91573b6

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+514
-542
lines changed

.pylintrc

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[BASIC]
2+
3+
# Good variable names which should always be accepted, separated by a comma
4+
good-names=rr,rq
5+

examples/common/async_asyncio_client.py

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -68,27 +68,27 @@ async def start_async_test(client):
6868
rq = await client.write_coil(0, True, unit=UNIT)
6969
rr = await client.read_coils(0, 1, unit=UNIT)
7070
assert(rq.function_code < 0x80) # test that we are not an error
71-
assert(rr.bits[0] == True) # test the expected value
71+
assert(rr.bits[0]) # test the expected value
7272

7373
log.debug("Write to multiple coils and read back- test 1")
74-
rq = await client.write_coils(1, [True]*8, unit=UNIT)
74+
rq = await client.write_coils(1, [True] * 8, unit=UNIT)
7575
assert(rq.function_code < 0x80) # test that we are not an error
7676
rr = await client.read_coils(1, 21, unit=UNIT)
7777
assert(rr.function_code < 0x80) # test that we are not an error
78-
resp = [True]*21
78+
resp = [True] * 21
7979

8080
# If the returned output quantity is not a multiple of eight,
8181
# the remaining bits in the final data byte will be padded with zeros
8282
# (toward the high order end of the byte).
8383

84-
resp.extend([False]*3)
84+
resp.extend([False] * 3)
8585
assert(rr.bits == resp) # test the expected value
8686

8787
log.debug("Write to multiple coils and read back - test 2")
88-
rq = await client.write_coils(1, [False]*8, unit=UNIT)
88+
rq = await client.write_coils(1, [False] * 8, unit=UNIT)
8989
rr = await client.read_coils(1, 8, unit=UNIT)
9090
assert(rq.function_code < 0x80) # test that we are not an error
91-
assert(rr.bits == [False]*8) # test the expected value
91+
assert(rr.bits == [False] * 8) # test the expected value
9292

9393
log.debug("Read discrete inputs")
9494
rr = await client.read_discrete_inputs(0, 8, unit=UNIT)
@@ -101,27 +101,27 @@ async def start_async_test(client):
101101
assert(rr.registers[0] == 10) # test the expected value
102102

103103
log.debug("Write to multiple holding registers and read back")
104-
rq = await client.write_registers(1, [10]*8, unit=UNIT)
104+
rq = await client.write_registers(1, [10] * 8, unit=UNIT)
105105
rr = await client.read_holding_registers(1, 8, unit=UNIT)
106106
assert(rq.function_code < 0x80) # test that we are not an error
107-
assert(rr.registers == [10]*8) # test the expected value
107+
assert(rr.registers == [10] * 8) # test the expected value
108108

109109
log.debug("Read input registers")
110110
rr = await client.read_input_registers(1, 8, unit=UNIT)
111111
assert(rq.function_code < 0x80) # test that we are not an error
112112

113113
arguments = {
114-
'read_address': 1,
115-
'read_count': 8,
116-
'write_address': 1,
117-
'write_registers': [20]*8,
114+
'read_address': 1,
115+
'read_count': 8,
116+
'write_address': 1,
117+
'write_registers': [20] * 8,
118118
}
119119
log.debug("Read write registeres simulataneously")
120120
rq = await client.readwrite_registers(unit=UNIT, **arguments)
121121
rr = await client.read_holding_registers(1, 8, unit=UNIT)
122122
assert(rq.function_code < 0x80) # test that we are not an error
123-
assert(rq.registers == [20]*8) # test the expected value
124-
assert(rr.registers == [20]*8) # test the expected value
123+
assert(rq.registers == [20] * 8) # test the expected value
124+
assert(rr.registers == [20] * 8) # test the expected value
125125
await asyncio.sleep(1)
126126

127127

examples/common/async_asyncio_serial_client.py

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ async def start_async_test(client):
4444
# which defaults to `0x00`
4545
# ----------------------------------------------------------------------- #
4646
try:
47-
# ----------------------------------------------------------------------- #
47+
# ----------------------------------------------------------------------- #
4848
# example requests
49-
# ----------------------------------------------------------------------- #
49+
# ----------------------------------------------------------------------- #
5050
# simply call the methods that you would like to use.
5151
# An example session is displayed below along with some assert checks.
5252
# Note that some modbus implementations differentiate holding/
@@ -56,32 +56,32 @@ async def start_async_test(client):
5656
# so a change to one is a change to the other.
5757
# Keep both of these cases in mind when testing as the following will
5858
# _only_ pass with the supplied asynchronous modbus server (script supplied).
59-
# ----------------------------------------------------------------------- #
59+
# ----------------------------------------------------------------------- #
6060
log.debug("Write to a Coil and read back")
6161
rq = await client.write_coil(0, True, unit=UNIT)
6262
rr = await client.read_coils(0, 1, unit=UNIT)
6363
assert(rq.function_code < 0x80) # test that we are not an error
64-
assert(rr.bits[0] == True) # test the expected value
64+
assert(rr.bits[0]) # test the expected value
6565

6666
log.debug("Write to multiple coils and read back- test 1")
67-
rq = await client.write_coils(1, [True]*8, unit=UNIT)
67+
rq = await client.write_coils(1, [True] * 8, unit=UNIT)
6868
assert(rq.function_code < 0x80) # test that we are not an error
6969
rr = await client.read_coils(1, 21, unit=UNIT)
7070
assert(rr.function_code < 0x80) # test that we are not an error
71-
resp = [True]*21
71+
resp = [True] * 21
7272

7373
# If the returned output quantity is not a multiple of eight,
7474
# the remaining bits in the final data byte will be padded with zeros
7575
# (toward the high order end of the byte).
7676

77-
resp.extend([False]*3)
77+
resp.extend([False] * 3)
7878
assert(rr.bits == resp) # test the expected value
7979

8080
log.debug("Write to multiple coils and read back - test 2")
81-
rq = await client.write_coils(1, [False]*8, unit=UNIT)
81+
rq = await client.write_coils(1, [False] * 8, unit=UNIT)
8282
rr = await client.read_coils(1, 8, unit=UNIT)
8383
assert(rq.function_code < 0x80) # test that we are not an error
84-
assert(rr.bits == [False]*8) # test the expected value
84+
assert(rr.bits == [False] * 8) # test the expected value
8585

8686
log.debug("Read discrete inputs")
8787
rr = await client.read_discrete_inputs(0, 8, unit=UNIT)
@@ -94,27 +94,27 @@ async def start_async_test(client):
9494
assert(rr.registers[0] == 10) # test the expected value
9595

9696
log.debug("Write to multiple holding registers and read back")
97-
rq = await client.write_registers(1, [10]*8, unit=UNIT)
97+
rq = await client.write_registers(1, [10] * 8, unit=UNIT)
9898
rr = await client.read_holding_registers(1, 8, unit=UNIT)
9999
assert(rq.function_code < 0x80) # test that we are not an error
100-
assert(rr.registers == [10]*8) # test the expected value
100+
assert(rr.registers == [10] * 8) # test the expected value
101101

102102
log.debug("Read input registers")
103103
rr = await client.read_input_registers(1, 8, unit=UNIT)
104104
assert(rq.function_code < 0x80) # test that we are not an error
105105

106106
arguments = {
107-
'read_address': 1,
108-
'read_count': 8,
109-
'write_address': 1,
110-
'write_registers': [20]*8,
107+
'read_address': 1,
108+
'read_count': 8,
109+
'write_address': 1,
110+
'write_registers': [20] * 8,
111111
}
112112
log.debug("Read write registers simulataneously")
113113
rq = await client.readwrite_registers(unit=UNIT, **arguments)
114114
rr = await client.read_holding_registers(1, 8, unit=UNIT)
115115
assert(rq.function_code < 0x80) # test that we are not an error
116-
assert(rq.registers == [20]*8) # test the expected value
117-
assert(rr.registers == [20]*8) # test the expected value
116+
assert(rq.registers == [20] * 8) # test the expected value
117+
assert(rr.registers == [20] * 8) # test the expected value
118118
except Exception as e:
119119
log.exception(e)
120120
client.transport.close()
@@ -132,4 +132,3 @@ async def start_async_test(client):
132132
baudrate=9600, method="rtu")
133133
loop.run_until_complete(start_async_test(client.protocol))
134134
loop.close()
135-

examples/common/async_tornado_client.py

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,12 @@ def beginAsynchronousTest(client, protocol):
8181
dassert(rq, lambda r: r.function_code < 0x80) # test for no error
8282
dassert(rr, _print) # test the expected value
8383

84-
rq = client.write_coils(1, [False]*8, unit=UNIT)
84+
rq = client.write_coils(1, [False] * 8, unit=UNIT)
8585
rr = client.read_coils(1, 8, unit=UNIT)
8686
dassert(rq, lambda r: r.function_code < 0x80) # test for no error
8787
dassert(rr, _print) # test the expected value
8888

89-
rq = client.write_coils(1, [False]*8, unit=UNIT)
89+
rq = client.write_coils(1, [False] * 8, unit=UNIT)
9090
rr = client.read_discrete_inputs(1, 8, unit=UNIT)
9191
dassert(rq, lambda r: r.function_code < 0x80) # test for no error
9292
dassert(rr, _print) # test the expected value
@@ -96,20 +96,20 @@ def beginAsynchronousTest(client, protocol):
9696
dassert(rq, lambda r: r.function_code < 0x80) # test for no error
9797
dassert(rr, _print) # test the expected value
9898

99-
rq = client.write_registers(1, [10]*8, unit=UNIT)
99+
rq = client.write_registers(1, [10] * 8, unit=UNIT)
100100
rr = client.read_input_registers(1, 8, unit=UNIT)
101101
dassert(rq, lambda r: r.function_code < 0x80) # test for no error
102102
dassert(rr, _print) # test the expected value
103103

104104
arguments = {
105-
'read_address': 1,
106-
'read_count': 8,
107-
'write_address': 1,
108-
'write_registers': [20]*8,
105+
'read_address': 1,
106+
'read_count': 8,
107+
'write_address': 1,
108+
'write_registers': [20] * 8,
109109
}
110110
rq = client.readwrite_registers(**arguments, unit=UNIT)
111-
rr = client.read_input_registers(1,8, unit=UNIT)
112-
dassert(rq, lambda r: r.registers == [20]*8) # test the expected value
111+
rr = client.read_input_registers(1, 8, unit=UNIT)
112+
dassert(rq, lambda r: r.registers == [20] * 8) # test the expected value
113113
dassert(rr, _print) # test the expected value
114114

115115
# -----------------------------------------------------------------------#
@@ -144,6 +144,3 @@ def callback(protocol, future):
144144
if __name__ == "__main__":
145145
protocol, future = ModbusClient(schedulers.IO_LOOP, port=5020)
146146
future.add_done_callback(functools.partial(callback, protocol))
147-
148-
149-

examples/common/async_tornado_client_serial.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def dassert(future, callback):
4040

4141
def _assertor(value):
4242
# by pass assertion, an error here stops the write callbacks
43-
assert value
43+
assert value
4444

4545
def on_done(f):
4646
exc = f.exception()
@@ -85,12 +85,12 @@ def beginAsynchronousTest(client, protocol):
8585
dassert(rq, lambda r: r.function_code < 0x80) # test for no error
8686
dassert(rr, _print) # test the expected value
8787

88-
rq = client.write_coils(1, [False]*8, unit=UNIT)
88+
rq = client.write_coils(1, [False] * 8, unit=UNIT)
8989
rr = client.read_coils(1, 8, unit=UNIT)
9090
dassert(rq, lambda r: r.function_code < 0x80) # test for no error
9191
dassert(rr, _print) # test the expected value
9292

93-
rq = client.write_coils(1, [False]*8, unit=UNIT)
93+
rq = client.write_coils(1, [False] * 8, unit=UNIT)
9494
rr = client.read_discrete_inputs(1, 8, unit=UNIT)
9595
dassert(rq, lambda r: r.function_code < 0x80) # test for no error
9696
dassert(rr, _print) # test the expected value
@@ -100,20 +100,20 @@ def beginAsynchronousTest(client, protocol):
100100
dassert(rq, lambda r: r.function_code < 0x80) # test for no error
101101
dassert(rr, _print) # test the expected value
102102

103-
rq = client.write_registers(1, [10]*8, unit=UNIT)
103+
rq = client.write_registers(1, [10] * 8, unit=UNIT)
104104
rr = client.read_input_registers(1, 8, unit=UNIT)
105105
dassert(rq, lambda r: r.function_code < 0x80) # test for no error
106106
dassert(rr, _print) # test the expected value
107107

108108
arguments = {
109-
'read_address': 1,
110-
'read_count': 8,
111-
'write_address': 1,
112-
'write_registers': [20]*8,
109+
'read_address': 1,
110+
'read_count': 8,
111+
'write_address': 1,
112+
'write_registers': [20] * 8,
113113
}
114114
rq = client.readwrite_registers(**arguments, unit=UNIT)
115-
rr = client.read_input_registers(1,8, unit=UNIT)
116-
dassert(rq, lambda r: r.registers == [20]*8) # test the expected value
115+
rr = client.read_input_registers(1, 8, unit=UNIT)
116+
dassert(rq, lambda r: r.registers == [20] * 8) # test the expected value
117117
dassert(rr, _print) # test the expected value
118118

119119
# -----------------------------------------------------------------------#

examples/common/async_twisted_client.py

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
# choose the requested modbus protocol
2121
# --------------------------------------------------------------------------- #
2222

23-
from twisted.internet import reactor, protocol
23+
from twisted.internet import protocol
2424

2525
# --------------------------------------------------------------------------- #
2626
# configure the client logging
@@ -98,38 +98,38 @@ def beginAsynchronousTest(client):
9898
rq = client.write_coil(1, True, unit=UNIT)
9999
rr = client.read_coils(1, 1, unit=UNIT)
100100
dassert(rq, lambda r: not r.isError()) # test for no error
101-
dassert(rr, lambda r: r.bits[0] == True) # test the expected value
101+
dassert(rr, lambda r: r.bits[0]) # test the expected value
102102

103-
rq = client.write_coils(1, [True]*8, unit=UNIT)
103+
rq = client.write_coils(1, [True] * 8, unit=UNIT)
104104
rr = client.read_coils(1, 8, unit=UNIT)
105105
dassert(rq, lambda r: not r.isError()) # test for no error
106-
dassert(rr, lambda r: r.bits == [True]*8) # test the expected value
106+
dassert(rr, lambda r: r.bits == [True] * 8) # test the expected value
107107

108-
rq = client.write_coils(1, [False]*8, unit=UNIT)
108+
rq = client.write_coils(1, [False] * 8, unit=UNIT)
109109
rr = client.read_discrete_inputs(1, 8, unit=UNIT)
110110
dassert(rq, lambda r: not r.isError()) # test for no error
111-
dassert(rr, lambda r: r.bits == [True]*8) # test the expected value
111+
dassert(rr, lambda r: r.bits == [True] * 8) # test the expected value
112112

113113
rq = client.write_register(1, 10, unit=UNIT)
114114
rr = client.read_holding_registers(1, 1, unit=UNIT)
115115
dassert(rq, lambda r: not r.isError()) # test for no error
116116
dassert(rr, lambda r: r.registers[0] == 10) # test the expected value
117117

118-
rq = client.write_registers(1, [10]*8, unit=UNIT)
118+
rq = client.write_registers(1, [10] * 8, unit=UNIT)
119119
rr = client.read_input_registers(1, 8, unit=UNIT)
120120
dassert(rq, lambda r: not r.isError()) # test for no error
121-
dassert(rr, lambda r: r.registers == [17]*8) # test the expected value
121+
dassert(rr, lambda r: r.registers == [17] * 8) # test the expected value
122122

123123
arguments = {
124-
'read_address': 1,
125-
'read_count': 8,
126-
'write_address': 1,
127-
'write_registers': [20]*8,
124+
'read_address': 1,
125+
'read_count': 8,
126+
'write_address': 1,
127+
'write_registers': [20] * 8,
128128
}
129129
rq = client.readwrite_registers(arguments, unit=UNIT)
130130
rr = client.read_input_registers(1, 8, unit=UNIT)
131-
dassert(rq, lambda r: r.registers == [20]*8) # test the expected value
132-
dassert(rr, lambda r: r.registers == [17]*8) # test the expected value
131+
dassert(rq, lambda r: r.registers == [20] * 8) # test the expected value
132+
dassert(rr, lambda r: r.registers == [17] * 8) # test the expected value
133133
stopAsynchronousTest(client)
134134

135135
# ----------------------------------------------------------------------- #
@@ -165,8 +165,5 @@ def beginAsynchronousTest(client):
165165

166166
if __name__ == "__main__":
167167
protocol, deferred = AsyncModbusTCPClient(schedulers.REACTOR, port=5020)
168-
# protocol, deferred = AsyncModbusUDPClient(schedulers.REACTOR, port=5020)
169-
# callback=beginAsynchronousTest,
170-
# errback=err)
171168
deferred.addCallback(beginAsynchronousTest)
172169
deferred.addErrback(err)

examples/common/async_twisted_client_serial.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
CLIENT_DELAY = 1
2828
UNIT = 0x01
2929

30+
3031
class ExampleProtocol(ModbusClientProtocol):
3132

3233
def __init__(self, framer):
@@ -77,12 +78,10 @@ def error_handler(self, failure):
7778
if __name__ == "__main__":
7879
import time
7980
proto, client = AsyncModbusSerialClient(schedulers.REACTOR,
80-
method="rtu",
81-
port=SERIAL_PORT,
82-
timeout=2,
81+
method="rtu",
82+
port=SERIAL_PORT,
83+
timeout=2,
8384
proto_cls=ExampleProtocol)
8485
proto.start()
8586
time.sleep(10) # Wait for operation to complete
8687
# proto.stop()
87-
88-

examples/common/asynchronous_processor.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,4 +188,3 @@ def main():
188188

189189
if __name__ == "__main__":
190190
main()
191-

0 commit comments

Comments
 (0)