Skip to content

use f-strings where possible #144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 7 additions & 13 deletions adafruit_minimqtt/adafruit_minimqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,8 @@ def __init__(
self.client_id = client_id
else:
# assign a unique client_id
self.client_id = "cpy{0}{1}".format(
randint(0, int(time.monotonic() * 100) % 1000), randint(0, 99)
self.client_id = (
f"cpy{randint(0, int(time.monotonic() * 100) % 1000)}{randint(0, 99)}"
)
# generated client_id's enforce spec.'s length rules
if len(self.client_id.encode("utf-8")) > 23 or not self.client_id:
Expand Down Expand Up @@ -261,13 +261,9 @@ def _get_connect_socket(self, host, port, *, timeout=1):
)

if self.logger is not None and port == MQTT_TLS_PORT:
self.logger.info(
"Establishing a SECURE SSL connection to {0}:{1}".format(host, port)
)
self.logger.info(f"Establishing a SECURE SSL connection to {host}:{port}")
elif self.logger is not None:
self.logger.info(
"Establishing an INSECURE connection to {0}:{1}".format(host, port)
)
self.logger.info(f"Establishing an INSECURE connection to {host}:{port}")

addr_info = self._socket_pool.getaddrinfo(
host, port, 0, self._socket_pool.SOCK_STREAM
Expand Down Expand Up @@ -543,7 +539,7 @@ def disconnect(self):
self._sock.send(MQTT_DISCONNECT)
except RuntimeError as e:
if self.logger is not None:
self.logger.warning("Unable to send DISCONNECT packet: {}".format(e))
self.logger.warning(f"Unable to send DISCONNECT packet: {e}")
if self.logger is not None:
self.logger.debug("Closing socket")
self._sock.close()
Expand Down Expand Up @@ -598,7 +594,7 @@ def publish(self, topic, msg, retain=False, qos=0):
else:
raise MMQTTException("Invalid message data type.")
if len(msg) > MQTT_MSG_MAX_SZ:
raise MMQTTException("Message size larger than %d bytes." % MQTT_MSG_MAX_SZ)
raise MMQTTException(f"Message size larger than {MQTT_MSG_MAX_SZ} bytes.")
assert (
0 <= qos <= 1
), "Quality of Service Level 2 is unsupported by this library."
Expand Down Expand Up @@ -881,9 +877,7 @@ def _wait_for_msg(self, timeout=0.1):
self.logger.debug("Got PINGRESP")
sz = self._sock_exact_recv(1)[0]
if sz != 0x00:
raise MMQTTException(
"Unexpected PINGRESP returned from broker: {}.".format(sz)
)
raise MMQTTException(f"Unexpected PINGRESP returned from broker: {sz}.")
return MQTT_PINGRESP

if res[0] & MQTT_PKT_TYPE_MASK != MQTT_PUBLISH:
Expand Down