summaryrefslogtreecommitdiffstats
path: root/port_for/utils.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 05:38:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-19 05:38:14 +0000
commit9e256557e44c09aed7da1420bbd066d434a10951 (patch)
tree7c040c7b4dc2e6205d6003fabcda874acd551025 /port_for/utils.py
parentInitial commit. (diff)
downloadport-for-9e256557e44c09aed7da1420bbd066d434a10951.tar.xz
port-for-9e256557e44c09aed7da1420bbd066d434a10951.zip
Adding upstream version 0.7.1.upstream/0.7.1
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'port_for/utils.py')
-rw-r--r--port_for/utils.py29
1 files changed, 29 insertions, 0 deletions
diff --git a/port_for/utils.py b/port_for/utils.py
new file mode 100644
index 0000000..361d5c0
--- /dev/null
+++ b/port_for/utils.py
@@ -0,0 +1,29 @@
+# -*- coding: utf-8 -*-
+import itertools
+from typing import Iterable, Iterator, Tuple, Set
+
+
+def ranges_to_set(lst: Iterable[Tuple[int, int]]) -> Set[int]:
+ """
+ Convert a list of ranges to a set of numbers::
+
+ >>> ranges = [(1,3), (5,6)]
+ >>> sorted(list(ranges_to_set(ranges)))
+ [1, 2, 3, 5, 6]
+
+ """
+ return set(itertools.chain(*(range(x[0], x[1] + 1) for x in lst)))
+
+
+def to_ranges(lst: Iterable[int]) -> Iterator[Tuple[int, int]]:
+ """
+ Convert a list of numbers to a list of ranges::
+
+ >>> numbers = [1,2,3,5,6]
+ >>> list(to_ranges(numbers))
+ [(1, 3), (5, 6)]
+
+ """
+ for a, b in itertools.groupby(enumerate(lst), lambda t: t[1] - t[0]):
+ c = list(b)
+ yield c[0][1], c[-1][1]