diff options
author | Chris Liechti <cliechti@gmx.net> | 2016-02-15 23:48:45 +0100 |
---|---|---|
committer | Chris Liechti <cliechti@gmx.net> | 2016-02-15 23:48:45 +0100 |
commit | 984c5c5baf53b35504e1696d1d8428c3b83cf25b (patch) | |
tree | a93299ec715ed55cc8f2db656121ac53f666ee49 | |
parent | 0269b2cb8545a0979295187cc2fabbddc9c60094 (diff) | |
download | pyserial-git-984c5c5baf53b35504e1696d1d8428c3b83cf25b.tar.gz |
style: replace % with format calls
-rw-r--r-- | serial/serialposix.py | 42 | ||||
-rw-r--r-- | serial/serialutil.py | 57 |
2 files changed, 45 insertions, 54 deletions
diff --git a/serial/serialposix.py b/serial/serialposix.py index c920cbe..ae113c2 100644 --- a/serial/serialposix.py +++ b/serial/serialposix.py @@ -35,15 +35,15 @@ don't know how to number ttys on this system. ! Use an explicit path (eg /dev/ttyS1) or send this information to ! the author of this module: -sys.platform = %r -os.name = %r -serialposix.py version = %s +sys.platform = {!r} +os.name = {!r} +serialposix.py version = {} also add the device name of the serial port and where the counting starts for the first serial port. e.g. 'first serial port: /dev/ttyS0' and with a bit luck you can get this module running... -""" % (sys.platform, os.name, serial.VERSION)) +""".format(sys.platform, os.name, serial.VERSION)) raise NotImplementedError('no number-to-device mapping defined on this platform') def _set_special_baudrate(self, baudrate): @@ -125,7 +125,7 @@ if plat[:5] == 'linux': # Linux (confirmed) # noqa # set serial_struct fcntl.ioctl(self.fd, TCSETS2, buf) except IOError as e: - raise ValueError('Failed to set custom baud rate (%s): %s' % (baudrate, e)) + raise ValueError('Failed to set custom baud rate ({}): {}'.format(baudrate, e)) def _set_rs485_mode(self, rs485_settings): buf = array.array('i', [0] * 8) # flags, delaytx, delayrx, padding @@ -150,7 +150,7 @@ if plat[:5] == 'linux': # Linux (confirmed) # noqa buf[0] = 0 # clear SER_RS485_ENABLED fcntl.ioctl(self.fd, TIOCSRS485, buf) except IOError as e: - raise ValueError('Failed to set RS485 mode: %s' % (e,)) + raise ValueError('Failed to set RS485 mode: {}'.format(e)) elif plat == 'cygwin': # cygwin/win32 (confirmed) @@ -292,7 +292,7 @@ class Serial(SerialBase, PlatformSpecific): self.fd = os.open(self.portstr, os.O_RDWR | os.O_NOCTTY | os.O_NONBLOCK) except OSError as msg: self.fd = None - raise SerialException(msg.errno, "could not open port %s: %s" % (self._port, msg)) + raise SerialException(msg.errno, "could not open port {}: {}".format(self._port, msg)) #~ fcntl.fcntl(self.fd, fcntl.F_SETFL, 0) # set blocking try: @@ -328,7 +328,7 @@ class Serial(SerialBase, PlatformSpecific): orig_attr = termios.tcgetattr(self.fd) iflag, oflag, cflag, lflag, ispeed, ospeed, cc = orig_attr except termios.error as msg: # if a port is nonexistent but has a /dev file, it'll fail here - raise SerialException("Could not configure port: %s" % msg) + raise SerialException("Could not configure port: {}".format(msg)) # set up raw mode / no echo / binary cflag |= (termios.CLOCAL | termios.CREAD) lflag &= ~(termios.ICANON | termios.ECHO | termios.ECHOE | @@ -347,7 +347,7 @@ class Serial(SerialBase, PlatformSpecific): # setup baud rate try: - ispeed = ospeed = getattr(termios, 'B%s' % (self._baudrate)) + ispeed = ospeed = getattr(termios, 'B{}'.format(self._baudrate)) except AttributeError: try: ispeed = ospeed = self.BAUDRATE_CONSTANTS[self._baudrate] @@ -358,10 +358,10 @@ class Serial(SerialBase, PlatformSpecific): try: custom_baud = int(self._baudrate) # store for later except ValueError: - raise ValueError('Invalid baud rate: %r' % self._baudrate) + raise ValueError('Invalid baud rate: {!r}'.format(self._baudrate)) else: if custom_baud < 0: - raise ValueError('Invalid baud rate: %r' % self._baudrate) + raise ValueError('Invalid baud rate: {!r}'.format(self._baudrate)) # setup char len cflag &= ~termios.CSIZE @@ -374,7 +374,7 @@ class Serial(SerialBase, PlatformSpecific): elif self._bytesize == 5: cflag |= termios.CS5 else: - raise ValueError('Invalid char len: %r' % self._bytesize) + raise ValueError('Invalid char len: {!r}'.format(self._bytesize)) # setup stop bits if self._stopbits == serial.STOPBITS_ONE: cflag &= ~(termios.CSTOPB) @@ -383,7 +383,7 @@ class Serial(SerialBase, PlatformSpecific): elif self._stopbits == serial.STOPBITS_TWO: cflag |= (termios.CSTOPB) else: - raise ValueError('Invalid stop bit specification: %r' % self._stopbits) + raise ValueError('Invalid stop bit specification: {!r}'.format(self._stopbits)) # setup parity iflag &= ~(termios.INPCK | termios.ISTRIP) if self._parity == serial.PARITY_NONE: @@ -399,7 +399,7 @@ class Serial(SerialBase, PlatformSpecific): cflag |= (termios.PARENB | CMSPAR) cflag &= ~(termios.PARODD) else: - raise ValueError('Invalid parity: %r' % self._parity) + raise ValueError('Invalid parity: {!r}'.format(self._parity)) # setup flow control # xonxoff if hasattr(termios, 'IXANY'): @@ -428,11 +428,11 @@ class Serial(SerialBase, PlatformSpecific): # buffer # vmin "minimal number of characters to be read. 0 for non blocking" if vmin < 0 or vmin > 255: - raise ValueError('Invalid vmin: %r ' % vmin) + raise ValueError('Invalid vmin: {!r}'.format(vmin)) cc[termios.VMIN] = vmin # vtime if vtime < 0 or vtime > 255: - raise ValueError('Invalid vtime: %r' % vtime) + raise ValueError('Invalid vtime: {!r}'.format(vtime)) cc[termios.VTIME] = vtime # activate settings if force_update or [iflag, oflag, cflag, lflag, ispeed, ospeed, cc] != orig_attr: @@ -505,13 +505,13 @@ class Serial(SerialBase, PlatformSpecific): # 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): @@ -547,7 +547,7 @@ class Serial(SerialBase, PlatformSpecific): raise except OSError as v: if v.errno != errno.EAGAIN: - raise SerialException('write failed: %s' % (v,)) + raise SerialException('write failed: {}'.format(v)) # still calculate and check timeout if timeout and timeout - time.time() < 0: raise writeTimeoutError @@ -753,10 +753,10 @@ class VTIMESerial(Serial): orig_attr = termios.tcgetattr(self.fd) iflag, oflag, cflag, lflag, ispeed, ospeed, cc = orig_attr except termios.error as msg: # if a port is nonexistent but has a /dev file, it'll fail here - raise serial.SerialException("Could not configure port: %s" % msg) + raise serial.SerialException("Could not configure port: {}".format(msg)) if vtime < 0 or vtime > 255: - raise ValueError('Invalid vtime: %r' % vtime) + raise ValueError('Invalid vtime: {!r}'.format(vtime)) cc[termios.VTIME] = vtime cc[termios.VMIN] = vmin diff --git a/serial/serialutil.py b/serial/serialutil.py index 3474d93..de75ec0 100644 --- a/serial/serialutil.py +++ b/serial/serialutil.py @@ -55,7 +55,7 @@ def to_bytes(seq): elif isinstance(seq, memoryview): return seq.tobytes() elif isinstance(seq, unicode): - raise TypeError('unicode strings are not supported, please encode to bytes: %r' % (seq,)) + raise TypeError('unicode strings are not supported, please encode to bytes: {!r}'.format(seq)) else: b = bytearray() for item in seq: @@ -171,7 +171,7 @@ class SerialBase(io.RawIOBase): if 'interCharTimeout' in kwargs: self.inter_byte_timeout = kwargs.pop('interCharTimeout') if kwargs: - raise ValueError('unexpected keyword arguments: %r' % (kwargs,)) + raise ValueError('unexpected keyword arguments: {!r}'.format(kwargs)) if port is not None: self.open() @@ -224,10 +224,10 @@ class SerialBase(io.RawIOBase): try: b = int(baudrate) except TypeError: - raise ValueError("Not a valid baudrate: %r" % (baudrate,)) + raise ValueError("Not a valid baudrate: {!r}".format(baudrate)) else: if b <= 0: - raise ValueError("Not a valid baudrate: %r" % (baudrate,)) + raise ValueError("Not a valid baudrate: {!r}".format(baudrate)) self._baudrate = b if self.is_open: self._reconfigure_port() @@ -241,7 +241,7 @@ class SerialBase(io.RawIOBase): def bytesize(self, bytesize): """Change byte size.""" if bytesize not in self.BYTESIZES: - raise ValueError("Not a valid byte size: %r" % (bytesize,)) + raise ValueError("Not a valid byte size: {!r}".format(bytesize)) self._bytesize = bytesize if self.is_open: self._reconfigure_port() @@ -255,7 +255,7 @@ class SerialBase(io.RawIOBase): def parity(self, parity): """Change parity setting.""" if parity not in self.PARITIES: - raise ValueError("Not a valid parity: %r" % (parity,)) + raise ValueError("Not a valid parity: {!r}".format(parity)) self._parity = parity if self.is_open: self._reconfigure_port() @@ -269,7 +269,7 @@ class SerialBase(io.RawIOBase): def stopbits(self, stopbits): """Change stop bits size.""" if stopbits not in self.STOPBITS: - raise ValueError("Not a valid stop bit size: %r" % (stopbits,)) + raise ValueError("Not a valid stop bit size: {!r}".format(stopbits)) self._stopbits = stopbits if self.is_open: self._reconfigure_port() @@ -286,9 +286,9 @@ class SerialBase(io.RawIOBase): try: timeout + 1 # test if it's a number, will throw a TypeError if not... except TypeError: - raise ValueError("Not a valid timeout: %r" % (timeout,)) + raise ValueError("Not a valid timeout: {!r}".format(timeout)) if timeout < 0: - raise ValueError("Not a valid timeout: %r" % (timeout,)) + raise ValueError("Not a valid timeout: {!r}".format(timeout)) self._timeout = timeout if self.is_open: self._reconfigure_port() @@ -303,11 +303,11 @@ class SerialBase(io.RawIOBase): """Change timeout setting.""" if timeout is not None: if timeout < 0: - raise ValueError("Not a valid timeout: %r" % (timeout,)) + raise ValueError("Not a valid timeout: {!r}".format(timeout)) try: timeout + 1 # test if it's a number, will throw a TypeError if not... except TypeError: - raise ValueError("Not a valid timeout: %r" % timeout) + raise ValueError("Not a valid timeout: {!r}".format(timeout)) self._write_timeout = timeout if self.is_open: @@ -323,11 +323,11 @@ class SerialBase(io.RawIOBase): """Change inter-byte timeout setting.""" if ic_timeout is not None: if ic_timeout < 0: - raise ValueError("Not a valid timeout: %r" % ic_timeout) + raise ValueError("Not a valid timeout: {!r}".format(ic_timeout)) try: ic_timeout + 1 # test if it's a number, will throw a TypeError if not... except TypeError: - raise ValueError("Not a valid timeout: %r" % ic_timeout) + raise ValueError("Not a valid timeout: {!r}".format(ic_timeout)) self._inter_byte_timeout = ic_timeout if self.is_open: @@ -448,20 +448,11 @@ class SerialBase(io.RawIOBase): def __repr__(self): """String representation of the current port settings and its state.""" - return "%s<id=0x%x, open=%s>(port=%r, baudrate=%r, bytesize=%r, parity=%r, stopbits=%r, timeout=%r, xonxoff=%r, rtscts=%r, dsrdtr=%r)" % ( - self.__class__.__name__, - id(self), - self.is_open, - self.portstr, - self.baudrate, - self.bytesize, - self.parity, - self.stopbits, - self.timeout, - self.xonxoff, - self.rtscts, - self.dsrdtr, - ) + return '{name}<id=0x{id:x}, open={p.is_open}>(port={p.portstr!r}, ' \ + 'baudrate={p.baudrate!r}, bytesize={p.bytesize!r}, parity={p.parity!r}, ' \ + 'stopbits={p.stopbits!r}, timeout={p.timeout!r}, xonxoff={p.xonxoff!r}, ' \ + 'rtscts={p.rtscts!r}, dsrdtr={p.dsrdtr!r})'.format( + name=self.__class__.__name__, id=id(self), p=self) # - - - - - - - - - - - - - - - - - - - - - - - - # compatibility with io library @@ -612,9 +603,9 @@ class SerialBase(io.RawIOBase): if __name__ == '__main__': import sys s = SerialBase() - sys.stdout.write('port name: %s\n' % s.name) - sys.stdout.write('baud rates: %s\n' % s.BAUDRATES) - sys.stdout.write('byte sizes: %s\n' % s.BYTESIZES) - sys.stdout.write('parities: %s\n' % s.PARITIES) - sys.stdout.write('stop bits: %s\n' % s.STOPBITS) - sys.stdout.write('%s\n' % s) + sys.stdout.write('port name: {}\n'.format(s.name)) + sys.stdout.write('baud rates: {}\n'.format(s.BAUDRATES)) + sys.stdout.write('byte sizes: {}\n'.format(s.BYTESIZES)) + sys.stdout.write('parities: {}\n'.format(s.PARITIES)) + sys.stdout.write('stop bits: {}\n'.format(s.STOPBITS)) + sys.stdout.write('{}\n'.format(s)) |