summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorChris Liechti <cliechti@gmx.net>2016-06-21 23:22:53 +0200
committerChris Liechti <cliechti@gmx.net>2016-06-21 23:22:53 +0200
commit9a16666a667ca476b50c68d6ebdc515e6fa8c44d (patch)
tree28d35eb64d92c0a1cf89d0a6a732a137c002974e
parent3debab29c37fd08d2aca481b2bccd1c45f1ceea6 (diff)
downloadpyserial-git-9a16666a667ca476b50c68d6ebdc515e6fa8c44d.tar.gz
style: use .format() instead of % formatting
-rw-r--r--examples/at_protocol.py8
-rwxr-xr-xexamples/port_publisher.py26
-rwxr-xr-xexamples/rfc2217_server.py4
-rwxr-xr-xexamples/tcp_serial_redirect.py2
-rwxr-xr-xexamples/wxSerialConfigDialog.py4
-rwxr-xr-xexamples/wxTerminal.py18
6 files changed, 31 insertions, 31 deletions
diff --git a/examples/at_protocol.py b/examples/at_protocol.py
index 36eb6bd..e3a9bde 100644
--- a/examples/at_protocol.py
+++ b/examples/at_protocol.py
@@ -91,7 +91,7 @@ class ATProtocol(serial.threaded.LineReader):
else:
lines.append(line)
except queue.Empty:
- raise ATException('AT command timeout (%r)' % (command,))
+ raise ATException('AT command timeout ({!r})'.format(command))
# test
@@ -123,16 +123,16 @@ if __name__ == '__main__':
"""Handle events and command responses starting with '+...'"""
if event.startswith('+RRBDRES') and self._awaiting_response_for.startswith('AT+JRBD'):
rev = event[9:9 + 12]
- mac = ':'.join('%02X' % ord(x) for x in rev.decode('hex')[::-1])
+ mac = ':'.join('{:02X}'.format(ord(x)) for x in rev.decode('hex')[::-1])
self.event_responses.put(mac)
else:
- logging.warning('unhandled event: %r' % event)
+ logging.warning('unhandled event: {!r}'.format(event))
def command_with_event_response(self, command):
"""Send a command that responds with '+...' line"""
with self.lock: # ensure that just one thread is sending commands at once
self._awaiting_response_for = command
- self.transport.write(b'%s\r\n' % (command.encode(self.ENCODING, self.UNICODE_HANDLING),))
+ self.transport.write(b'{}\r\n'.format(command.encode(self.ENCODING, self.UNICODE_HANDLING)))
response = self.event_responses.get()
self._awaiting_response_for = None
return response
diff --git a/examples/port_publisher.py b/examples/port_publisher.py
index cf44945..ae07f77 100755
--- a/examples/port_publisher.py
+++ b/examples/port_publisher.py
@@ -86,7 +86,7 @@ class ZeroconfService:
self.group = None
def __str__(self):
- return "%r @ %s:%s (%s)" % (self.name, self.host, self.port, self.stype)
+ return "{!r} @ {}:{} ({})".format(self.name, self.host, self.port, self.stype)
class Forwarder(ZeroconfService):
@@ -154,7 +154,7 @@ class Forwarder(ZeroconfService):
self.handle_server_error()
#~ raise
if self.log is not None:
- self.log.info("%s: Waiting for connection on %s..." % (self.device, self.network_port))
+ self.log.info("{}: Waiting for connection on {}...".format(self.device, self.network_port))
# zeroconfig
self.publish()
@@ -165,7 +165,7 @@ class Forwarder(ZeroconfService):
def close(self):
"""Close all resources and unpublish service"""
if self.log is not None:
- self.log.info("%s: closing..." % (self.device, ))
+ self.log.info("{}: closing...".format(self.device))
self.alive = False
self.unpublish()
if self.server_socket:
@@ -291,7 +291,7 @@ class Forwarder(ZeroconfService):
self.socket.setblocking(0)
self.socket.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
if self.log is not None:
- self.log.warning('%s: Connected by %s:%s' % (self.device, addr[0], addr[1]))
+ self.log.warning('{}: Connected by {}:{}'.format(self.device, addr[0], addr[1]))
self.serial.rts = True
self.serial.dtr = True
if self.log is not None:
@@ -302,7 +302,7 @@ class Forwarder(ZeroconfService):
# reject connection if there is already one
connection.close()
if self.log is not None:
- self.log.warning('%s: Rejecting connect from %s:%s' % (self.device, addr[0], addr[1]))
+ self.log.warning('{}: Rejecting connect from {}:{}'.format(self.device, addr[0], addr[1]))
def handle_server_error(self):
"""Socket server fails"""
@@ -326,7 +326,7 @@ class Forwarder(ZeroconfService):
self.socket.close()
self.socket = None
if self.log is not None:
- self.log.warning('%s: Disconnected' % self.device)
+ self.log.warning('{}: Disconnected'.format(self.device))
def test():
@@ -451,7 +451,7 @@ terminated, it waits for the next connect.
# exit first parent
sys.exit(0)
except OSError as e:
- log.critical("fork #1 failed: %d (%s)\n" % (e.errno, e.strerror))
+ log.critical("fork #1 failed: {} ({})\n".format(e.errno, e.strerror))
sys.exit(1)
# decouple from parent environment
@@ -465,10 +465,10 @@ terminated, it waits for the next connect.
if pid > 0:
# exit from second parent, save eventual PID before
if args.pidfile is not None:
- open(args.pidfile, 'w').write("%d" % pid)
+ open(args.pidfile, 'w').write("{}".formt(pid))
sys.exit(0)
except OSError as e:
- log.critical("fork #2 failed: %d (%s)\n" % (e.errno, e.strerror))
+ log.critical("fork #2 failed: {} ({})\n".format(e.errno, e.strerror))
sys.exit(1)
if args.logfile is None:
@@ -512,7 +512,7 @@ terminated, it waits for the next connect.
except KeyError:
pass
else:
- log.info("unpublish: %s" % (forwarder))
+ log.info("unpublish: {}".format(forwarder))
alive = True
next_check = 0
@@ -526,7 +526,7 @@ terminated, it waits for the next connect.
connected = [d for d, p, i in serial.tools.list_ports.grep(args.ports_regex)]
# Handle devices that are published, but no longer connected
for device in set(published).difference(connected):
- log.info("unpublish: %s" % (published[device]))
+ log.info("unpublish: {}".format(published[device]))
unpublish(published[device])
# Handle devices that are connected but not yet published
for device in sorted(set(connected).difference(published)):
@@ -537,11 +537,11 @@ terminated, it waits for the next connect.
port += 1
published[device] = Forwarder(
device,
- "%s on %s" % (device, hostname),
+ "{} on {}".format(device, hostname),
port,
on_close=unpublish,
log=log)
- log.warning("publish: %s" % (published[device]))
+ log.warning("publish: {}".format(published[device]))
published[device].open()
# select_start = time.time()
diff --git a/examples/rfc2217_server.py b/examples/rfc2217_server.py
index 7830e40..5955fc0 100755
--- a/examples/rfc2217_server.py
+++ b/examples/rfc2217_server.py
@@ -58,7 +58,7 @@ class Redirector(object):
# escape outgoing data when needed (Telnet IAC (0xff) character)
self.write(serial.to_bytes(self.rfc2217.escape(data)))
except socket.error as msg:
- self.log.error('%s' % (msg,))
+ self.log.error('{}'.format(msg))
# probably got disconnected
break
self.alive = False
@@ -78,7 +78,7 @@ class Redirector(object):
break
self.serial.write(serial.to_bytes(self.rfc2217.filter(data)))
except socket.error as msg:
- self.log.error('%s' % (msg,))
+ self.log.error('{}'.format(msg))
# probably got disconnected
break
self.stop()
diff --git a/examples/tcp_serial_redirect.py b/examples/tcp_serial_redirect.py
index 97a73b4..1d8b917 100755
--- a/examples/tcp_serial_redirect.py
+++ b/examples/tcp_serial_redirect.py
@@ -146,7 +146,7 @@ it waits for the next connect.
break
ser.write(data) # get a bunch of bytes and send them
except socket.error as msg:
- sys.stderr.write('ERROR: %s\n' % msg)
+ sys.stderr.write('ERROR: {}\n'.format(msg))
# probably got disconnected
break
except socket.error as msg:
diff --git a/examples/wxSerialConfigDialog.py b/examples/wxSerialConfigDialog.py
index a29b67d..0064a9c 100755
--- a/examples/wxSerialConfigDialog.py
+++ b/examples/wxSerialConfigDialog.py
@@ -99,7 +99,7 @@ class SerialConfigDialog(wx.Dialog):
self.choice_port.Clear()
self.ports = []
for n, (portname, desc, hwid) in enumerate(sorted(serial.tools.list_ports.comports())):
- self.choice_port.Append('%s - %s' % (portname, desc))
+ self.choice_port.Append(u'{} - {}'.format(portname, desc))
self.ports.append(portname)
if self.serial.name == portname:
preferred_index = n
@@ -115,7 +115,7 @@ class SerialConfigDialog(wx.Dialog):
if preferred_index is not None:
self.combo_box_baudrate.SetSelection(preferred_index)
else:
- self.combo_box_baudrate.SetValue(u'%d' % (self.serial.baudrate,))
+ self.combo_box_baudrate.SetValue(u'{}'.format(self.serial.baudrate))
if self.show & SHOW_FORMAT:
# fill in data bits and select current setting
self.choice_databits.Clear()
diff --git a/examples/wxTerminal.py b/examples/wxTerminal.py
index 4ebabb7..0811721 100755
--- a/examples/wxTerminal.py
+++ b/examples/wxTerminal.py
@@ -274,15 +274,15 @@ class TerminalFrame(wx.Frame):
dlg.ShowModal()
else:
self.StartThread()
- self.SetTitle("Serial Terminal on %s [%s,%s,%s,%s%s%s]" % (
- self.serial.portstr,
- self.serial.baudrate,
- self.serial.bytesize,
- self.serial.parity,
- self.serial.stopbits,
- ' RTS/CTS' if self.serial.rtscts else '',
- ' Xon/Xoff' if self.serial.xonxoff else '',
- ))
+ self.SetTitle("Serial Terminal on {} [{},{},{},{}{}{}]".format(
+ self.serial.portstr,
+ self.serial.baudrate,
+ self.serial.bytesize,
+ self.serial.parity,
+ self.serial.stopbits,
+ ' RTS/CTS' if self.serial.rtscts else '',
+ ' Xon/Xoff' if self.serial.xonxoff else '',
+ ))
ok = True
else:
# on startup, dialog aborted