summaryrefslogtreecommitdiff
path: root/coverage/misc.py
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2019-06-18 21:50:50 -0400
committerNed Batchelder <ned@nedbatchelder.com>2019-06-19 06:00:35 -0400
commit7a7a93d7abc710f11c93a6f0a4edbea7cb0c47e5 (patch)
treeca8c5f089d8bf59b66ed65d99d96e0e5a4c86a48 /coverage/misc.py
parentf36c3acd2f236cebbac254fc9c71e2080cbfece2 (diff)
downloadpython-coveragepy-git-7a7a93d7abc710f11c93a6f0a4edbea7cb0c47e5.tar.gz
Move arcz_to_arcs to misc so we can use it elsewhere
Diffstat (limited to 'coverage/misc.py')
-rw-r--r--coverage/misc.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/coverage/misc.py b/coverage/misc.py
index d87a7b89..ad7b834f 100644
--- a/coverage/misc.py
+++ b/coverage/misc.py
@@ -281,6 +281,45 @@ def substitute_variables(text, variables=os.environ):
return text
+# Map chars to numbers for arcz_to_arcs
+_arcz_map = {'.': -1}
+_arcz_map.update(dict((c, ord(c) - ord('0')) for c in '123456789'))
+_arcz_map.update(dict(
+ (c, 10 + ord(c) - ord('A')) for c in 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
+))
+
+def arcz_to_arcs(arcz):
+ """Convert a compact textual representation of arcs to a list of pairs.
+
+ The text has space-separated pairs of letters. Period is -1, 1-9 are
+ 1-9, A-Z are 10 through 36. The resulting list is sorted regardless of
+ the order of the input pairs.
+
+ ".1 12 2." --> [(-1,1), (1,2), (2,-1)]
+
+ Minus signs can be included in the pairs:
+
+ "-11, 12, 2-5" --> [(-1,1), (1,2), (2,-5)]
+
+ """
+ arcs = []
+ for pair in arcz.split():
+ asgn = bsgn = 1
+ if len(pair) == 2:
+ a, b = pair
+ else:
+ assert len(pair) == 3
+ if pair[0] == '-':
+ _, a, b = pair
+ asgn = -1
+ else:
+ assert pair[1] == '-'
+ a, _, b = pair
+ bsgn = -1
+ arcs.append((asgn * _arcz_map[a], bsgn * _arcz_map[b]))
+ return sorted(arcs)
+
+
class BaseCoverageException(Exception):
"""The base of all Coverage exceptions."""
pass