Skip to content

Commit 729c77a

Browse files
authored
Merge pull request adafruit#144 from vladak/f_strings
use f-strings where possible
2 parents 3aca8b3 + 64ddb5c commit 729c77a

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
@@ -205,8 +205,8 @@ def __init__(
205205
self.client_id = client_id
206206
else:
207207
# assign a unique client_id
208-
self.client_id = "cpy{0}{1}".format(
209-
randint(0, int(time.monotonic() * 100) % 1000), randint(0, 99)
208+
self.client_id = (
209+
f"cpy{randint(0, int(time.monotonic() * 100) % 1000)}{randint(0, 99)}"
210210
)
211211
# generated client_id's enforce spec.'s length rules
212212
if len(self.client_id.encode("utf-8")) > 23 or not self.client_id:
@@ -260,13 +260,9 @@ def _get_connect_socket(self, host, port, *, timeout=1):
260260
)
261261

262262
if self.logger is not None and port == MQTT_TLS_PORT:
263-
self.logger.info(
264-
"Establishing a SECURE SSL connection to {0}:{1}".format(host, port)
265-
)
263+
self.logger.info(f"Establishing a SECURE SSL connection to {host}:{port}")
266264
elif self.logger is not None:
267-
self.logger.info(
268-
"Establishing an INSECURE connection to {0}:{1}".format(host, port)
269-
)
265+
self.logger.info(f"Establishing an INSECURE connection to {host}:{port}")
270266

271267
addr_info = self._socket_pool.getaddrinfo(
272268
host, port, 0, self._socket_pool.SOCK_STREAM
@@ -542,7 +538,7 @@ def disconnect(self):
542538
self._sock.send(MQTT_DISCONNECT)
543539
except RuntimeError as e:
544540
if self.logger is not None:
545-
self.logger.warning("Unable to send DISCONNECT packet: {}".format(e))
541+
self.logger.warning(f"Unable to send DISCONNECT packet: {e}")
546542
if self.logger is not None:
547543
self.logger.debug("Closing socket")
548544
self._sock.close()
@@ -597,7 +593,7 @@ def publish(self, topic, msg, retain=False, qos=0):
597593
else:
598594
raise MMQTTException("Invalid message data type.")
599595
if len(msg) > MQTT_MSG_MAX_SZ:
600-
raise MMQTTException("Message size larger than %d bytes." % MQTT_MSG_MAX_SZ)
596+
raise MMQTTException(f"Message size larger than {MQTT_MSG_MAX_SZ} bytes.")
601597
assert (
602598
0 <= qos <= 1
603599
), "Quality of Service Level 2 is unsupported by this library."
@@ -880,9 +876,7 @@ def _wait_for_msg(self, timeout=0.1):
880876
self.logger.debug("Got PINGRESP")
881877
sz = self._sock_exact_recv(1)[0]
882878
if sz != 0x00:
883-
raise MMQTTException(
884-
"Unexpected PINGRESP returned from broker: {}.".format(sz)
885-
)
879+
raise MMQTTException(f"Unexpected PINGRESP returned from broker: {sz}.")
886880
return MQTT_PINGRESP
887881

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

0 commit comments

Comments
 (0)