style: remove unneeded temporaries, use format() calls
diff --git a/serial/urlhandler/protocol_socket.py b/serial/urlhandler/protocol_socket.py
index a0ac59b..e25b14c 100644
--- a/serial/urlhandler/protocol_socket.py
+++ b/serial/urlhandler/protocol_socket.py
@@ -59,7 +59,7 @@
self._socket = socket.create_connection(self.from_url(self.portstr))
except Exception as msg:
self._socket = None
- raise SerialException("Could not open port %s: %s" % (self.portstr, msg))
+ raise SerialException("Could not open port {}: {}".format(self.portstr, msg))
self._socket.settimeout(POLL_TIMEOUT) # used for write timeout support :/
@@ -103,7 +103,10 @@
"""extract host and port from an URL string"""
parts = urlparse.urlsplit(url)
if parts.scheme != "socket":
- raise SerialException('expected a string in the form "socket://<host>:<port>[?logging={debug|info|warning|error}]": not starting with socket:// (%r)' % (parts.scheme,))
+ raise SerialException(
+ 'expected a string in the form '
+ '"socket://<host>:<port>[?logging={debug|info|warning|error}]": '
+ 'not starting with socket:// ({!r})'.format(parts.scheme))
try:
# process options now, directly altering self
for option, values in urlparse.parse_qs(parts.query, True).items():
@@ -113,14 +116,15 @@
self.logger.setLevel(LOGGER_LEVELS[values[0]])
self.logger.debug('enabled logging')
else:
- raise ValueError('unknown option: %r' % (option,))
- # get host and port
- host, port = parts.hostname, parts.port
- if not 0 <= port < 65536:
+ raise ValueError('unknown option: {!r}'.format(option))
+ if not 0 <= parts.port < 65536:
raise ValueError("port not in range 0...65535")
except ValueError as e:
- raise SerialException('expected a string in the form "socket://<host>:<port>[?logging={debug|info|warning|error}]": %s' % e)
- return (host, port)
+ raise SerialException(
+ 'expected a string in the form '
+ '"socket://<host>:<port>[?logging={debug|info|warning|error}]": {}'.format(e))
+
+ return (parts.hostname, parts.port)
# - - - - - - - - - - - - - - - - - - - - - - - -
@@ -172,18 +176,18 @@
pass
except socket.error as e:
# connection fails -> terminate loop
- raise SerialException('connection failed (%s)' % e)
+ raise SerialException('connection failed ({})'.format(e))
except OSError as e:
# this is for Python 3.x where select.error is a subclass of
# OSError ignore EAGAIN errors. all other errors are shown
if e.errno != errno.EAGAIN:
- raise SerialException('read failed: %s' % (e,))
+ raise SerialException('read failed: {}'.format(e))
except select.error as e:
# this is for Python 2.x
# ignore EAGAIN errors. all other errors are shown
# see also http://www.python.org/dev/peps/pep-3151/#select
if e[0] != errno.EAGAIN:
- raise SerialException('read failed: %s' % (e,))
+ raise SerialException('read failed: {}'.format(e))
return bytes(read)
def write(self, data):
@@ -198,7 +202,7 @@
self._socket.sendall(to_bytes(data))
except socket.error as e:
# XXX what exception if socket connection fails
- raise SerialException("socket connection failed: %s" % e)
+ raise SerialException("socket connection failed: {}".format(e))
return len(data)
def reset_input_buffer(self):
@@ -226,23 +230,23 @@
if not self.is_open:
raise portNotOpenError
if self.logger:
- self.logger.info('ignored send_break(%r)' % (duration,))
+ self.logger.info('ignored send_break({!r})'.format(duration))
def _update_break_state(self):
"""Set break: Controls TXD. When active, to transmitting is
possible."""
if self.logger:
- self.logger.info('ignored _update_break_state(%r)' % (self._break_state,))
+ self.logger.info('ignored _update_break_state({!r})'.format(self._break_state))
def _update_rts_state(self):
"""Set terminal status line: Request To Send"""
if self.logger:
- self.logger.info('ignored _update_rts_state(%r)' % (self._rts_state,))
+ self.logger.info('ignored _update_rts_state({!r})'.format(self._rts_state))
def _update_dtr_state(self):
"""Set terminal status line: Data Terminal Ready"""
if self.logger:
- self.logger.info('ignored _update_dtr_state(%r)' % (self._dtr_state,))
+ self.logger.info('ignored _update_dtr_state({!r})'.format(self._dtr_state))
@property
def cts(self):
@@ -293,11 +297,11 @@
if __name__ == '__main__':
import sys
s = Serial('socket://localhost:7000')
- sys.stdout.write('%s\n' % s)
+ sys.stdout.write('{}\n'.format(s))
sys.stdout.write("write...\n")
s.write(b"hello\n")
s.flush()
- sys.stdout.write("read: %s\n" % s.read(5))
+ sys.stdout.write("read: {}\n".format(s.read(5)))
s.close()