style: use .format() in various places instead of "%" formatting
diff --git a/serial/urlhandler/protocol_alt.py b/serial/urlhandler/protocol_alt.py
index e33144e..c14a87e 100644
--- a/serial/urlhandler/protocol_alt.py
+++ b/serial/urlhandler/protocol_alt.py
@@ -30,23 +30,23 @@
if parts.scheme != 'alt':
raise serial.SerialException(
'expected a string in the form "alt://port[?option[=value][&option[=value]]]": '
- 'not starting with alt:// (%r)' % (parts.scheme,))
+ 'not starting with alt:// ({!r})'.format(parts.scheme))
class_name = 'Serial'
try:
for option, values in urlparse.parse_qs(parts.query, True).items():
if option == 'class':
class_name = values[0]
else:
- raise ValueError('unknown option: %r' % (option,))
+ raise ValueError('unknown option: {!r}'.format(option))
except ValueError as e:
raise serial.SerialException(
'expected a string in the form '
- '"alt://port[?option[=value][&option[=value]]]": %s' % e)
+ '"alt://port[?option[=value][&option[=value]]]": {!r}'.format(e))
if not hasattr(serial, class_name):
- raise ValueError('unknown class: %r' % (class_name,))
+ raise ValueError('unknown class: {!r}'.format(class_name))
cls = getattr(serial, class_name)
if not issubclass(cls, serial.Serial):
- raise ValueError('class %r is not an instance of Serial' % (class_name,))
+ raise ValueError('class {!r} is not an instance of Serial'.format(class_name))
return (''.join([parts.netloc, parts.path]), cls)
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
diff --git a/serial/urlhandler/protocol_hwgrep.py b/serial/urlhandler/protocol_hwgrep.py
index 9b3a082..49bbebe 100644
--- a/serial/urlhandler/protocol_hwgrep.py
+++ b/serial/urlhandler/protocol_hwgrep.py
@@ -59,12 +59,12 @@
# pick n'th element
n = int(value) - 1
if n < 1:
- raise ValueError('option "n" expects a positive integer larger than 1: %r' % (value,))
+ raise ValueError('option "n" expects a positive integer larger than 1: {!r}'.format(value))
elif option == 'skip_busy':
# open to test if port is available. not the nicest way..
test_open = True
else:
- raise ValueError('unknown option: %r' % (option,))
+ raise ValueError('unknown option: {!r}'.format(option))
# use a for loop to get the 1st element from the generator
for port, desc, hwid in sorted(serial.tools.list_ports.grep(regexp)):
if test_open:
@@ -80,7 +80,7 @@
continue
return port
else:
- raise serial.SerialException('no ports found matching regexp %r' % (url,))
+ raise serial.SerialException('no ports found matching regexp {!r}'.format(url))
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
if __name__ == '__main__':
diff --git a/serial/urlhandler/protocol_loop.py b/serial/urlhandler/protocol_loop.py
index daf2415..819da77 100644
--- a/serial/urlhandler/protocol_loop.py
+++ b/serial/urlhandler/protocol_loop.py
@@ -91,7 +91,7 @@
"""
# not that's it of any real use, but it helps in the unit tests
if not isinstance(self._baudrate, numbers.Integral) or not 0 < self._baudrate < 2 ** 32:
- raise ValueError("invalid baudrate: %r" % (self._baudrate))
+ raise ValueError("invalid baudrate: {!r}".format(self._baudrate))
if self.logger:
self.logger.info('_reconfigure_port()')
@@ -102,7 +102,7 @@
raise SerialException(
'expected a string in the form '
'"loop://[?logging={debug|info|warning|error}]": not starting '
- 'with loop:// (%r)' % (parts.scheme,))
+ 'with loop:// ({!r})'.format(parts.scheme))
try:
# process options now, directly altering self
for option, values in urlparse.parse_qs(parts.query, True).items():
@@ -112,11 +112,11 @@
self.logger.setLevel(LOGGER_LEVELS[values[0]])
self.logger.debug('enabled logging')
else:
- raise ValueError('unknown option: %r' % (option,))
+ raise ValueError('unknown option: {!r}'.format(option))
except ValueError as e:
raise SerialException(
'expected a string in the form '
- '"loop://[?logging={debug|info|warning|error}]": %s' % e)
+ '"loop://[?logging={debug|info|warning|error}]": {}'.format(e))
# - - - - - - - - - - - - - - - - - - - - - - - -
@@ -128,7 +128,7 @@
if self.logger:
# attention the logged value can differ from return value in
# threaded environments...
- self.logger.debug('in_waiting -> %d' % (self.queue.qsize(),))
+ self.logger.debug('in_waiting -> {:d}'.format(self.queue.qsize()))
return self.queue.qsize()
def read(self, size=1):
@@ -217,17 +217,17 @@
possible.
"""
if self.logger:
- self.logger.info('_update_break_state(%r)' % (self._break_state,))
+ self.logger.info('_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('_update_rts_state(%r) -> state of CTS' % (self._rts_state,))
+ self.logger.info('_update_rts_state({!r}) -> state of CTS'.format(self._rts_state))
def _update_dtr_state(self):
"""Set terminal status line: Data Terminal Ready"""
if self.logger:
- self.logger.info('_update_dtr_state(%r) -> state of DSR' % (self._dtr_state,))
+ self.logger.info('_update_dtr_state({!r}) -> state of DSR'.format(self._dtr_state))
@property
def cts(self):
@@ -235,14 +235,14 @@
if not self.is_open:
raise portNotOpenError
if self.logger:
- self.logger.info('CTS -> state of RTS (%r)' % (self._rts_state,))
+ self.logger.info('CTS -> state of RTS ({!r})'.format(self._rts_state))
return self._rts_state
@property
def dsr(self):
"""Read terminal status line: Data Set Ready"""
if self.logger:
- self.logger.info('DSR -> state of DTR (%r)' % (self._dtr_state,))
+ self.logger.info('DSR -> state of DTR ({!r})'.format(self._dtr_state))
return self._dtr_state
@property
@@ -271,11 +271,11 @@
if __name__ == '__main__':
import sys
s = Serial('loop://')
- sys.stdout.write('%s\n' % s)
+ sys.stdout.write('{}\n'.format(s))
sys.stdout.write("write...\n")
s.write("hello\n")
s.flush()
- sys.stdout.write("read: %s\n" % s.read(5))
+ sys.stdout.write("read: {!r}\n".format(s.read(5)))
s.close()
diff --git a/serial/urlhandler/protocol_spy.py b/serial/urlhandler/protocol_spy.py
index 939d5aa..3479010 100644
--- a/serial/urlhandler/protocol_spy.py
+++ b/serial/urlhandler/protocol_spy.py
@@ -173,7 +173,7 @@
raise serial.SerialException(
'expected a string in the form '
'"spy://port[?option[=value][&option[=value]]]": '
- 'not starting with spy:// (%r)' % (parts.scheme,))
+ 'not starting with spy:// ({!r})'.format(parts.scheme))
# process options now, directly altering self
formatter = FormatHexdump
color = False
@@ -189,11 +189,11 @@
elif option == 'all':
self.show_all = True
else:
- raise ValueError('unknown option: %r' % (option,))
+ raise ValueError('unknown option: {!r}'.format(option))
except ValueError as e:
raise serial.SerialException(
'expected a string in the form '
- '"spy://port[?option[=value][&option[=value]]]": %s' % e)
+ '"spy://port[?option[=value][&option[=value]]]": {}'.format(e))
self.formatter = formatter(output, color)
return ''.join([parts.netloc, parts.path])