From 60aa1c661f67329c2ce3b848af63620dbf13636a Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Thu, 12 Oct 2023 14:54:16 +0000 Subject: [PATCH] 'Refactored by Sourcery' --- common.py | 22 ++++++---------------- linkcheck.py | 10 ++++------ make-html.py | 32 +++++++++++++------------------- update-readmes.py | 2 +- 4 files changed, 24 insertions(+), 42 deletions(-) diff --git a/common.py b/common.py index 7180739..47cde1f 100644 --- a/common.py +++ b/common.py @@ -91,9 +91,7 @@ def header_link(title): for character in title: if character in string.whitespace: result += '-' - elif character in string.punctuation: - pass - else: + elif character not in string.punctuation: result += character.lower() return result @@ -104,13 +102,7 @@ def askyesno(question, default=True): The default answer is yes if default is True and no if default is False. """ - if default: - # yes by default - question += ' [Y/n] ' - else: - # no by default - question += ' [y/N] ' - + question += ' [Y/n] ' if default else ' [y/N] ' while True: result = input(question).upper().strip() if result == 'Y': @@ -125,15 +117,15 @@ def askyesno(question, default=True): @contextlib.contextmanager def backup(filename): """A context manager that backs up a file.""" - shutil.copy(filename, filename + '.backup') + shutil.copy(filename, f'{filename}.backup') try: yield except Exception: # It failed, we need to restore from the backup. - shutil.copy(filename + '.backup', filename) + shutil.copy(f'{filename}.backup', filename) else: # Everything's fine, we can safely get rid of the backup. - os.remove(filename + '.backup') + os.remove(f'{filename}.backup') def header_link(title): @@ -149,8 +141,6 @@ def header_link(title): for character in title: if character in string.whitespace: result += '-' - elif character in string.punctuation: - pass - else: + elif character not in string.punctuation: result += character.lower() return result diff --git a/linkcheck.py b/linkcheck.py index 2dcd4c5..ddc3a97 100755 --- a/linkcheck.py +++ b/linkcheck.py @@ -67,13 +67,11 @@ def check(this_file, target, title, titledict): # A directory. if not os.path.isdir(path): return "not a directory" - else: - # A file. - if not os.path.isfile(path): - return "not a file" + elif not os.path.isfile(path): + return "not a file" if title is not None and title not in titledict[path]: - return "no title named %s" % title + return f"no title named {title}" return "ok" @@ -145,7 +143,7 @@ def main(): status = check(filename, target, title, titledict) if status != "ok": print(" file %s, line %d: %s" % (filename, lineno, status)) - print(" %s" % get_line(filename, lineno)) + print(f" {get_line(filename, lineno)}") broken += 1 total += 1 diff --git a/make-html.py b/make-html.py index b40ec3a..349f49b 100755 --- a/make-html.py +++ b/make-html.py @@ -28,6 +28,7 @@ """Create HTML files of the tutorial.""" + import argparse import os import platform @@ -37,11 +38,7 @@ import textwrap import webbrowser -if platform.system() == 'Windows': - python = 'py' -else: - python = 'python3' - +python = 'py' if platform.system() == 'Windows' else 'python3' try: import mistune except ImportError: @@ -49,7 +46,7 @@ print("You can install it by running this command on a terminal or ") print("command prompt:") print() - print(" %s -m pip install mistune" % python) + print(f" {python} -m pip install mistune") sys.exit(1) try: @@ -108,7 +105,7 @@ def fix_filename(filename): # some/place/BEFORE -> some/place/AFTER return filename[:-len(before)] + after if filename.endswith('.md'): - filename = filename[:-3] + '.html' + filename = f'{filename[:-3]}.html' return filename @@ -137,7 +134,7 @@ def link(self, link, title, text): elif '#' in link: # it's like "some-file#title", we need to fix some-file before, after = link.split('#', 1) - link = fix_filename(before) + '#' + after + link = f'{fix_filename(before)}#{after}' else: # it's like "some-file" link = fix_filename(link) @@ -164,13 +161,11 @@ def block_code(self, code, lang=None): continue if line.startswith('+'): - result.append('
%s
' - % line.strip('+')) + result.append(f"""{line.strip('+')}
""") elif line.startswith('-'): - result.append('%s
' - % line.strip('-')) + result.append(f"""{line.strip('-')}
""") else: - result.append('%s
' % line) + result.append(f'{line}
') return '\n'.join(result) @@ -191,9 +186,7 @@ def table(self, header, body): def wrap_text(text): """Like textwrap.fill, but respects newlines.""" - result = [] - for part in text.split('\n'): - result.append(textwrap.fill(part)) + result = [textwrap.fill(part) for part in text.split('\n')] return '\n'.join(result) @@ -226,7 +219,7 @@ def main(): if pygments is None: print("Pygments isn't installed. You can install it like this:") print() - print(" %s -m pip install pygments" % python) + print(f" {python} -m pip install pygments") print() print("You can also continue without Pygments, but the code examples") print("will not be colored.") @@ -236,8 +229,9 @@ def main(): args.pygments_style = None if os.path.exists(args.outdir): - if not common.askyesno("%s exists. Do you want to remove it?" - % args.outdir): + if not common.askyesno( + f"{args.outdir} exists. Do you want to remove it?" + ): print("Interrupt.") return if os.path.isdir(args.outdir): diff --git a/update-readmes.py b/update-readmes.py index 1934dbb..363f8b3 100755 --- a/update-readmes.py +++ b/update-readmes.py @@ -102,7 +102,7 @@ def main(): # the links that point to the subdir must now point to the # current directory, so we fix that - content = BEGINNING + content.replace(directory + '/', '').rstrip() + content = BEGINNING + content.replace(f'{directory}/', '').rstrip() path = os.path.join(directory, 'README.md') this_changed = update_file(path, content) something_changed = something_changed or this_changed