From a3483cf731933112aa643e7c2a2bdc2803b5a927 Mon Sep 17 00:00:00 2001 From: Alex Chan Date: Sat, 18 Jun 2016 20:15:30 +0100 Subject: [PATCH] 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. --- utils/80+-check | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/utils/80+-check b/utils/80+-check index 1d9371ce373df..3863ae63439f4 100755 --- a/utils/80+-check +++ b/utils/80+-check @@ -16,13 +16,15 @@ def get_arguments(): stripped. The reason why we print out the line with whitespace stripped is that in the case where there is a lot of redundant whitespace, the output becomes hard to read and no value is provided in terms of finding the line - in question.""")) + in question. + + Exits with 1 if it finds any violating lines, 0 otherwise.""")) parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), default=sys.stdin, help=textwrap.dedent("""The file to read input from. If no file is provided, stdin is used.""")) - parser.add_argument('-o', type=argparse.FileType('w'), + parser.add_argument('-o', '--output', type=argparse.FileType('w'), default=sys.stdout, help=textwrap.dedent("""The file to print to. If no file is provided, stdout is used"""), @@ -37,17 +39,15 @@ def get_arguments(): args = get_arguments() -count = 0 found_violation = False -for l in args.infile: +for lineno, line in enumerate(args.infile, start=1): # sys.stdin.readlines() includes a newline. So we subtract 1 from our # length to get the "true" line length. - length = len(l) - int(args.count_newline) + length = len(line) - int(args.count_newline) if length > args.max_length: found_violation = True - print("line: {}. length: {}: {}".format(count, length, l.strip()), + print("line: {}. length: {}: {}".format(lineno, length, line.strip()), file=args.outfile) - count += 1 sys.exit(found_violation)