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
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)

View file

@ -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):
'''

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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,

View file

@ -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)

View file

@ -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):