Skip to content

Commit e9cf4a5

Browse files
committed
-
1 parent ead996e commit e9cf4a5

File tree

1 file changed

+27
-0
lines changed

1 file changed

+27
-0
lines changed

source_py3/python_toolbox/segment_tools.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,19 @@
77

88

99
def crop_segment(segment, base_segment):
10+
'''
11+
Crop `segment` to fit inside `base_segment`.
12+
13+
This means that if it was partially outside of `base_segment`, that portion
14+
would be cut off and you'll get only the intersection of `segment` and
15+
`base_segment`.
16+
17+
Example:
18+
19+
>>> crop_segment((7, 17), (10, 20))
20+
(10, 17)
21+
22+
'''
1023
start, end = segment
1124
base_start, base_end = base_segment
1225
if not (base_start <= start <= base_end or \
@@ -20,6 +33,20 @@ def crop_segment(segment, base_segment):
2033

2134

2235
def merge_segments(segments):
36+
'''
37+
"Clean" a bunch of segments by removing any shared portions.
38+
39+
This function takes an iterable of segments and returns a cleaned one in
40+
which any duplicated portions were removed. Some segments which were
41+
contained in others would be removed completely, while other segments that
42+
touched each other would be merged.
43+
44+
Example:
45+
46+
>>> merge_segments((0, 10), (4, 16), (16, 17), (30, 40))
47+
((0, 17), (30, 40))
48+
49+
'''
2350
assert all(len(segment) == 2 for segment in segments)
2451
sorted_segments = sorted(segments)
2552
if not sorted_segments:

0 commit comments

Comments
 (0)