Replace some % formatters with fstrings.

This commit is contained in:
Ethan Dalool 2020-02-18 00:42:33 -08:00
parent 7c0f3979a4
commit f7e314f392
8 changed files with 21 additions and 22 deletions

View file

@ -63,7 +63,7 @@ def to_base(number, base, decimal_places=10, alphabet=None):
alphabet = ALPHABET alphabet = ALPHABET
if base > len(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 = '' result = ''
whole_portion = int(number) whole_portion = int(number)

View file

@ -90,7 +90,7 @@ def normalize_unit_string(string):
unit_string_l = unit_string.lower() unit_string_l = unit_string.lower()
if string in (unit_string_l, unit_string_l[0], unit_string_l.replace('i', '')): if string in (unit_string_l, unit_string_l[0], unit_string_l.replace('i', '')):
return unit_string return unit_string
raise ValueError('Unrecognized unit string "%s"' % string) raise ValueError(f'Unrecognized unit string "{string}".')
def parsebytes(string): def parsebytes(string):
''' '''

View file

@ -65,28 +65,27 @@ class ExpressionTree:
t = t.replace('(', '\\(') t = t.replace('(', '\\(')
t = t.replace(')', '\\)') t = t.replace(')', '\\)')
if ' ' in t: if ' ' in t:
t = '"%s"' % t t = f'"{t}"'
return t return t
if len(self.children) == 1: if len(self.children) == 1:
child = self.children[0] child = self.children[0]
childstring = str(child) childstring = str(child)
if child.token in OPERATORS: if child.token in OPERATORS:
childstring = '(%s)' % childstring return f'{self.token}({childstring})'
return '%s%s' % (self.token, childstring) return f'{self.token} {childstring}'
return '%s %s' % (self.token, childstring)
children = [] children = []
for child in self.children: for child in self.children:
childstring = str(child) childstring = str(child)
if child.token in OPERATORS: if child.token in OPERATORS:
childstring = '(%s)' % childstring childstring = f'({childstring})'
children.append(childstring) children.append(childstring)
if len(children) == 1: 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) s = s.join(children)
return s return s
@ -119,7 +118,7 @@ class ExpressionTree:
current.parent = new current.parent = new
current = new current = new
else: 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: elif current.token in BINARY_OPERATORS:
if new.token in BINARY_OPERATORS: if new.token in BINARY_OPERATORS:
@ -191,7 +190,7 @@ class ExpressionTree:
return '""' return '""'
t = self.token t = self.token
if ' ' in t: if ' ' in t:
t = '"%s"' % t t = f'"{t}"'
output = t output = t
indent = 1 indent = 1

View file

@ -340,7 +340,7 @@ def glob_patternize(piece):
piece = glob.escape(piece) piece = glob.escape(piece)
for character in piece: for character in piece:
if character not in '![]': if character not in '![]':
replacement = '[%s]' % character replacement = f'[{character}]'
piece = piece.replace(character, replacement, 1) piece = piece.replace(character, replacement, 1)
break break
return piece return piece

View file

@ -42,7 +42,7 @@ class Ratelimiter:
single second, then relax for 29 seconds, for example. single second, then relax for 29 seconds, for example.
''' '''
if mode not in ('sleep', 'reject'): if mode not in ('sleep', 'reject'):
raise ValueError('Invalid mode %s' % repr(mode)) raise ValueError(f'Invalid mode {repr(mode)}.')
self.allowance = allowance self.allowance = allowance
self.period = period self.period = period

View file

@ -59,7 +59,7 @@ def callback_v1(fpobj, written_bytes, total_bytes):
else: else:
ends = '' ends = ''
percent = (100 * written_bytes) / max(total_bytes, 1) percent = (100 * written_bytes) / max(total_bytes, 1)
percent = '%07.3f' % percent percent = f'{percent:07.3f}'
written = '{:,}'.format(written_bytes) written = '{:,}'.format(written_bytes)
total = '{:,}'.format(total_bytes) total = '{:,}'.format(total_bytes)
written = written.rjust(len(total), ' ') 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_args = dir_args or tuple()
dir_kwargs = dir_kwargs or dict() dir_kwargs = dir_kwargs or dict()
return copy_dir(source, *dir_args, **dir_kwargs) 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( def copy_dir(
source, source,

View file

@ -13,16 +13,16 @@ def delete_filler(pairs):
In context: In context:
(qmarks, bindings) = delete_filler(pairs) (qmarks, bindings) = delete_filler(pairs)
query = 'DELETE FROM table %s' % qmarks query = f'DELETE FROM table {qmarks}'
cur.execute(query, bindings) cur.execute(query, bindings)
''' '''
qmarks = [] qmarks = []
bindings = [] bindings = []
for (key, value) in pairs.items(): for (key, value) in pairs.items():
qmarks.append('%s = ?' % key) qmarks.append(f'{key} = ?')
bindings.append(value) bindings.append(value)
qmarks = ' AND '.join(qmarks) qmarks = ' AND '.join(qmarks)
qmarks = 'WHERE %s' % qmarks qmarks = f'WHERE {qmarks}'
return (qmarks, bindings) return (qmarks, bindings)
def insert_filler(column_names, values, require_all=True): def insert_filler(column_names, values, require_all=True):
@ -44,7 +44,7 @@ def insert_filler(column_names, values, require_all=True):
In context: In context:
(qmarks, bindings) = insert_filler(COLUMN_NAMES, data) (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) cur.execute(query, bindings)
''' '''
values = values.copy() values = values.copy()
@ -88,7 +88,7 @@ def update_filler(pairs, where_key):
In context: In context:
(qmarks, bindings) = update_filler(data, where_key) (qmarks, bindings) = update_filler(data, where_key)
query = 'UPDATE table %s' % qmarks query = f'UPDATE table {qmarks}'
cur.execute(query, bindings) cur.execute(query, bindings)
''' '''
pairs = pairs.copy() pairs = pairs.copy()
@ -105,7 +105,7 @@ def update_filler(pairs, where_key):
qmarks = [] qmarks = []
bindings = [] bindings = []
for (key, value) in pairs.items(): for (key, value) in pairs.items():
qmarks.append('%s = ?' % key) qmarks.append(f'{key} = ?')
bindings.append(value) bindings.append(value)
bindings.append(where_value) bindings.append(where_value)
setters = ', '.join(qmarks) setters = ', '.join(qmarks)

View file

@ -22,7 +22,7 @@ class Tree:
return hash(self.abspath()) return hash(self.abspath())
def __repr__(self): def __repr__(self):
return 'Tree(%s)' % self.identifier return f'Tree({self.identifier})'
@staticmethod @staticmethod
def assert_identifier_ok(identifier): def assert_identifier_ok(identifier):