Skip to content

Commit 64ddb5c

Browse files
committed
use f-strings where possible
1 parent 645ae31 commit 64ddb5c

File tree

1 file changed

+7
-13
lines changed

1 file changed

+7
-13
lines changed

adafruit_minimqtt/adafruit_minimqtt.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,8 @@ def __init__(
206206
self.client_id = client_id
207207
else:
208208
# assign a unique client_id
209-
self.client_id = "cpy{0}{1}".format(
210-
randint(0, int(time.monotonic() * 100) % 1000), randint(0, 99)
209+
self.client_id = (
210+
f"cpy{randint(0, int(time.monotonic() * 100) % 1000)}{randint(0, 99)}"
211211
)
212212
# generated client_id's enforce spec.'s length rules
213213
if len(self.client_id.encode("utf-8")) > 23 or not self.client_id:
@@ -261,13 +261,9 @@ def _get_connect_socket(self, host, port, *, timeout=1):
261261
)
262262

263263
if self.logger is not None and port == MQTT_TLS_PORT:
264-
self.logger.info(
265-
"Establishing a SECURE SSL connection to {0}:{1}".format(host, port)
266-
)
264+
self.logger.info(f"Establishing a SECURE SSL connection to {host}:{port}")
267265
elif self.logger is not None:
268-
self.logger.info(
269-
"Establishing an INSECURE connection to {0}:{1}".format(host, port)
270-
)
266+
self.logger.info(f"Establishing an INSECURE connection to {host}:{port}")
271267

272268
addr_info = self._socket_pool.getaddrinfo(
273269
host, port, 0, self._socket_pool.SOCK_STREAM
@@ -543,7 +539,7 @@ def disconnect(self):
543539
self._sock.send(MQTT_DISCONNECT)
544540
except RuntimeError as e:
545541
if self.logger is not None:
546-
self.logger.warning("Unable to send DISCONNECT packet: {}".format(e))
542+
self.logger.warning(f"Unable to send DISCONNECT packet: {e}")
547543
if self.logger is not None:
548544
self.logger.debug("Closing socket")
549545
self._sock.close()
@@ -598,7 +594,7 @@ def publish(self, topic, msg, retain=False, qos=0):
598594
else:
599595
raise MMQTTException("Invalid message data type.")
600596
if len(msg) > MQTT_MSG_MAX_SZ:
601-
raise MMQTTException("Message size larger than %d bytes." % MQTT_MSG_MAX_SZ)
597+
raise MMQTTException(f"Message size larger than {MQTT_MSG_MAX_SZ} bytes.")
602598
assert (
603599
0 <= qos <= 1
604600
), "Quality of Service Level 2 is unsupported by this library."
@@ -881,9 +877,7 @@ def _wait_for_msg(self, timeout=0.1):
881877
self.logger.debug("Got PINGRESP")
882878
sz = self._sock_exact_recv(1)[0]
883879
if sz != 0x00:
884-
raise MMQTTException(
885-
"Unexpected PINGRESP returned from broker: {}.".format(sz)
886-
)
880+
raise MMQTTException(f"Unexpected PINGRESP returned from broker: {sz}.")
887881
return MQTT_PINGRESP
888882

889883
if res[0] & MQTT_PKT_TYPE_MASK != MQTT_PUBLISH:

0 commit comments

Comments
 (0)