skillfed

portion

Python data structure and operations for intervals

portion v2.6.2 1.7M downloads/30d#3,643 on PyPI523
Copyleft license LGPL-3.0-or-later Active released

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 portion

uv

uv add portion

poetry

poetry add portion

Installing 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

Development Status :: 5 - Production/StableIntended Audience :: DevelopersIntended Audience :: EducationIntended Audience :: Information TechnologyIntended Audience :: Science/ResearchLicense :: OSI Approved :: GNU Lesser General Public License v3 (LGPLv3)Programming Language :: Python :: 3 :: OnlyProgramming Language :: Python :: 3.10Programming Language :: Python :: 3.11Programming Language :: Python :: 3.12Programming Language :: Python :: 3.13Programming Language :: Python :: 3.14Topic :: Scientific/EngineeringTopic :: Scientific/Engineering :: Mathematics

Tags

interval data structureinterval operations union intersectionrange and span handlinginterval set arithmeticcomparable object intervalsinterval simplificationdiscrete interval iteration
interval-arithmeticrange-operationsset-theory

More Scientific/Engineering packages