Skip to content

Commit a3483cf

Browse files
committed
Python improvements in utils/80+-check
* The original version had an off-by-one error in the line number (assuming you use the GitHub model of starting lines at 1). Fix this error, and move to using `enumerate()` instead of tracking the `count` variable ourselves. * Add a comment about the exit codes to the help message. * Increase clarity in variable/argument names.
1 parent 5dffe97 commit a3483cf

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

utils/80+-check

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ def get_arguments():
1616
stripped. The reason why we print out the line with whitespace stripped is
1717
that in the case where there is a lot of redundant whitespace, the output
1818
becomes hard to read and no value is provided in terms of finding the line
19-
in question."""))
19+
in question.
20+
21+
Exits with 1 if it finds any violating lines, 0 otherwise."""))
2022

2123
parser.add_argument('infile', nargs='?', type=argparse.FileType('r'),
2224
default=sys.stdin,
2325
help=textwrap.dedent("""The file to read input from. If
2426
no file is provided, stdin is used."""))
25-
parser.add_argument('-o', type=argparse.FileType('w'),
27+
parser.add_argument('-o', '--output', type=argparse.FileType('w'),
2628
default=sys.stdout,
2729
help=textwrap.dedent("""The file to print to. If no
2830
file is provided, stdout is used"""),
@@ -37,17 +39,15 @@ def get_arguments():
3739

3840
args = get_arguments()
3941

40-
count = 0
4142
found_violation = False
4243

43-
for l in args.infile:
44+
for lineno, line in enumerate(args.infile, start=1):
4445
# sys.stdin.readlines() includes a newline. So we subtract 1 from our
4546
# length to get the "true" line length.
46-
length = len(l) - int(args.count_newline)
47+
length = len(line) - int(args.count_newline)
4748
if length > args.max_length:
4849
found_violation = True
49-
print("line: {}. length: {}: {}".format(count, length, l.strip()),
50+
print("line: {}. length: {}: {}".format(lineno, length, line.strip()),
5051
file=args.outfile)
51-
count += 1
5252

5353
sys.exit(found_violation)

0 commit comments

Comments
 (0)