arranges

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.

__eq__

def __eq__(other: Any) -> bool

Compare the two lists based on their string representations

__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

__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

__get_pydantic_core_schema__

@classmethod
def __get_pydantic_core_schema__(cls, source_type: Any,
                                 handler: GetCoreSchemaHandler) -> CoreSchema

For automatic validation in pydantic

arranges.segment

start_stop_to_str

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

Returns a string representation of a range 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 range.

__new__

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

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

__or__

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

Return the union of this range and the other range

__iter__

def __iter__()

Iterate over the values in this range

__len__

def __len__() -> int

Get the length of this range

__bool__

def __bool__() -> bool

True if this range has a length

last

@property
def last() -> int

Gets the last value in this range. Will return inf if the range 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 ranges

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 range, and 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.

__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.

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