From 64c853d8cd9bb6397d7c91cf4571a3a34cc77070 Mon Sep 17 00:00:00 2001 From: Sverre Rabbelier Date: Sun, 20 Jul 2008 23:33:28 +0200 Subject: [PATCH] gitstats: Use format specifiers instead of appending to a string Instead of using '"foo" + str(myvar) + "bar"' it reads a lot more natural to use '"foo %s bar" % myvar', plus it prevents whitespace 'bugs'. --- src/git_stats/author.py | 12 ++++++------ src/git_stats/bug.py | 12 ++++++------ src/git_stats/config.py | 4 ++-- src/git_stats/diff.py | 7 ++++--- src/git_stats/parse.py | 9 +++++---- 5 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/git_stats/author.py b/src/git_stats/author.py index caaacaf..4e0e490 100644 --- a/src/git_stats/author.py +++ b/src/git_stats/author.py @@ -78,7 +78,7 @@ def activityInArea(log): activity.added = int(addpart) activity.deleted = int(deletepart) except ValueError, e: - print("On line '" + str(i) + "', could not convert number: " + str(e)) + print("On line '%d', could not convert number: %s" % (i,str(e))) activity.path = splitline[2][:-1] activities.append(activity) @@ -112,7 +112,7 @@ def activityInArea(log): activities = [] ids = [] else: - print("Cannot parse line " + str(i) + ".") + print("Cannot parse line %d." % i) # Return the result return activityByAuthor @@ -167,7 +167,7 @@ def activity(id, field, startFrom): for key in sorted(activity): value = activity[key] - result.append("\t" + str(value) + " = " + str(key)) + result.append("\t%s = %s" % (str(value), str(key))) result.append("") @@ -184,7 +184,7 @@ def activity(id, field, startFrom): for key in sorted(activity): value = activity[key] - result.append(str(value) + " = " + str(key)) + result.append("%s = %s" % (str(value), str(key))) return result @@ -222,7 +222,7 @@ def aggregateActivity(idFilter, field, startFrom): for key in sorted(aggregatedActivity): value = aggregatedActivity[key] - result.append(str(value) + " = " + str(key)) + result.append("%s = %s" % (str(value), str(key))) return result @@ -308,7 +308,7 @@ def dispatch(*args): result = [] for key, value in activity_for_file.iteritems(): - result.append(key + ": " + str(value)) + result.append("%s: %s" % (key, str(value))) for line in result: print(line) diff --git a/src/git_stats/bug.py b/src/git_stats/bug.py index fd7a47a..15e4bcd 100644 --- a/src/git_stats/bug.py +++ b/src/git_stats/bug.py @@ -78,24 +78,24 @@ class CommitInfo: result = [] for branch in self.branches: - result.append("In branch: " + branch) + result.append("In branch: '%s'." % branch) for commit in self.reverts: - result.append("Reverts commit: " + commit) + result.append("Reverts commit: '%s'." % commit) if self.msg_matches: - result.append("Message matches: '" + self.msg_matches + "'.") + result.append("Message matches: '%s'." % self.msg_matches) if self.diff_matches: - result.append("Diff matches: '" + self.diff_matches + "'.") + result.append("Diff matches: '%s'." % self.diff_matches) if self.isBugfix(): - result.append("Bugfix rating: " + str(self.bugfixRating())) + result.append("Bugfix rating: %d." % self.bugfixRating()) res = self.commit + ":" for line in result: - res += "\n" + line + res = "%s\n%s" % (res,line) return res diff --git a/src/git_stats/config.py b/src/git_stats/config.py index 4e2cfe6..2497d98 100644 --- a/src/git_stats/config.py +++ b/src/git_stats/config.py @@ -176,14 +176,14 @@ def main(args): print("convert_camel_case is ON:") for key, value in result.iteritems(): - print(str(key) + ": " + str(value)) + print("%s: %s" % (key, str(value))) result = readAll(False) print("") print("convertCamelCase is OFF:") for key, value in result.iteritems(): - print(str(key) + ": " + str(value)) + print("%s: %s" % (key, str(value))) if __name__ == '__main__': import sys diff --git a/src/git_stats/diff.py b/src/git_stats/diff.py index b3e35cb..04fc808 100644 --- a/src/git_stats/diff.py +++ b/src/git_stats/diff.py @@ -38,10 +38,11 @@ class FileDiff: self.bfile = line[4:] def __str__(self): - a = "Diff for '" + self.afile + "' (" + str(self.astart) + ") against '" - b = self.bfile + "' (" + str(self.bstart) + ")" + self.context + "." + str = ("Diff for '%s' (%d) against '%s' (%d)\n%s\n%s\n%s" % + (self.afile, self.astart, self.bfile, self.bstart, + self.context, self.linesAdded, self.linesDeleted)) - return a + b + '\n' + str(self.linesAdded) + '\n' + str(self.linesDeleted) + return str def _splitDiff(diff): """Splits off the diff in chunks, one for each file diff --git a/src/git_stats/parse.py b/src/git_stats/parse.py index 1a7d81b..3dce95b 100644 --- a/src/git_stats/parse.py +++ b/src/git_stats/parse.py @@ -25,12 +25,13 @@ def check_file(value, relative=False): file = git.ls_files("--full-name", "--", value, with_keep_cwd=relative) if not file: - raise OptionValueError("Unknown file '" + value + "'") + raise OptionValueError("Unknown file '%s'" % value) splitfile = file.split('\n') if len(splitfile) > 1: - raise OptionValueError("Please specify only one file, '" + value + "' matches more than one file") + raise OptionValueError( + "Please specify only one file, '%s' matches more than one" % value) return splitfile[0] @@ -42,7 +43,7 @@ def _check_commit(option, opt, value): rev = git.rev_parse("--verify", value, with_exceptions=False) if not rev: - raise OptionValueError("Unknown commit '" + value + "'") + raise OptionValueError("Unknown commit '%s'" % value) return rev @@ -61,7 +62,7 @@ def _check_bool(option, opt, value): if value == "None": return None - raise OptionValueError("Not a boolean: '" + value + "'") + raise OptionValueError("Not a boolean: '%s'" % value) class GitOption(Option): """This parser understands three new types: -- 2.11.4.GIT