Skip to content

Commit 39f3d85

Browse files
committed
Use f-strings where reasonable
But don't report not using them as linting error.
1 parent 51b4010 commit 39f3d85

File tree

10 files changed

+28
-29
lines changed

10 files changed

+28
-29
lines changed

.pylintrc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ init-hook = sys.path.insert(0, 'webware')
1313
disable =
1414
attribute-defined-outside-init,
1515
broad-except,
16+
consider-using-f-string,
1617
cyclic-import,
1718
similarities,
1819
eval-used,

webware/Admin/ServletCache.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ def writeContent(self):
3434
wr('<table>')
3535
for factory in factories:
3636
name = factory.name()
37-
wr('<tr><td><a href="#{0}">{0}</a></td></tr>'.format(name))
37+
wr(f'<tr><td><a href="#{name}">{name}</a></td></tr>')
3838
wr('</table>')
3939
hasField = self.request().hasField
4040
wr('<form action="ServletCache" method="post">')
4141
for factory in factories:
4242
name = factory.name()
43-
wr('<a id="{0}"></a><h4>{0}</h4>'.format(name))
43+
wr(f'<a id="{name}"></a><h4>{name}</h4>')
4444
if hasField('flush_' + name):
4545
factory.flushCache()
4646
wr('<p style="color:green">'

webware/Examples/ListBox.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,24 +21,24 @@ def awake(self, transaction):
2121
self._vars = session.value('vars')
2222
else:
2323
self._vars = dict(
24-
items=[], height=10, width=250,
25-
newCount=1, formCount=1)
24+
items=[], height=10, width=250, newCount=1, formCount=1)
2625
session.setValue('vars', self._vars)
2726
self._error = None
2827

2928
def writeContent(self):
3029
enc, wr = self.htmlEncode, self.writeln
3130
wr('<div style="text-align:center">')
3231
if debug:
33-
wr('<p>fields = {}</p>'.format(enc(str(self.request().fields()))))
34-
wr('<p>vars = {}</p>'.format(enc(str(self._vars))))
32+
wr(f'<p>fields = {enc(str(self.request().fields()))}</p>')
33+
wr(f'<p>vars = {enc(str(self._vars))}</p>')
3534
# Intro text is provided by our class' doc string:
3635
intro = self.__class__.__doc__.strip().split('\n\n')
37-
wr('<h2>{}</h2>'.format(intro.pop(0)))
36+
wr(f'<h2>{intro.pop(0)}</h2>')
3837
for s in intro:
39-
wr('<p>{}</p>'.format('<br>'.join(
40-
s.strip() for s in s.splitlines())))
41-
wr('<p style="color:red">{}</p>'.format(self._error or '&nbsp;'))
38+
s = '<br>'.join(s.strip() for s in s.splitlines())
39+
wr(f'<p>{s}</p>')
40+
s = self._error or '&nbsp;'
41+
wr(f'<p style="color:red">{s}</p>')
4242
wr('''
4343
<form action="ListBox" method="post">
4444
<input name="formCount" type="hidden" value="{formCount}">
@@ -47,8 +47,8 @@ def writeContent(self):
4747
'''.format(**self._vars))
4848
index = 0
4949
for item in self._vars['items']:
50-
wr('<option value="{}">{}</option>'.format(
51-
index, enc(item['name'])))
50+
name = enc(item['name'])
51+
wr(f'<option value="{index}">{name}</option>')
5252
index += 1
5353
if not index:
5454
wr('<option value="" disabled>--- empty ---</option>')
@@ -80,8 +80,8 @@ def widthChange():
8080

8181
def new(self):
8282
"""Add a new item to the list box."""
83-
self._vars['items'].append(dict(
84-
name='New item {}'.format(self._vars['newCount'])))
83+
newCount = self._vars['newCount']
84+
self._vars['items'].append(dict(name=f'New item {newCount}'))
8585
self._vars['newCount'] += 1
8686
self.writeBody()
8787

webware/Examples/View.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,8 @@ def writeContent(self):
2323
filename = req.field('filename')
2424
if sep in filename:
2525
self.write(
26-
'<h3 style="color:red">Error</h3>'
27-
'<p>Cannot request a file'
28-
' outside of this directory {filename!r}</p>')
26+
'<h3 style="color:red">Error</h3><p>Cannot request a file'
27+
f' outside of this directory {filename!r}</p>')
2928
return
3029
filename = self.request().serverSidePath(filename)
3130
self.request().fields()['filename'] = filename

webware/MiscUtils/CSVJoiner.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ def joinCSVFields(fields):
1717
if not isinstance(field, str):
1818
raise UnicodeDecodeError('CSV fields should be strings')
1919
if '"' in field:
20-
newField = '"{}"'.format(field.replace('"', '""'))
20+
field = field.replace('"', '""')
21+
newField = f'"{field}"'
2122
elif ',' in field or '\n' in field or '\r' in field:
2223
newField = f'"{field}"'
2324
else:

webware/MiscUtils/DictForArgs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class DictForArgsError(Exception):
1313

1414

1515
def _SyntaxError(s):
16-
raise DictForArgsError('Syntax error: %r' % s)
16+
raise DictForArgsError(f'Syntax error: {s!r}')
1717

1818

1919
_nameRE = re.compile(r'\w+')

webware/MiscUtils/Tests/TestDictForArgs.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,11 +64,9 @@ def _testNegative(self, input_):
6464
except DictForArgsError:
6565
pass # expected
6666
except Exception as err:
67-
self.fail(
68-
f'Expecting DictForArgError.\nGot error: {err!r}.\n')
67+
self.fail(f'Expecting DictForArgError.\nGot error: {err!r}.\n')
6968
else:
70-
self.fail(
71-
f'Expecting DictForArgError.\nGot result: {res!r}.\n')
69+
self.fail(f'Expecting DictForArgError.\nGot result: {res!r}.\n')
7270

7371
def testPyDictForArgs(self):
7472
cases = '''\

webware/MiscUtils/Tests/TestNamedValueAccess.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ def testBasicAccess(self):
9191
# pylint: disable=assignment-from-no-return,no-member
9292
for obj in self.objs:
9393
value = func(obj, 'foo')
94-
self.assertEqual(value, 1, 'value = %r, obj = %r' % (value, obj))
94+
self.assertEqual(value, 1, f'value = {value!r}, obj = {obj!r}')
9595
self.assertRaises(NamedValueAccessError, func, obj, 'bar')
9696
value = func(obj, 'bar', 2)
97-
self.assertEqual(value, 2, 'value = %r, obj = %r' % (value, obj))
97+
self.assertEqual(value, 2, f'value = {value!r}, obj = {obj!r}')
9898

9999
def testBasicAccessRepeated(self):
100100
"""Just repeat checkBasicAccess multiple times to check stability."""

webware/WebUtils/Funcs.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ def htmlForDict(d, addSpace=None, filterValueCallBack=None,
8080
if isinstance(topHeading, tuple):
8181
append('<th>{}</th><th>{}</th>'.format(*topHeading))
8282
else:
83-
append('<th colspan="2">{}</th>'.format(topHeading))
83+
append(f'<th colspan="2">{topHeading}</th>')
8484
append('</tr>\n')
8585
for key in sorted(d):
8686
value = d[key]

webware/WebUtils/HTTPStatusCodes.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,11 @@ def htmlTableOfHTTPStatusCodes(
6262

6363

6464
if __name__ == '__main__':
65-
print('''<html>
65+
print(f'''<html>
6666
<head>
6767
<title>HTTP Status Codes</title>
6868
</head>
6969
<body>
70-
{}
70+
{htmlTableOfHTTPStatusCodes()}
7171
</body>
72-
</html>'''.format(htmlTableOfHTTPStatusCodes()))
72+
</html>''')

0 commit comments

Comments
 (0)