The set

class RangeSet(*args)

A class to represent a mutable set of Ranges that may or may not overlap. A RangeSet will generally interface cleanly with a Range or another RangeSet. In general, most methods called on a RangeSet will try to coerce its argument to a RangeSet if it isn’t one already (so, a list of Ranges is a valid argument for many methods).

A RangeSet can be constructed from any number of Range-like objects or iterables containing Range-like objects, all given as positional arguments. Any iterables will be flattened by one later before having their contents added to this RangeSet.

Examples of valid RangeSets include:

>>> a = RangeSet()  # empty RangeSet
>>> b = RangeSet([Range(0, 1), Range(2, 3), Range(4, 5)])  # single iterable as a positional argument
>>> c = RangeSet(Range(0, 1), Range(2, 3), Range(4, 5))   # multiple Ranges as positional arguments
>>> d = RangeSet("[0, 1)", ["[1.5, 2)", "[2.5, 3)"], "[4, 5]")  # multiple positional arguments, one is an iterable

Nested iterables are not supported, and will raise a ValueError:

>>> e = RangeSet([[Range(0, 1), Range(2, 3)], [Range(4, 5), Range(6, 7)]])

Internally, Ranges are stored ordered from least to greatest. Overlapping ranges will be combined into a single Range. For example:

>>> f = RangeSet("[0, 3]", "[2, 4)", "[5, 6]")
>>> str(f) == "{[0, 4), [5, 6]}"

All Ranges in a given RangeSet must be comparable to each other, or else errors may occur. The Range type is by default comparable with itself, but this functions by comparing the start and end values - if two Ranges have non-mutually-comparable start/end values, then they cannot be compared, which breaks this data structure’s internal organization. This is an intentional design decision.

RangeSets are hashable, meaning they can be used as keys in dicts.

__init__(*args)

Constructs a new RangeSet containing the given sub-ranges. :param args: For each positional argument, if the argument is Rangelike, it is added to this RangeSet,

or if it is an iterable containing Rangelikes, all contained Rangelikes are added to this RangeSet.