portion
Python data structure and operations for intervals
What it is and what it does
portion is a Python library for creating and manipulating intervals—single ranges or unions of ranges—with automatic simplification and rich set operations. It handles any comparable objects as bounds (numbers, dates, strings) and supports both finite and infinite intervals with open or closed boundaries. The library provides intersection, union, complement, and difference operations, plus tests for emptiness, overlap, and adjacency.
Typical use cases include scheduling systems, range queries, temporal logic, and mathematical interval arithmetic. It includes a dict-like structure to map intervals to data, discrete iteration over interval values, and import/export to strings and Python built-in types. The library is actively maintained, well-tested, and requires only sortedcontainers as a runtime dependency.
Use it for:
- Scheduling and time-slot management: represent availability windows and find overlaps or gaps.
- Range queries in databases or data structures: efficiently test membership and compute intersections.
- Temporal logic and constraint solving: work with time intervals and their logical combinations.
- Numerical analysis: represent and manipulate domains, ranges, and solution sets.
- Interval mapping: associate data with ranges and query by interval containment.
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
portion provides data structures and operations for working with intervals—including unions, intersections, complements, and containment tests—with automatic simplification and support for any comparable objects.
Yes. portion is production-stable (Development Status 5), actively maintained, has no known vulnerabilities, and imposes minimal install friction. The copyleft license (LGPL-3.0-or-later) is a consideration for proprietary use but poses no barrier for open-source projects. Install it if you need interval arithmetic or range operations.
Install
portion on PyPI
pip
pip install portionuv
uv add portionpoetry
poetry add portionInstalling portion
Before you install
Low friction: pure Python wheel with a single runtime dependency (sortedcontainers). Active maintenance with a release 61 days ago and 523 repository stars.
License in practice
LGPL-3.0-or-later (copyleft): derivative works and distributions must preserve the license and provide source code access; suitable for open-source projects but requires careful review in proprietary contexts.
Quickstart
pip install portion
import portion as P
# Create intervals
interval = P.closed(1, 2)
print(interval) # [1,2]
# Perform operations
result = P.closed(0, 2) & P.closed(1, 3)
print(result) # [1,2]
Requires Python 3.10 or later.
Verify before relying
- Whether discrete interval simplification (e.g., [0,1] | [2,3] → [0,3] for integers) is available via the customization API mentioned in the description.
- Performance characteristics for very large interval sets or deeply nested operations.
- Whether the dict-like interval mapping structure supports all standard dict operations.
Package facts
| License | LGPL-3.0-or-later (copyleft) |
| Python support | supports the current Python release (>=3.10) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 1 — sortedcontainers |
| Maintenance | actively maintained — 61 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 1,697,074/month — #3,643 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: portion-2.6.2-py3-none-any.whl
Keywords: interval, interval-set, range, span
Tags
More Scientific/Engineering packages
NumPy provides an N-dimensional array object…
permissive · top 100 on PyPI
pandaspandas provides fast, flexible data structures…
permissive · top 100 on PyPI
scipyscipy provides numerical algorithms for…
permissive · top 100 on PyPI
scikit-learnscikit-learn provides a comprehensive Python…
permissive · top 1,000 on PyPI
dilldill extends Python's pickle module to…
permissive · top 1,000 on PyPI
multiprocessMultiprocess is an enhanced fork of Python's…
permissive · top 1,000 on PyPI
intervalsProvides Python classes for representing and…
permissive · top 15,000 on PyPI
intervaltreeA self-balancing interval tree data structure…
permissive · top 5,000 on PyPI
nclsNCLS provides a fast interval overlap query…
permissive · top 15,000 on PyPI
igwn-segmentsProvides segment, segmentlist, and…
copyleft · top 15,000 on PyPI
python-rangesProvides Range, RangeSet, and RangeDict data…
permissive · top 15,000 on PyPI
infinityProvides an Infinity class that can be compared…
permissive · top 15,000 on PyPI
multisetProvides a mutable and immutable multiset data…
permissive · top 15,000 on PyPI
timeloopTimeloop runs periodic tasks at fixed intervals…
permissive · top 15,000 on PyPI
skia-pathopsPython bindings for Google Skia's Path Ops…
permissive · top 15,000 on PyPI