Skip to content

Commit

Permalink
address upcoming UP031 lint
Browse files Browse the repository at this point in the history
  • Loading branch information
minrk committed Aug 20, 2024
1 parent d8cc94c commit 0175db5
Show file tree
Hide file tree
Showing 27 changed files with 72 additions and 90 deletions.
2 changes: 1 addition & 1 deletion examples/device/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def produce(url, ident):
s.connect(url)
print(f"Producing {ident}")
for i in range(MSGS):
s.send(('%s: %i' % (ident, time.time())).encode('utf8'))
s.send((f'{ident}: {time.time():.0f}').encode())
time.sleep(1)
print(f"Producer {ident} done")
s.close()
Expand Down
4 changes: 2 additions & 2 deletions examples/draft/client-server.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
for i in range(10):
client = ctx.socket(zmq.CLIENT)
client.connect(url)
client.send(b'request %i' % i)
client.send(f'request {i}'.encode("ascii"))
msg = server.recv(copy=False)
print(f'server recvd {msg.bytes!r} from {msg.routing_id!r}')
server.send_string('reply %i' % i, routing_id=msg.routing_id)
server.send_string(f'reply {i}', routing_id=msg.routing_id)
reply = client.recv_string()
print(f'client recvd {reply!r}')
time.sleep(0.1)
Expand Down
2 changes: 1 addition & 1 deletion examples/eventloop/asyncweb.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def slow_responder() -> None:
frame, msg = socket.recv_multipart()
print(f"\nworker received {msg!r}\n", end='')
time.sleep(random.randint(1, 5))
socket.send_multipart([frame, msg + b" to you too, #%i" % i])
socket.send_multipart([frame, msg + f" to you too, #{i}".encode()])
i += 1


Expand Down
2 changes: 1 addition & 1 deletion examples/gevent/poll.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def sender():
sender.connect('inproc://polltest2')

for i in range(10):
sender.send('test %d' % i)
sender.send(f'test {i}')
gevent.sleep(1)


Expand Down
2 changes: 1 addition & 1 deletion examples/heartbeat/ping.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
time.sleep(t)
for i in range(4):
n += 1
msg = 'ping %i' % n
msg = f'ping {n}'
tic = time.time()
req.send_string(msg)
resp = req.recv_string()
Expand Down
8 changes: 4 additions & 4 deletions examples/logger/zmqlogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
def sub_logger(port: int, level: int = logging.DEBUG) -> None:
ctx = zmq.Context()
sub = ctx.socket(zmq.SUB)
sub.bind('tcp://127.0.0.1:%i' % port)
sub.bind(f'tcp://127.0.0.1:{port}')
sub.setsockopt(zmq.SUBSCRIBE, b"")
logging.basicConfig(level=level)

Expand All @@ -46,17 +46,17 @@ def sub_logger(port: int, level: int = logging.DEBUG) -> None:
def log_worker(port: int, interval: float = 1, level: int = logging.DEBUG) -> None:
ctx = zmq.Context()
pub = ctx.socket(zmq.PUB)
pub.connect('tcp://127.0.0.1:%i' % port)
pub.connect(f'tcp://127.0.0.1:{port}')

logger = logging.getLogger(str(os.getpid()))
logger.setLevel(level)
handler: PUBHandler = PUBHandler(pub)
logger.addHandler(handler)
print("starting logger at %i with level=%s" % (os.getpid(), level))
print(f"starting logger at {os.getpid()} with level={level}")

while True:
level = random.choice(LOG_LEVELS)
logger.log(level, "Hello from %i!" % os.getpid())
logger.log(level, f"Hello from {os.getpid()}!")
time.sleep(interval)


Expand Down
2 changes: 1 addition & 1 deletion examples/monitoring/simple_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def line() -> None:
for name in dir(zmq):
if name.startswith('EVENT_'):
value = getattr(zmq, name)
print("%21s : %4i" % (name, value))
print(f"{name:21} : {value:4}")
EVENT_MAP[value] = name


Expand Down
4 changes: 2 additions & 2 deletions examples/monitoring/zmq_monitor_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ def event_monitor_thread_async(
for name in dir(zmq):
if name.startswith('EVENT_'):
value = getattr(zmq, name)
print("%21s : %4i" % (name, value))
print(f"{name:21} : {value:4}")
EVENT_MAP[value] = name

print("\n")
Expand Down Expand Up @@ -108,7 +108,7 @@ def event_monitor_thread(monitor: zmq.Socket) -> None:
for name in dir(zmq):
if name.startswith('EVENT_'):
value = getattr(zmq, name)
print("%21s : %4i" % (name, value))
print(f"{name:21} : {value:4}")
EVENT_MAP[value] = name

print()
Expand Down
4 changes: 2 additions & 2 deletions examples/serialization/serialsocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def send_zipped_pickle(
"""pack and compress an object with pickle and zlib."""
pobj = pickle.dumps(obj, protocol)
zobj = zlib.compress(pobj)
print('zipped pickle is %i bytes' % len(zobj))
print(f'zipped pickle is {len(zobj)} bytes')
return self.send(zobj, flags=flags)

def recv_zipped_pickle(self, flags: int = 0) -> Any:
Expand Down Expand Up @@ -67,7 +67,7 @@ def main() -> None:
rep.bind('inproc://a')
req.connect('inproc://a')
A = numpy.ones((1024, 1024))
print("Array is %i bytes" % (A.nbytes))
print(f"Array is {A.nbytes} bytes")

# send/recv with pickle+zip
req.send_zipped_pickle(A)
Expand Down
2 changes: 1 addition & 1 deletion perf/collect.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def compute_data_point(
count = 2
results = []
print(f'copy={copy}, size={size}')
print('%8s %5s %7s' % ('count', 'dt', 'result'))
print(f"{'count':8} {'dt':5} {'result':7}")
while duration < t_max:
with timer() as get_duration:
result = do_run(
Expand Down
8 changes: 4 additions & 4 deletions perf/perf.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,8 +133,8 @@ def latency(url, count, size, poll=False, copy=True, quiet=False):
latency = 1e6 * elapsed / (count * 2.0)

if not quiet:
print("message size : %8i [B]" % (size,))
print("roundtrip count: %8i [msgs]" % (count,))
print(f"message size : {size:8d} [B]")
print(f"roundtrip count: {count:8d} [msgs]")
print(f"mean latency : {latency:12.3f} [µs]")
print(f"test time : {elapsed:12.3f} [s]")
ctx.destroy()
Expand Down Expand Up @@ -216,8 +216,8 @@ def throughput(url, count, size, poll=False, copy=True, quiet=False):
megabits = throughput * size * 8 / 1e6

if not quiet:
print("message size : %8i [B]" % size)
print("message count : %8i [msgs]" % count)
print(f"message size : {size:8d} [B]")
print(f"message count : {count:8d} [msgs]")
print(f"send only : {send_throughput:8.0f} [msg/s]")
print(f"mean throughput: {throughput:8.0f} [msg/s]")
print(f"mean throughput: {megabits:12.3f} [Mb/s]")
Expand Down
2 changes: 1 addition & 1 deletion tests/test_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ async def can_connect(self, server, client, timeout=1000):
result = False
iface = 'tcp://127.0.0.1'
port = server.bind_to_random_port(iface)
client.connect("%s:%i" % (iface, port))
client.connect(f"{iface}:{port}")
msg = [b"Hello World"]
# run poll on server twice
# to flush spurious events
Expand Down
20 changes: 10 additions & 10 deletions tests/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def test_single_socket_forwarder_connect(self):
dev = devices.ThreadDevice(zmq.QUEUE, zmq.REP, -1)
req = self.context.socket(zmq.REQ)
port = req.bind_to_random_port('tcp://127.0.0.1')
dev.connect_in('tcp://127.0.0.1:%i' % port)
dev.connect_in(f'tcp://127.0.0.1:{port}')
dev.start()
time.sleep(0.25)
msg = b'hello'
Expand All @@ -44,7 +44,7 @@ def test_single_socket_forwarder_connect(self):
dev = devices.ThreadDevice(zmq.QUEUE, zmq.REP, -1)
req = self.context.socket(zmq.REQ)
port = req.bind_to_random_port('tcp://127.0.0.1')
dev.connect_out('tcp://127.0.0.1:%i' % port)
dev.connect_out(f'tcp://127.0.0.1:{port}')
dev.start()
time.sleep(0.25)
msg = b'hello again'
Expand All @@ -59,7 +59,7 @@ def test_single_socket_forwarder_bind(self):
dev = devices.ThreadDevice(zmq.QUEUE, zmq.REP, -1)
port = dev.bind_in_to_random_port('tcp://127.0.0.1')
req = self.context.socket(zmq.REQ)
req.connect('tcp://127.0.0.1:%i' % port)
req.connect(f'tcp://127.0.0.1:{port}')
dev.start()
time.sleep(0.25)
msg = b'hello'
Expand All @@ -70,7 +70,7 @@ def test_single_socket_forwarder_bind(self):
dev = devices.ThreadDevice(zmq.QUEUE, zmq.REP, -1)
port = dev.bind_in_to_random_port('tcp://127.0.0.1')
req = self.context.socket(zmq.REQ)
req.connect('tcp://127.0.0.1:%i' % port)
req.connect(f'tcp://127.0.0.1:{port}')
dev.start()
time.sleep(0.25)
msg = b'hello again'
Expand All @@ -92,7 +92,7 @@ def test_device_bind_to_random_with_args(self):
)
for port in ports:
if port < min or port > max:
self.fail('Unexpected port number: %i' % port)
self.fail(f'Unexpected port number: {port}')

def test_device_bind_to_random_binderror(self):
dev = devices.ThreadDevice(zmq.PULL, zmq.PUSH, -1)
Expand All @@ -117,11 +117,11 @@ def test_proxy(self):
time.sleep(0.25)
msg = b'hello'
push = self.context.socket(zmq.PUSH)
push.connect("%s:%i" % (iface, port))
push.connect(f"{iface}:{port}")
pull = self.context.socket(zmq.PULL)
pull.connect("%s:%i" % (iface, port2))
pull.connect(f"{iface}:{port2}")
mon = self.context.socket(zmq.PULL)
mon.connect("%s:%i" % (iface, port3))
mon.connect(f"{iface}:{port3}")
push.send(msg)
self.sockets.extend([push, pull, mon])
assert msg == self.recv(pull)
Expand All @@ -143,7 +143,7 @@ def test_proxy_bind_to_random_with_args(self):
)
for port in ports:
if port < min or port > max:
self.fail('Unexpected port number: %i' % port)
self.fail(f'Unexpected port number: {port}')


if have_gevent:
Expand All @@ -158,7 +158,7 @@ def test_green_device(self):
self.sockets.extend([req, rep])
port = rep.bind_to_random_port('tcp://127.0.0.1')
g = gevent.spawn(zmq.green.device, zmq.QUEUE, rep, rep)
req.connect('tcp://127.0.0.1:%i' % port)
req.connect(f'tcp://127.0.0.1:{port}')
req.send(b'hi')
timeout = gevent.Timeout(3)
timeout.start()
Expand Down
10 changes: 5 additions & 5 deletions tests/test_monqueue.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ def build_device(self, mon_sub=b"", in_prefix=b'in', out_prefix=b'out'):
mport = mon.bind_to_random_port('tcp://127.0.0.1')
mon.setsockopt(zmq.SUBSCRIBE, mon_sub)

self.device.connect_in("tcp://127.0.0.1:%i" % aport)
self.device.connect_out("tcp://127.0.0.1:%i" % bport)
self.device.connect_mon("tcp://127.0.0.1:%i" % mport)
self.device.connect_in(f"tcp://127.0.0.1:{aport}")
self.device.connect_out(f"tcp://127.0.0.1:{bport}")
self.device.connect_mon(f"tcp://127.0.0.1:{mport}")
self.device.start()
time.sleep(0.2)
try:
Expand Down Expand Up @@ -191,8 +191,8 @@ def test_router_router(self):
b.identity = b'b'
self.sockets.extend([a, b])

a.connect('tcp://127.0.0.1:%i' % porta)
b.connect('tcp://127.0.0.1:%i' % portb)
a.connect(f'tcp://127.0.0.1:{porta}')
b.connect(f'tcp://127.0.0.1:{portb}')
dev.start()
time.sleep(1)
if zmq.zmq_version_info() >= (3, 1, 0):
Expand Down
18 changes: 9 additions & 9 deletions tests/test_proxy_steerable.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ def test_proxy_steerable(self):
time.sleep(0.25)
msg = b'hello'
push = self.context.socket(zmq.PUSH)
push.connect("%s:%i" % (iface, port))
push.connect(f"{iface}:{port}")
pull = self.context.socket(zmq.PULL)
pull.connect("%s:%i" % (iface, port2))
pull.connect(f"{iface}:{port2}")
mon = self.context.socket(zmq.PULL)
mon.connect("%s:%i" % (iface, port3))
mon.connect(f"{iface}:{port3}")
ctrl = self.context.socket(zmq.PAIR)
ctrl.connect("%s:%i" % (iface, port4))
ctrl.connect(f"{iface}:{port4}")
push.send(msg)
self.sockets.extend([push, pull, mon, ctrl])
assert msg == self.recv(pull)
Expand All @@ -65,7 +65,7 @@ def test_proxy_steerable_bind_to_random_with_args(self):
)
for port in ports:
if port < min or port > max:
self.fail('Unexpected port number: %i' % port)
self.fail(f'Unexpected port number: {port}')

def test_proxy_steerable_statistics(self):
if zmq.zmq_version_info() < (4, 3):
Expand All @@ -80,13 +80,13 @@ def test_proxy_steerable_statistics(self):
time.sleep(0.25)
msg = b'hello'
push = self.context.socket(zmq.PUSH)
push.connect("%s:%i" % (iface, port))
push.connect(f"{iface}:{port}")
pull = self.context.socket(zmq.PULL)
pull.connect("%s:%i" % (iface, port2))
pull.connect(f"{iface}:{port2}")
mon = self.context.socket(zmq.PULL)
mon.connect("%s:%i" % (iface, port3))
mon.connect(f"{iface}:{port3}")
ctrl = self.context.socket(zmq.PAIR)
ctrl.connect("%s:%i" % (iface, port4))
ctrl.connect(f"{iface}:{port4}")
push.send(msg)
self.sockets.extend([push, pull, mon, ctrl])
assert msg == self.recv(pull)
Expand Down
8 changes: 4 additions & 4 deletions tests/test_security.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def test_null(self):
assert server.plain_server == 0
iface = 'tcp://127.0.0.1'
port = server.bind_to_random_port(iface)
client.connect("%s:%i" % (iface, port))
client.connect(f"{iface}:{port}")
self.bounce(server, client, False)

def test_plain(self):
Expand All @@ -139,7 +139,7 @@ def test_plain(self):
with self.zap():
iface = 'tcp://127.0.0.1'
port = server.bind_to_random_port(iface)
client.connect("%s:%i" % (iface, port))
client.connect(f"{iface}:{port}")
self.bounce(server, client)

def skip_plain_inauth(self):
Expand All @@ -157,7 +157,7 @@ def skip_plain_inauth(self):
with self.zap():
iface = 'tcp://127.0.0.1'
port = server.bind_to_random_port(iface)
client.connect("%s:%i" % (iface, port))
client.connect(f"{iface}:{port}")
client.send(b'ping')
server.rcvtimeo = 250
self.assertRaisesErrno(zmq.EAGAIN, server.recv)
Expand Down Expand Up @@ -234,5 +234,5 @@ def test_curve(self):
with self.zap():
iface = 'tcp://127.0.0.1'
port = server.bind_to_random_port(iface)
client.connect("%s:%i" % (iface, port))
client.connect(f"{iface}:{port}")
self.bounce(server, client)
2 changes: 1 addition & 1 deletion tests/test_socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ def test_tracker(self):
sock = socket.socket()
sock.bind(('127.0.0.1', 0))
port = sock.getsockname()[1]
iface = "%s:%i" % (addr, port)
iface = f"{addr}:{port}"
sock.close()
time.sleep(0.1)

Expand Down
2 changes: 1 addition & 1 deletion tests/test_zmqstream.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ async def push_pull(socket):
push = zmqstream.ZMQStream(socket(zmq.PUSH))
pull = zmqstream.ZMQStream(socket(zmq.PULL))
port = push.bind_to_random_port('tcp://127.0.0.1')
pull.connect('tcp://127.0.0.1:%i' % port)
pull.connect(f'tcp://127.0.0.1:{port}')
return (push, pull)


Expand Down
4 changes: 2 additions & 2 deletions zmq/backend/cffi/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def new_int_pointer():


def new_binary_data(length):
return ffi.new('char[%d]' % length), nsp(ffi.sizeof('char') * length)
return ffi.new(f'char[{length:d}]'), nsp(ffi.sizeof('char') * length)


def value_uint64_pointer(val):
Expand All @@ -46,7 +46,7 @@ def value_int_pointer(val):


def value_binary_data(val, length):
return ffi.new('char[%d]' % (length + 1), val), ffi.sizeof('char') * length
return ffi.new(f'char[{length + 1:d}]', val), ffi.sizeof('char') * length


ZMQ_FD_64BIT = ffi.sizeof('ZMQ_FD_T') == 8
Expand Down
Loading

0 comments on commit 0175db5

Please sign in to comment.