summaryrefslogtreecommitdiff
path: root/coverage/misc.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2009-03-12 08:01:07 -0400
committerNed Batchelder <ned@nedbatchelder.com>2009-03-12 08:01:07 -0400
commit97acd0683f04102041818d79388e2535ac00f0c7 (patch)
tree590f04fea917fdfc5c955d4dbc7e72bdd3cdd342 /coverage/misc.py
parenteb90554e67ca7dc4f170244e67740265b9ac3218 (diff)
downloadpython-coveragepy-git-97acd0683f04102041818d79388e2535ac00f0c7.tar.gz
Move format_lines into misc.py since it doesn't need to be a method of coverage.
Diffstat (limited to 'coverage/misc.py')
-rw-r--r--coverage/misc.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/coverage/misc.py b/coverage/misc.py
index 15ddad08..a6d9f20b 100644
--- a/coverage/misc.py
+++ b/coverage/misc.py
@@ -14,5 +14,37 @@ def nice_pair(pair):
return "%d-%d" % (start, end)
+def format_lines(statements, lines):
+ """Nicely format a list of line numbers.
+
+ Format a list of line numbers for printing by coalescing groups of lines as
+ long as the lines represent consecutive statements. This will coalesce
+ even if there are gaps between statements.
+
+ For example, if `statements` is [1,2,3,4,5,10,11,12,13,14] and
+ `lines` is [1,2,5,10,11,13,14] then the result will be "1-2, 5-11, 13-14".
+
+ """
+ pairs = []
+ i = 0
+ j = 0
+ start = None
+ pairs = []
+ while i < len(statements) and j < len(lines):
+ if statements[i] == lines[j]:
+ if start == None:
+ start = lines[j]
+ end = lines[j]
+ j = j + 1
+ elif start:
+ pairs.append((start, end))
+ start = None
+ i = i + 1
+ if start:
+ pairs.append((start, end))
+ ret = ', '.join(map(nice_pair, pairs))
+ return ret
+
+
class CoverageException(Exception):
pass