Skip to content

Commit 2c9f186

Browse files
authored
Please black (examples). (#898)
1 parent 5c6eb36 commit 2c9f186

40 files changed

+817
-505
lines changed

examples/common/async_asyncio_client.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
# Import the required asynchronous client
1616
# ----------------------------------------------------------------------- #
1717
from pymodbus.client.asynchronous.tcp import AsyncModbusTCPClient as ModbusClient
18+
1819
# from pymodbus.client.asynchronous.udp import (
1920
# AsyncModbusUDPClient as ModbusClient)
2021
from pymodbus.client.asynchronous import schedulers
@@ -132,8 +133,8 @@ def run_with_not_running_loop():
132133
assert not loop.is_running() # nosec
133134
asyncio.set_event_loop(loop)
134135
new_loop, client = ModbusClient( # NOSONAR pylint: disable=unpacking-non-sequence
135-
schedulers.ASYNC_IO,
136-
port=5020, loop=loop)
136+
schedulers.ASYNC_IO, port=5020, loop=loop
137+
)
137138
loop.run_until_complete(start_async_test(client.protocol))
138139
loop.close()
139140
_logger.debug("--------------RUN_WITH_NOT_RUNNING_LOOP---------------")
@@ -162,10 +163,14 @@ def start_loop(loop):
162163
asyncio.sleep(1)
163164
assert loop.is_running() # nosec
164165
asyncio.set_event_loop(loop)
165-
loop, client = ModbusClient(schedulers.ASYNC_IO, # NOSONAR pylint: disable=unpacking-non-sequence
166-
port=5020, loop=loop)
166+
loop, client = ModbusClient( # NOSONAR # pylint: disable=unpacking-non-sequence
167+
schedulers.ASYNC_IO, # NOSONAR
168+
port=5020,
169+
loop=loop,
170+
) # NOSONAR
167171
future = asyncio.run_coroutine_threadsafe(
168-
start_async_test(client.protocol), loop=loop)
172+
start_async_test(client.protocol), loop=loop
173+
)
169174
future.add_done_callback(done)
170175
while not future.done():
171176
time.sleep(0.1)
@@ -177,7 +182,9 @@ def start_loop(loop):
177182
def run_with_no_loop():
178183
"""Create a loop."""
179184
_logger.debug("---------------------RUN_WITH_NO_LOOP-----------------")
180-
loop, client = ModbusClient(schedulers.ASYNC_IO, port=5020) # NOSONAR pylint: disable=unpacking-non-sequence
185+
loop, client = ModbusClient( # NOSONAR # pylint: disable=unpacking-non-sequence
186+
schedulers.ASYNC_IO, port=5020
187+
) # NOSONAR
181188
loop.run_until_complete(start_async_test(client.protocol))
182189
loop.close()
183190
_logger.debug("--------DONE RUN_WITH_NO_LOOP-------------")

examples/common/async_asyncio_serial_client.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,11 @@ async def start_async_test(client): # pylint: disable=redefined-outer-name
127127
# ----------------------------------------------------------------------- #
128128
# socat -d -d PTY,link=/tmp/ptyp0,raw,echo=0,ispeed=9600 PTY,
129129
# link=/tmp/ttyp0,raw,echo=0,ospeed=9600
130-
loop, client = ModbusClient(schedulers.ASYNC_IO, port="/tmp/ttyp0", # NOSONAR # nosec pylint: disable=unpacking-non-sequence
131-
baudrate=9600, method="rtu")
130+
loop, client = ModbusClient( # NOSONAR # pylint: disable=unpacking-non-sequence
131+
schedulers.ASYNC_IO,
132+
port="/tmp/ttyp0", # NOSONAR # nosec
133+
baudrate=9600,
134+
method="rtu",
135+
)
132136
loop.run_until_complete(start_async_test(client.protocol))
133137
loop.close()

examples/common/async_tornado_client.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def _assertor(value):
3636
assert value # nosec
3737

3838
def on_done(f_trans):
39-
if (exc := f_trans.exception()):
39+
if exc := f_trans.exception():
4040
_logger.debug(exc)
4141
return _assertor(False)
4242

@@ -77,28 +77,28 @@ def begin_asynchronous_test(client, protocol): # pylint: disable=redefined-oute
7777
"""Begin async test."""
7878
rq = client.write_coil(1, True, unit=UNIT)
7979
rr = client.read_coils(1, 1, unit=UNIT)
80-
dassert(rq, lambda r: r.function_code < 0x80) # test for no error
81-
dassert(rr, _print) # test the expected value
80+
dassert(rq, lambda r: r.function_code < 0x80) # test for no error
81+
dassert(rr, _print) # test the expected value
8282

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

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

9393
rq = client.write_register(1, 10, unit=UNIT)
9494
rr = client.read_holding_registers(1, 1, unit=UNIT)
95-
dassert(rq, lambda r: r.function_code < 0x80) # test for no error
96-
dassert(rr, _print) # test the expected value
95+
dassert(rq, lambda r: r.function_code < 0x80) # test for no error
96+
dassert(rr, _print) # test the expected value
9797

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

103103
arguments = {
104104
"read_address": 1,
@@ -108,15 +108,16 @@ def begin_asynchronous_test(client, protocol): # pylint: disable=redefined-oute
108108
}
109109
rq = client.readwrite_registers(**arguments, unit=UNIT)
110110
rr = client.read_input_registers(1, 8, unit=UNIT)
111-
dassert(rq, lambda r: r.registers == [20] * 8) # test the expected value
112-
dassert(rr, _print) # test the expected value
111+
dassert(rq, lambda r: r.registers == [20] * 8) # test the expected value
112+
dassert(rr, _print) # test the expected value
113113

114114
# -----------------------------------------------------------------------#
115115
# close the client at some time later
116116
# -----------------------------------------------------------------------#
117117
IOLoop.current().add_timeout(IOLoop.current().time() + 1, client.close)
118118
IOLoop.current().add_timeout(IOLoop.current().time() + 2, protocol.stop)
119119

120+
120121
# ---------------------------------------------------------------------------#
121122
# choose the client you want
122123
# ---------------------------------------------------------------------------#
@@ -135,13 +136,15 @@ def err(*args, **kwargs):
135136
def callback(protocol, future): # pylint: disable=redefined-outer-name
136137
"""Call as callback."""
137138
_logger.debug("Client connected")
138-
if (exp := future.exception()):
139+
if exp := future.exception():
139140
return err(exp)
140141

141142
client = future.result()
142143
return begin_asynchronous_test(client, protocol)
143144

144145

145146
if __name__ == "__main__":
146-
protocol, future = ModbusClient(schedulers.IO_LOOP, port=5020) # NOSONAR pylint: disable=unpacking-non-sequence
147+
protocol, future = ModbusClient( # NOSONAR # pylint: disable=unpacking-non-sequence
148+
schedulers.IO_LOOP, port=5020
149+
) # NOSONAR
147150
future.add_done_callback(functools.partial(callback, protocol))

examples/common/async_tornado_client_serial.py

Lines changed: 29 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,10 @@
2323
# configure the client logging
2424
# ---------------------------------------------------------------------------#
2525

26-
FORMAT = ("%(asctime)-15s %(threadName)-15s"
27-
" %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s")
26+
FORMAT = (
27+
"%(asctime)-15s %(threadName)-15s"
28+
" %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s"
29+
)
2830
log = logging.getLogger()
2931
log.setLevel(logging.DEBUG)
3032

@@ -43,7 +45,7 @@ def _assertor(value):
4345
assert value # nosec
4446

4547
def on_done(f_trans):
46-
if (exc := f_trans.exception()):
48+
if exc := f_trans.exception():
4749
log.debug(exc)
4850
return _assertor(False)
4951

@@ -78,32 +80,34 @@ def _print(value):
7880
# ---------------------------------------------------------------------------#
7981

8082

81-
def begin_asynchronous_test(client, protocol): # NOSONAR pylint: disable=redefined-outer-name
83+
def begin_asynchronous_test(
84+
client, protocol
85+
): # NOSONAR pylint: disable=redefined-outer-name
8286
"""Begin async test."""
8387
rq = client.write_coil(1, True, unit=UNIT)
8488
rr = client.read_coils(1, 1, unit=UNIT)
85-
dassert(rq, lambda r: r.function_code < 0x80) # test for no error
86-
dassert(rr, _print) # test the expected value
89+
dassert(rq, lambda r: r.function_code < 0x80) # test for no error
90+
dassert(rr, _print) # test the expected value
8791

8892
rq = client.write_coils(1, [False] * 8, unit=UNIT)
8993
rr = client.read_coils(1, 8, unit=UNIT)
90-
dassert(rq, lambda r: r.function_code < 0x80) # test for no error
91-
dassert(rr, _print) # test the expected value
94+
dassert(rq, lambda r: r.function_code < 0x80) # test for no error
95+
dassert(rr, _print) # test the expected value
9296

9397
rq = client.write_coils(1, [False] * 8, unit=UNIT)
9498
rr = client.read_discrete_inputs(1, 8, unit=UNIT)
95-
dassert(rq, lambda r: r.function_code < 0x80) # test for no error
96-
dassert(rr, _print) # test the expected value
99+
dassert(rq, lambda r: r.function_code < 0x80) # test for no error
100+
dassert(rr, _print) # test the expected value
97101

98102
rq = client.write_register(1, 10, unit=UNIT)
99103
rr = client.read_holding_registers(1, 1, unit=UNIT)
100-
dassert(rq, lambda r: r.function_code < 0x80) # test for no error
101-
dassert(rr, _print) # test the expected value
104+
dassert(rq, lambda r: r.function_code < 0x80) # test for no error
105+
dassert(rr, _print) # test the expected value
102106

103107
rq = client.write_registers(1, [10] * 8, unit=UNIT)
104108
rr = client.read_input_registers(1, 8, unit=UNIT)
105-
dassert(rq, lambda r: r.function_code < 0x80) # test for no error
106-
dassert(rr, _print) # test the expected value
109+
dassert(rq, lambda r: r.function_code < 0x80) # test for no error
110+
dassert(rr, _print) # test the expected value
107111

108112
arguments = {
109113
"read_address": 1,
@@ -113,8 +117,8 @@ def begin_asynchronous_test(client, protocol): # NOSONAR pylint: disable=redefi
113117
}
114118
rq = client.readwrite_registers(**arguments, unit=UNIT)
115119
rr = client.read_input_registers(1, 8, unit=UNIT)
116-
dassert(rq, lambda r: r.registers == [20] * 8) # test the expected value
117-
dassert(rr, _print) # test the expected value
120+
dassert(rq, lambda r: r.registers == [20] * 8) # test the expected value
121+
dassert(rr, _print) # test the expected value
118122

119123
# -----------------------------------------------------------------------#
120124
# close the client at some time later
@@ -131,6 +135,7 @@ def begin_asynchronous_test(client, protocol): # NOSONAR pylint: disable=redefi
131135
# directory, or start a pymodbus server.
132136
# ---------------------------------------------------------------------------#
133137

138+
134139
def err(*args, **kwargs):
135140
"""Handle error."""
136141
txt = f"Err {args} {kwargs}"
@@ -140,7 +145,7 @@ def err(*args, **kwargs):
140145
def callback(protocol, future): # pylint: disable=redefined-outer-name
141146
"""Call Callback."""
142147
log.debug("Client connected")
143-
if (exp := future.exception()):
148+
if exp := future.exception():
144149
return err(exp)
145150

146151
client = future.result()
@@ -158,11 +163,13 @@ def callback(protocol, future): # pylint: disable=redefined-outer-name
158163
# ----------------------------------------------------------------------- #
159164

160165
# Rtu
161-
protocol, future = AsyncModbusSerialClient(schedulers.IO_LOOP, # NOSONAR pylint: disable=unpacking-non-sequence
162-
method="rtu",
163-
port="/tmp/ptyp0", # nosec NOSONAR
164-
baudrate=9600,
165-
timeout=2)
166+
protocol, future = AsyncModbusSerialClient( # NOSONAR # pylint: disable=unpacking-non-sequence
167+
schedulers.IO_LOOP, # NOSONAR
168+
method="rtu",
169+
port="/tmp/ptyp0", # nosec NOSONAR
170+
baudrate=9600,
171+
timeout=2,
172+
) # NOSONAR
166173

167174
# Asci
168175
# from pymodbus.transaction import ModbusAsciiFramer

examples/common/async_twisted_client.py

Lines changed: 25 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,17 @@
1212
from twisted.internet import reactor
1313

1414
from pymodbus.client.asynchronous.tcp import AsyncModbusTCPClient
15+
1516
# from pymodbus.client.asynchronous.udp import AsyncModbusUDPClient
1617
from pymodbus.client.asynchronous import schedulers
1718

1819
# --------------------------------------------------------------------------- #
1920
# configure the client logging
2021
# --------------------------------------------------------------------------- #
21-
FORMAT = ("%(asctime)-15s %(threadName)-15s"
22-
" %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s")
22+
FORMAT = (
23+
"%(asctime)-15s %(threadName)-15s"
24+
" %(levelname)-8s %(module)-15s:%(lineno)-8s %(message)s"
25+
)
2326
logging.basicConfig(format=FORMAT) # NOSONAR
2427
log = logging.getLogger()
2528
log.setLevel(logging.DEBUG)
@@ -37,11 +40,14 @@ def err(*args, **kwargs):
3740

3841
def dassert(deferred, callback): # pylint: disable=redefined-outer-name
3942
"""Dassert."""
43+
4044
def _assertor(value):
4145
assert value # nosec
46+
4247
deferred.addCallback(lambda r: _assertor(callback(r)))
4348
deferred.addErrback(err)
4449

50+
4551
# --------------------------------------------------------------------------- #
4652
# specify slave to query
4753
# --------------------------------------------------------------------------- #
@@ -71,6 +77,7 @@ def example_requests(client):
7177
rr.addCallback(process_response)
7278
stop_asynchronous_test(client)
7379

80+
7481
# --------------------------------------------------------------------------- #
7582
# example requests
7683
# --------------------------------------------------------------------------- #
@@ -96,28 +103,28 @@ def begin_asynchronous_test(client):
96103
"""Begin async test."""
97104
rq = client.write_coil(1, True, unit=UNIT)
98105
rr = client.read_coils(1, 1, unit=UNIT)
99-
dassert(rq, lambda r: not r.isError()) # test for no error
100-
dassert(rr, lambda r: r.bits[0]) # test the expected value
106+
dassert(rq, lambda r: not r.isError()) # test for no error
107+
dassert(rr, lambda r: r.bits[0]) # test the expected value
101108

102109
rq = client.write_coils(1, [True] * 8, unit=UNIT)
103110
rr = client.read_coils(1, 8, unit=UNIT)
104-
dassert(rq, lambda r: not r.isError()) # test for no error
105-
dassert(rr, lambda r: r.bits == [True] * 8) # test the expected value
111+
dassert(rq, lambda r: not r.isError()) # test for no error
112+
dassert(rr, lambda r: r.bits == [True] * 8) # test the expected value
106113

107114
rq = client.write_coils(1, [False] * 8, unit=UNIT)
108115
rr = client.read_discrete_inputs(1, 8, unit=UNIT)
109-
dassert(rq, lambda r: not r.isError()) # test for no error
110-
dassert(rr, lambda r: r.bits == [True] * 8) # test the expected value
116+
dassert(rq, lambda r: not r.isError()) # test for no error
117+
dassert(rr, lambda r: r.bits == [True] * 8) # test the expected value
111118

112119
rq = client.write_register(1, 10, unit=UNIT)
113120
rr = client.read_holding_registers(1, 1, unit=UNIT)
114-
dassert(rq, lambda r: not r.isError()) # test for no error
115-
dassert(rr, lambda r: r.registers[0] == 10) # test the expected value
121+
dassert(rq, lambda r: not r.isError()) # test for no error
122+
dassert(rr, lambda r: r.registers[0] == 10) # test the expected value
116123

117124
rq = client.write_registers(1, [10] * 8, unit=UNIT)
118125
rr = client.read_input_registers(1, 8, unit=UNIT)
119-
dassert(rq, lambda r: not r.isError()) # test for no error
120-
dassert(rr, lambda r: r.registers == [17] * 8) # test the expected value
126+
dassert(rq, lambda r: not r.isError()) # test for no error
127+
dassert(rr, lambda r: r.registers == [17] * 8) # test the expected value
121128

122129
arguments = {
123130
"read_address": 1,
@@ -127,8 +134,8 @@ def begin_asynchronous_test(client):
127134
}
128135
rq = client.readwrite_registers(arguments, unit=UNIT)
129136
rr = client.read_input_registers(1, 8, unit=UNIT)
130-
dassert(rq, lambda r: r.registers == [20] * 8) # test the expected value
131-
dassert(rr, lambda r: r.registers == [17] * 8) # test the expected value
137+
dassert(rq, lambda r: r.registers == [20] * 8) # test the expected value
138+
dassert(rr, lambda r: r.registers == [17] * 8) # test the expected value
132139
stop_asynchronous_test(client)
133140

134141
# ----------------------------------------------------------------------- #
@@ -137,6 +144,7 @@ def begin_asynchronous_test(client):
137144
# reactor.callLater(1, client.transport.loseConnection)
138145
reactor.callLater(2, reactor.stop) # pylint: disable=no-member
139146

147+
140148
# --------------------------------------------------------------------------- #
141149
# extra requests
142150
# --------------------------------------------------------------------------- #
@@ -163,7 +171,8 @@ def begin_asynchronous_test(client):
163171

164172

165173
if __name__ == "__main__":
166-
protocol, deferred = AsyncModbusTCPClient(schedulers.REACTOR, # NOSONAR pylint: disable=unpacking-non-sequence
167-
port=5020)
174+
protocol, deferred = AsyncModbusTCPClient( # NOSONAR # pylint: disable=unpacking-non-sequence
175+
schedulers.REACTOR, port=5020 # NOSONAR
176+
) # NOSONAR
168177
deferred.addCallback(begin_asynchronous_test)
169178
deferred.addErrback(err)

0 commit comments

Comments
 (0)