Skip to content

Commit 60aa1c6

Browse files
author
Sourcery AI
committed
'Refactored by Sourcery'
1 parent e112702 commit 60aa1c6

File tree

4 files changed

+24
-42
lines changed

4 files changed

+24
-42
lines changed

common.py

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ def header_link(title):
9191
for character in title:
9292
if character in string.whitespace:
9393
result += '-'
94-
elif character in string.punctuation:
95-
pass
96-
else:
94+
elif character not in string.punctuation:
9795
result += character.lower()
9896
return result
9997

@@ -104,13 +102,7 @@ def askyesno(question, default=True):
104102
The default answer is yes if default is True and no if default is
105103
False.
106104
"""
107-
if default:
108-
# yes by default
109-
question += ' [Y/n] '
110-
else:
111-
# no by default
112-
question += ' [y/N] '
113-
105+
question += ' [Y/n] ' if default else ' [y/N] '
114106
while True:
115107
result = input(question).upper().strip()
116108
if result == 'Y':
@@ -125,15 +117,15 @@ def askyesno(question, default=True):
125117
@contextlib.contextmanager
126118
def backup(filename):
127119
"""A context manager that backs up a file."""
128-
shutil.copy(filename, filename + '.backup')
120+
shutil.copy(filename, f'{filename}.backup')
129121
try:
130122
yield
131123
except Exception:
132124
# It failed, we need to restore from the backup.
133-
shutil.copy(filename + '.backup', filename)
125+
shutil.copy(f'{filename}.backup', filename)
134126
else:
135127
# Everything's fine, we can safely get rid of the backup.
136-
os.remove(filename + '.backup')
128+
os.remove(f'{filename}.backup')
137129

138130

139131
def header_link(title):
@@ -149,8 +141,6 @@ def header_link(title):
149141
for character in title:
150142
if character in string.whitespace:
151143
result += '-'
152-
elif character in string.punctuation:
153-
pass
154-
else:
144+
elif character not in string.punctuation:
155145
result += character.lower()
156146
return result

linkcheck.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,13 +67,11 @@ def check(this_file, target, title, titledict):
6767
# A directory.
6868
if not os.path.isdir(path):
6969
return "not a directory"
70-
else:
71-
# A file.
72-
if not os.path.isfile(path):
73-
return "not a file"
70+
elif not os.path.isfile(path):
71+
return "not a file"
7472

7573
if title is not None and title not in titledict[path]:
76-
return "no title named %s" % title
74+
return f"no title named {title}"
7775
return "ok"
7876

7977

@@ -145,7 +143,7 @@ def main():
145143
status = check(filename, target, title, titledict)
146144
if status != "ok":
147145
print(" file %s, line %d: %s" % (filename, lineno, status))
148-
print(" %s" % get_line(filename, lineno))
146+
print(f" {get_line(filename, lineno)}")
149147
broken += 1
150148
total += 1
151149

make-html.py

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828

2929
"""Create HTML files of the tutorial."""
3030

31+
3132
import argparse
3233
import os
3334
import platform
@@ -37,19 +38,15 @@
3738
import textwrap
3839
import webbrowser
3940

40-
if platform.system() == 'Windows':
41-
python = 'py'
42-
else:
43-
python = 'python3'
44-
41+
python = 'py' if platform.system() == 'Windows' else 'python3'
4542
try:
4643
import mistune
4744
except ImportError:
4845
print("mistune isn't installed.", file=sys.stderr)
4946
print("You can install it by running this command on a terminal or ")
5047
print("command prompt:")
5148
print()
52-
print(" %s -m pip install mistune" % python)
49+
print(f" {python} -m pip install mistune")
5350
sys.exit(1)
5451

5552
try:
@@ -108,7 +105,7 @@ def fix_filename(filename):
108105
# some/place/BEFORE -> some/place/AFTER
109106
return filename[:-len(before)] + after
110107
if filename.endswith('.md'):
111-
filename = filename[:-3] + '.html'
108+
filename = f'{filename[:-3]}.html'
112109
return filename
113110

114111

@@ -137,7 +134,7 @@ def link(self, link, title, text):
137134
elif '#' in link:
138135
# it's like "some-file#title", we need to fix some-file
139136
before, after = link.split('#', 1)
140-
link = fix_filename(before) + '#' + after
137+
link = f'{fix_filename(before)}#{after}'
141138
else:
142139
# it's like "some-file"
143140
link = fix_filename(link)
@@ -164,13 +161,11 @@ def block_code(self, code, lang=None):
164161
continue
165162

166163
if line.startswith('+'):
167-
result.append('<p><font color="green">%s</font></p>'
168-
% line.strip('+'))
164+
result.append(f"""<p><font color="green">{line.strip('+')}</font></p>""")
169165
elif line.startswith('-'):
170-
result.append('<p><font color="red">%s</font></p>'
171-
% line.strip('-'))
166+
result.append(f"""<p><font color="red">{line.strip('-')}</font></p>""")
172167
else:
173-
result.append('<p>%s</p>' % line)
168+
result.append(f'<p>{line}</p>')
174169

175170
return '\n'.join(result)
176171

@@ -191,9 +186,7 @@ def table(self, header, body):
191186

192187
def wrap_text(text):
193188
"""Like textwrap.fill, but respects newlines."""
194-
result = []
195-
for part in text.split('\n'):
196-
result.append(textwrap.fill(part))
189+
result = [textwrap.fill(part) for part in text.split('\n')]
197190
return '\n'.join(result)
198191

199192

@@ -226,7 +219,7 @@ def main():
226219
if pygments is None:
227220
print("Pygments isn't installed. You can install it like this:")
228221
print()
229-
print(" %s -m pip install pygments" % python)
222+
print(f" {python} -m pip install pygments")
230223
print()
231224
print("You can also continue without Pygments, but the code examples")
232225
print("will not be colored.")
@@ -236,8 +229,9 @@ def main():
236229
args.pygments_style = None
237230

238231
if os.path.exists(args.outdir):
239-
if not common.askyesno("%s exists. Do you want to remove it?"
240-
% args.outdir):
232+
if not common.askyesno(
233+
f"{args.outdir} exists. Do you want to remove it?"
234+
):
241235
print("Interrupt.")
242236
return
243237
if os.path.isdir(args.outdir):

update-readmes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ def main():
102102

103103
# the links that point to the subdir must now point to the
104104
# current directory, so we fix that
105-
content = BEGINNING + content.replace(directory + '/', '').rstrip()
105+
content = BEGINNING + content.replace(f'{directory}/', '').rstrip()
106106
path = os.path.join(directory, 'README.md')
107107
this_changed = update_file(path, content)
108108
something_changed = something_changed or this_changed

0 commit comments

Comments
 (0)