Skip to content

Instantly share code, notes, and snippets.

@imontesino
Created June 1, 2023 09:10
Show Gist options
  • Save imontesino/dabbe47900456dd3a97008e46cbe5e2b to your computer and use it in GitHub Desktop.
Save imontesino/dabbe47900456dd3a97008e46cbe5e2b to your computer and use it in GitHub Desktop.
Automate adding a dot to the end of one line descriptions in python docstrings.
""" Tool to add missing dot at the en of a docstring. """
import logging
import subprocess
import argparse
# parse arguments
parser = argparse.ArgumentParser(description='Add missing dot at the end of a docstring.')
parser.add_argument('path', type=str,
help='path to the folder containing the files to be checked')
args = parser.parse_args()
code_path = args.path
ret = subprocess.run(
['python3', '-m', 'pydocstyle', '--select=D415', code_path],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
)
std_out = ret.stdout.decode('utf-8')
for line in std_out.splitlines():
if line.startswith(code_path):
print()
file, line_number = line.split('.py:')
file = file + '.py'
line_number = int(line_number.split(' ')[0])
print(f"{file}: {line_number}")
with open(file, 'r') as f:
lines = f.readlines()
if lines[line_number - 1].endswith('"""\n'):
print("Triple quote ",lines[line_number - 1].strip())
line = lines[line_number - 1].replace('"""\n', '. """\n')
elif lines[line_number - 1].endswith(' \n'):
print("empty space ",lines[line_number - 1].strip())
line = lines[line_number - 1].replace(' \n', '.\n')
elif lines[line_number - 1].endswith(',\n'):
logging.warning("Comma ",lines[line_number - 1].strip())
continue
else:
print("No space ",lines[line_number - 1].strip())
line = lines[line_number - 1].replace('\n', '.\n')
lines[line_number - 1] = line
with open(file, 'w') as f:
f.writelines(lines)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment