Intervals
The `intervals` library (version 0.9.2) provides basic tools for handling intervals or ranges of comparable objects in Python. It supports defining closed, open, or half-open intervals and performing operations such as checking for containment. This specific package, maintained by kvesteri, appears to be abandoned, with no updates since 2016, and its functionality has largely been superseded by other, more actively maintained interval libraries like `portion` (which itself evolved from a library called `python-intervals`).
Common errors
-
TypeError: 'Interval' object is not iterable
cause Attempting to directly iterate over an `Interval` object as if it were a sequence of numbers (e.g., `for x in my_interval:`). The `Interval` object represents a range, not a collection of discrete values for iteration.fixIf you need to iterate over discrete values within an interval, you must explicitly generate them (e.g., using `range()` for integer intervals) or convert the interval to a list of its atomic components if it's an `IntervalSet` (though this library's `Interval` class is not a set of points). For `portion` (a recommended alternative), you can use `P.open(1, 5).iterate(int)`. -
ValueError: Invalid interval: upper bound must be greater than or equal to lower bound if both bounds are open.
cause This error occurs when trying to create an interval like `(5, 5)`, where both bounds are open and equal, which implies an empty interval that cannot contain any number, and is considered an invalid construction by this library.fixTo represent an empty interval with equal bounds, ensure at least one bound is closed, e.g., `Interval.closedopen(5, 5)` or `Interval.openclosed(5, 5)` will correctly yield an empty interval. For a singleton, use `Interval.closed(5, 5)`. -
AttributeError: 'Interval' object has no attribute 'overlaps'
cause This specific error indicates that the `intervals` library (kvesteri's) in version 0.9.x might not provide a direct `overlaps` method on the `Interval` object itself, unlike some other interval libraries (e.g., `portion` or `pyinterval`). Users often expect such methods based on experience with other libraries or common interval logic.fixTo check for overlap with this library, you would typically need to implement the logic manually by comparing the bounds of the two intervals (e.g., `max(interval1.lower, interval2.lower) < min(interval1.upper, interval2.upper)` considering open/closed bounds). Alternatively, consider migrating to a more feature-rich library like `portion` which provides a dedicated `overlaps()` method.
Warnings
- breaking This library (`intervals` by kvesteri, version 0.9.2) is unmaintained and has not seen updates since 2016. It is highly recommended to use a currently maintained alternative, such as `portion` (which is a successor to a related library `python-intervals`), `pyinterval`, or `intervaltree` for better support, features, and Python version compatibility.
- gotcha Attempting to create a fully open, degenerate interval (e.g., `(a..a)`) will result in a `ValueError`. Both endpoints may be equal only if at least one of the bounds is closed (e.g., `[a..a]`, `[a..a)`, `(a..a]`).
- gotcha The `Interval()` constructor acts as a factory, attempting to guess the best matching interval subtype (e.g., `IntInterval`, `DateInterval`) based on the provided bounds. While convenient, this auto-detection can sometimes be slower or lead to unexpected types. For performance and clarity, directly instantiating specific interval types (e.g., `IntInterval(1, 5)`) is recommended if the type is known.
Install
-
pip install intervals
Imports
- Interval
import intervals.Interval
from intervals import Interval
Quickstart
from intervals import Interval, IntInterval
# Create an interval using the factory method
i1 = Interval.closed(1, 5) # Represents [1, 5]
print(f"Interval i1: {i1}")
# Create a half-open interval
i2 = Interval.closedopen(5, 10) # Represents [5, 10)
print(f"Interval i2: {i2}")
# Create a specific type of interval (e.g., integer interval)
i3 = IntInterval(3, 7) # Represents [3, 7]
print(f"Interval i3: {i3}")
# Check for containment
print(f"4 in i1: {4 in i1}") # Expected: True
print(f"5 in i2: {5 in i2}") # Expected: True
print(f"10 in i2: {10 in i2}") # Expected: False
# Check for overlap (note: older versions might not have a direct 'overlaps' method, requiring manual check or utility functions)
# For simple overlap, you might compare bounds manually or use set operations if applicable
# Or, rely on a library like 'portion' for richer operations.