From f7e314f392fa9b7baa445757ecff3b8234c7b13e Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Tue, 18 Feb 2020 00:42:33 -0800 Subject: [PATCH] Replace some % formatters with fstrings. --- voussoirkit/basenumber.py | 2 +- voussoirkit/bytestring.py | 2 +- voussoirkit/expressionmatch.py | 17 ++++++++--------- voussoirkit/pathclass.py | 2 +- voussoirkit/ratelimiter.py | 2 +- voussoirkit/spinal.py | 4 ++-- voussoirkit/sqlhelpers.py | 12 ++++++------ voussoirkit/treeclass.py | 2 +- 8 files changed, 21 insertions(+), 22 deletions(-) diff --git a/voussoirkit/basenumber.py b/voussoirkit/basenumber.py index d45f4a5..5b5ed13 100644 --- a/voussoirkit/basenumber.py +++ b/voussoirkit/basenumber.py @@ -63,7 +63,7 @@ def to_base(number, base, decimal_places=10, alphabet=None): alphabet = ALPHABET if base > len(alphabet): - raise ValueError('Not enough symbols in alphabet for base %d' % base) + raise ValueError(f'Not enough symbols in alphabet for base {base}.') result = '' whole_portion = int(number) diff --git a/voussoirkit/bytestring.py b/voussoirkit/bytestring.py index b2f8fde..cc633e5 100644 --- a/voussoirkit/bytestring.py +++ b/voussoirkit/bytestring.py @@ -90,7 +90,7 @@ def normalize_unit_string(string): unit_string_l = unit_string.lower() if string in (unit_string_l, unit_string_l[0], unit_string_l.replace('i', '')): return unit_string - raise ValueError('Unrecognized unit string "%s"' % string) + raise ValueError(f'Unrecognized unit string "{string}".') def parsebytes(string): ''' diff --git a/voussoirkit/expressionmatch.py b/voussoirkit/expressionmatch.py index 9ee6b17..1165ed9 100644 --- a/voussoirkit/expressionmatch.py +++ b/voussoirkit/expressionmatch.py @@ -65,28 +65,27 @@ class ExpressionTree: t = t.replace('(', '\\(') t = t.replace(')', '\\)') if ' ' in t: - t = '"%s"' % t + t = f'"{t}"' return t if len(self.children) == 1: child = self.children[0] childstring = str(child) if child.token in OPERATORS: - childstring = '(%s)' % childstring - return '%s%s' % (self.token, childstring) - return '%s %s' % (self.token, childstring) + return f'{self.token}({childstring})' + return f'{self.token} {childstring}' children = [] for child in self.children: childstring = str(child) if child.token in OPERATORS: - childstring = '(%s)' % childstring + childstring = f'({childstring})' children.append(childstring) if len(children) == 1: - return '%s %s' % (self.token, children[0]) + return f'{self.token} {children[0]}' - s = ' %s ' % self.token + s = f' {self.token} ' s = s.join(children) return s @@ -119,7 +118,7 @@ class ExpressionTree: current.parent = new current = new else: - raise Exception('Expected binary operator, got %s.' % new.token) + raise Exception(f'Expected binary operator, got {new.token}.') elif current.token in BINARY_OPERATORS: if new.token in BINARY_OPERATORS: @@ -191,7 +190,7 @@ class ExpressionTree: return '""' t = self.token if ' ' in t: - t = '"%s"' % t + t = f'"{t}"' output = t indent = 1 diff --git a/voussoirkit/pathclass.py b/voussoirkit/pathclass.py index 7e264a7..0bd6c98 100644 --- a/voussoirkit/pathclass.py +++ b/voussoirkit/pathclass.py @@ -340,7 +340,7 @@ def glob_patternize(piece): piece = glob.escape(piece) for character in piece: if character not in '![]': - replacement = '[%s]' % character + replacement = f'[{character}]' piece = piece.replace(character, replacement, 1) break return piece diff --git a/voussoirkit/ratelimiter.py b/voussoirkit/ratelimiter.py index 494ad3e..d721661 100644 --- a/voussoirkit/ratelimiter.py +++ b/voussoirkit/ratelimiter.py @@ -42,7 +42,7 @@ class Ratelimiter: single second, then relax for 29 seconds, for example. ''' if mode not in ('sleep', 'reject'): - raise ValueError('Invalid mode %s' % repr(mode)) + raise ValueError(f'Invalid mode {repr(mode)}.') self.allowance = allowance self.period = period diff --git a/voussoirkit/spinal.py b/voussoirkit/spinal.py index 49f38a2..4084f72 100644 --- a/voussoirkit/spinal.py +++ b/voussoirkit/spinal.py @@ -59,7 +59,7 @@ def callback_v1(fpobj, written_bytes, total_bytes): else: ends = '' percent = (100 * written_bytes) / max(total_bytes, 1) - percent = '%07.3f' % percent + percent = f'{percent:07.3f}' written = '{:,}'.format(written_bytes) total = '{:,}'.format(total_bytes) written = written.rjust(len(total), ' ') @@ -81,7 +81,7 @@ def copy(source, file_args=None, file_kwargs=None, dir_args=None, dir_kwargs=Non dir_args = dir_args or tuple() dir_kwargs = dir_kwargs or dict() return copy_dir(source, *dir_args, **dir_kwargs) - raise SpinalError('Neither file nor dir: %s' % source) + raise SpinalError(f'Neither file nor dir: {source}') def copy_dir( source, diff --git a/voussoirkit/sqlhelpers.py b/voussoirkit/sqlhelpers.py index e06c460..a24f4b0 100644 --- a/voussoirkit/sqlhelpers.py +++ b/voussoirkit/sqlhelpers.py @@ -13,16 +13,16 @@ def delete_filler(pairs): In context: (qmarks, bindings) = delete_filler(pairs) - query = 'DELETE FROM table %s' % qmarks + query = f'DELETE FROM table {qmarks}' cur.execute(query, bindings) ''' qmarks = [] bindings = [] for (key, value) in pairs.items(): - qmarks.append('%s = ?' % key) + qmarks.append(f'{key} = ?') bindings.append(value) qmarks = ' AND '.join(qmarks) - qmarks = 'WHERE %s' % qmarks + qmarks = f'WHERE {qmarks}' return (qmarks, bindings) def insert_filler(column_names, values, require_all=True): @@ -44,7 +44,7 @@ def insert_filler(column_names, values, require_all=True): In context: (qmarks, bindings) = insert_filler(COLUMN_NAMES, data) - query = 'INSERT INTO table VALUES(%s)' % qmarks + query = f'INSERT INTO table VALUES({qmarks})' cur.execute(query, bindings) ''' values = values.copy() @@ -88,7 +88,7 @@ def update_filler(pairs, where_key): In context: (qmarks, bindings) = update_filler(data, where_key) - query = 'UPDATE table %s' % qmarks + query = f'UPDATE table {qmarks}' cur.execute(query, bindings) ''' pairs = pairs.copy() @@ -105,7 +105,7 @@ def update_filler(pairs, where_key): qmarks = [] bindings = [] for (key, value) in pairs.items(): - qmarks.append('%s = ?' % key) + qmarks.append(f'{key} = ?') bindings.append(value) bindings.append(where_value) setters = ', '.join(qmarks) diff --git a/voussoirkit/treeclass.py b/voussoirkit/treeclass.py index a8e5465..738b2f3 100644 --- a/voussoirkit/treeclass.py +++ b/voussoirkit/treeclass.py @@ -22,7 +22,7 @@ class Tree: return hash(self.abspath()) def __repr__(self): - return 'Tree(%s)' % self.identifier + return f'Tree({self.identifier})' @staticmethod def assert_identifier_ok(identifier):