Represent ranges of integers (such as tokens positions) as a set of integers. A Span is hashable and not meant to be modified once created, like a frozenset. It is equivalent to a sparse closed interval. Originally derived and heavily modified from Whoosh Span.
| 40 | |
| 41 | |
| 42 | class Span(Set): |
| 43 | """ |
| 44 | Represent ranges of integers (such as tokens positions) as a set of integers. |
| 45 | A Span is hashable and not meant to be modified once created, like a frozenset. |
| 46 | It is equivalent to a sparse closed interval. |
| 47 | Originally derived and heavily modified from Whoosh Span. |
| 48 | """ |
| 49 | |
| 50 | def __init__(self, *args): |
| 51 | """ |
| 52 | Create a new Span from a start and end ints or an iterable of ints. |
| 53 | |
| 54 | First form: |
| 55 | Span(start int, end int) : the span is initialized with a range(start, end+1) |
| 56 | |
| 57 | Second form: |
| 58 | Span(iterable of ints) : the span is initialized with the iterable |
| 59 | |
| 60 | Spans are hashable and immutable. |
| 61 | |
| 62 | For example: |
| 63 | >>> s = Span(1) |
| 64 | >>> s.start |
| 65 | 1 |
| 66 | >>> s = Span([1, 2]) |
| 67 | >>> s.start |
| 68 | 1 |
| 69 | >>> s.end |
| 70 | 2 |
| 71 | >>> s |
| 72 | Span(1, 2) |
| 73 | |
| 74 | >>> s = Span(1, 3) |
| 75 | >>> s.start |
| 76 | 1 |
| 77 | >>> s.end |
| 78 | 3 |
| 79 | >>> s |
| 80 | Span(1, 3) |
| 81 | |
| 82 | >>> s = Span([6, 5, 1, 2]) |
| 83 | >>> s.start |
| 84 | 1 |
| 85 | >>> s.end |
| 86 | 6 |
| 87 | >>> s |
| 88 | Span(1, 2)|Span(5, 6) |
| 89 | >>> len(s) |
| 90 | 4 |
| 91 | |
| 92 | >>> Span([5, 6, 7, 8, 9, 10 ,11, 12]) == Span([5, 6, 7, 8, 9, 10 ,11, 12]) |
| 93 | True |
| 94 | >>> hash(Span([5, 6, 7, 8, 9, 10 ,11, 12])) == hash(Span([5, 6, 7, 8, 9, 10 ,11, 12])) |
| 95 | True |
| 96 | >>> hash(Span([5, 6, 7, 8, 9, 10 ,11, 12])) == hash(Span(5, 12)) |
| 97 | True |
| 98 | """ |
| 99 | len_args = len(args) |
no outgoing calls