Reviewers repeat the same comments often. Write a bot that makes five of them.
What to build
A program that applies five review rules to every line of Python code.
Requirements
- Rule 1: a line that, without spaces at the start, begins with
defdefines a function; if its name (the text up to the first() is not snake_case (onlya-z,0-9and_), commentline N: function name NAME should be snake_case. - Rule 2: a
defline whose text, with all spaces removed, contains=[]or={}getsline N: mutable default argument. - Rule 3: a line that, without spaces at the start and end, is exactly
except:getsline N: bare except hides errors. - Rule 4: a line that contains
print(getsline N: print call, use logging. - Rule 5: a line that contains
== Noneor!= Nonegetsline N: compare with None using is. - Report the comments in line order, and for one line in the order of the rules. At the end print
COUNT comment(s).
Input
The first line has n (1 to 100). The next n lines are the code.
Output
One line per comment, then the count.
Constraints
1 ≤ n ≤ 100.
Example
Input:
7
def addItem(items, item=[]):
items.append(item)
try:
print(items)
except:
pass
return items
Output:
line 1: function name addItem should be snake_case
line 1: mutable default argument
line 4: print call, use logging
line 5: bare except hides errors
4 comment(s)
Why it works
Each rule looks at one line only, so the bot goes through the lines and tests all five rules on each; keeping the rule order inside the loop gives the stated order for lines with several problems.
Examples
7
def addItem(items, item=[]):
items.append(item)
try:
print(items)
except:
pass
return items
line 1: function name addItem should be snake_case line 1: mutable default argument line 4: print call, use logging line 5: bare except hides errors 4 comment(s)
3
def total(prices):
return sum(prices)
x = total([1])
0 comment(s)
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
Check the rules in order on every line and print a comment for each one that applies.
Hint 2
For rule 3, remove all spaces from the def line and look for "=[]" or "={}".
Hint 3
Take the function name from the text between "def " and the first "(".
Solution
Show one correct solution (try it yourself first)
n = int(input())
count = 0
for number in range(1, n + 1):
line = input()
text = line.strip()
comments = []
if text.startswith("def "):
name = text[4:].split("(")[0]
if not all(ch in "abcdefghijklmnopqrstuvwxyz0123456789_" for ch in name):
comments.append(f"function name {name} should be snake_case")
squeezed = text.replace(" ", "")
if "=[]" in squeezed or "={}" in squeezed:
comments.append("mutable default argument")
if text == "except:":
comments.append("bare except hides errors")
if "print(" in line:
comments.append("print call, use logging")
if "== None" in line or "!= None" in line:
comments.append("compare with None using is")
for comment in comments:
count += 1
print(f"line {number}: {comment}")
print(f"{count} comment(s)")
▶ Open this solution in the compiler, then press Visualize to watch it run step by step