arranges

arranges.segment

start_stop_to_str

def start_stop_to_str(start: range_idx, stop: range_idx) -> str

Returns a string representation of a segment from start to stop.

Segment Objects

class Segment(str)

A single range segment that’s a string and can be hashed.

__init__

def __init__(start: range_idx, stop: range_idx = None)

Construct a new string with the canonical form of the segment.

__new__

def __new__(cls, start: range_idx, stop: range_idx = None) -> str

Construct a new string with the canonical form of the segment.

__or__

def __or__(other: "Segment") -> "Segment"

Return the union of this segment and the other one

__iter__

def __iter__()

Iterate over the values in this segment

__len__

def __len__() -> int

Get the length of this segment

__bool__

def __bool__() -> bool

True if we have a length

last

@property
def last() -> int

Gets the last value in this range. Will return inf if the segment has no end, and -1 if it has no contents,

from_str

@classmethod
@lru_cache
def from_str(cls, value: str) -> "Segment"

Construct from a string.

sort_key

@staticmethod
def sort_key(value: "Segment") -> tuple[int, int]

Sort key function for sorting range segments

isdisjoint

def isdisjoint(other: Any) -> bool

Return True if this range is disjoint from the other range

__eq__

def __eq__(other: Any) -> bool

Compare two segments

isconnected

def isconnected(other: "Segment") -> bool

True if this range is adjacent to or overlaps the other segment, so they can be joined together.

isadjacent

def isadjacent(other: "Segment") -> bool

True if this range is adjacent to the other range

intersects

def intersects(other: "Segment") -> bool

True if this range intersects the other range.

__lt__

def __lt__(other: "Segment") -> bool

Compare segments by (start, stop) tuple

__le__

def __le__(other: "Segment") -> bool

Compare segments by (start, stop) tuple

__gt__

def __gt__(other: "Segment") -> bool

Compare segments by (start, stop) tuple

__ge__

def __ge__(other: "Segment") -> bool

Compare segments by (start, stop) tuple

__contains__

def __contains__(other: Any) -> bool

Membership test. Supports integers, strings, ranges and iterables.

arranges.utils

Some quacky type helpers so we don’t have to use isinstance everywhere

_Boundless Objects

class _Boundless(float)

A class that represents a boundless end of a range

huge

An enormous number that’s used as a stand-in for infinity

__eq__

def __eq__(other) -> bool

Is this boundless?

__index__

def __index__() -> int

When used as an index, return a huge integer rather than infinity.

This is necessary because CPython doesn’t allow lengths larger than sys.maxsize, and Python has no way to represent infinity as an integer.

__hash__

def __hash__() -> int

Make this hashable so it can be used in sets

__sub__

def __sub__(other)

Subtraction that preserves _Boundless type

__rsub__

def __rsub__(other)

Right subtraction

__add__

def __add__(other)

Addition that preserves _Boundless type

__radd__

def __radd__(other)

Right addition

inf

A boundless end of a range. When used as a stop value it’s infinite, but when used as a length it’s the largest index integer possible in cpython.

to_int

def to_int(value: str, default: int) -> int

Convert a string to an integer. If the string is empty, return the default

is_rangelike

def is_rangelike(obj: Any) -> bool

Check if a value is a range-like object

is_intlike

def is_intlike(value: Any) -> bool

Can this object be converted to an integer?

is_iterable

def is_iterable(value: Any) -> bool

Is this object iterable?

as_type

def as_type(cls: Type[T], value: Any) -> T

Convert a value to a type, if necessary.

Saves a bit of construction time if the value is already the right type.

try_hash

def try_hash(obj: Any) -> int | None

Try to hash an object. If it can’t be hashed, return None

force_hash

def force_hash(value)

Force a hash for any value, using str() fallback for unhashable types

as_key

def as_key(k)

Convert a key to a Ranges object.

A lot of the time we’ll have N when we don’t mean “:N” So this converts keys to ranges, but not int-like objects.

arranges.dict

Dict Objects

class Dict()

Range-to-value mapping using slice syntax.

Stores mappings from ranges to values efficiently without storing individual positions. Supports slice syntax: d[100:200] = “value”

__setitem__

def __setitem__(key, value)

Set a range to a value: d[100:200] = “highlight”

__getitem__

def __getitem__(key)

Get value for a range or position: d[150] or d[100:200]

__contains__

def __contains__(key)

Check if a position/range is covered: 150 in d

__delitem__

def __delitem__(key)

Delete a key and update ranges

clear

def clear()

Clear all items and update ranges

pop

def pop(key, *args)

Pop a key and update ranges

popitem

def popitem()

Pop an item and update ranges

update

def update(*args, **kwargs)

Update dict and ranges

setdefault

def setdefault(key, default=None)

Set default and update ranges if key was added

get

def get(key, default=None)

Get value with default, converting key

__or__

def __or__(other)
Union operator ( ): return new Dict with combined contents

__ror__

def __ror__(other)
Reverse union operator: other self

__ior__

def __ior__(other)
Inplace union operator ( =): update self with other

copy

def copy()

Return a copy of this Dict

keys

def keys()

Return view of range keys

values

def values()

Return view of values

items

def items()

Return view of (range_key, value) pairs

__len__

def __len__()

Return number of stored ranges

__bool__

def __bool__()

Return True if not empty

__iter__

def __iter__()

Iterate over range keys

__reversed__

def __reversed__()

Iterate over range keys in reverse order

__eq__

def __eq__(other)

Check equality with another dict

__repr__

def __repr__()

String representation

ranges

@property
def ranges()

Union of all stored ranges (for compatibility)

arranges.ranges

Ranges Objects

class Ranges(str)

A range set that can be hashed and converted to a string.

__init__

def __init__(value: Any, stop: range_idx | None = None)

Construct a new string with the canonical form of the range.

__new__

def __new__(cls, value: Any, stop: range_idx | None = None) -> str

Construct a new string with the canonical form of the range.

This becomes “self” in init, so we’re always a string

construct_str

@classmethod
def construct_str(cls, value, stop) -> str

Create a string representation of a range series

from_str

@classmethod
def from_str(cls, value: str) -> tuple[Segment]

Construct from a string.

iterable_to_str

@classmethod
def iterable_to_str(cls, iterable: Iterable) -> str

Convert an iterable of ranges to a string

from_hashable_iterable

@classmethod
@lru_cache
def from_hashable_iterable(cls, value: tuple[Any]) -> tuple[Segment]

Cache the result of from_iterable

from_iterable

@classmethod
def from_iterable(cls, iterable: Iterable) -> tuple[Segment]

Sort and merge a list of ranges.

__hash__

def __hash__()

The hash of the string (which is what these things are)

__len__

def __len__() -> int

Get the total length of all ranges

__bool__

def __bool__() -> bool

True if this range has any elements

__eq__

def __eq__(other: Any) -> bool

Compare the two lists based on their string representations

__getitem__

def __getitem__(key)

Support slicing and indexing of ranges.

__contains__

def __contains__(other: Any) -> bool

Are all of the other ranges in our ranges?

__iter__

def __iter__()

Iterate over the values in our ranges.

Note that this could be boundless.

intersects

def intersects(other: Any) -> bool

True if this range overlaps with the other range

union

def union(other) -> "Ranges"

Return the union of this range and the other

__or__

def __or__(other: "Ranges") -> "Ranges"

Return the union of this range and the other

__and__

def __and__(other: "Ranges") -> "Ranges"

Return the intersection of this range and the other

__le__

def __le__(other: "Ranges") -> bool

Subset operator (<=): True if self is a subset of other

__lt__

def __lt__(other: "Ranges") -> bool

Proper subset operator (<): True if self is a proper subset of other

__ge__

def __ge__(other: "Ranges") -> bool

Superset operator (>=): True if self is a superset of other

__gt__

def __gt__(other: "Ranges") -> bool

Proper superset operator (>): True if self is a proper superset of other

__sub__

def __sub__(other: "Ranges") -> "Ranges"

Relative complement operator (-): Return elements in self that are not in other

__invert__

def __invert__()

The inverse of this range

validate

@classmethod
def validate(cls, value: Any) -> "Ranges"

Validate a value and convert it to a Range

first

@property
def first()

The start value of the first segment. Called “first” rather than “start” so that Ranges are not “range-like” things.

last

@property
def last()

The last value of the final segment. Exposing “last” rather than “stop” so that Ranges are not “range-like” things.