Skip to content

Selection

elva.widgets.ytextarea.selection

Selection definition.

Classes:

  • Selection

    An extended selection object supporting comparison.

Selection

Bases: Selection

An extended selection object supporting comparison.

The implementation eases comparing to locations and other selections.

Methods:

  • __contains__

    Hook called on the in operator.

  • __gt__

    Hook called on the > operator.

  • __ge__

    Hook called on the >= operator.

  • __lt__

    Hook called on the < operator.

  • __le__

    Hook called on the <= operator.

  • __eq__

    Hook called on the == operator.

  • __ne__

    Hook called on the != operator.

Attributes:

  • start (tuple) –

    The start location of the selection. Not necessarily the top one.

  • end (tuple) –

    The end location of the selection. Not necessarily the bottom one.

  • top (tuple) –

    The minimum of end and start location of the selection.

  • bottom (tuple) –

    The maximum of end and start location of the selection.

start instance-attribute

The start location of the selection. Not necessarily the top one.

end instance-attribute

The end location of the selection. Not necessarily the bottom one.

top property

The minimum of end and start location of the selection.

bottom property

The maximum of end and start location of the selection.

_on_type(obj, on_selection, on_tuple)

Perform defined actions depending on the type of the object to compare to.

Parameters:

  • obj (tuple | Self) –

    the object to compare to.

  • on_selection (Callable[[], S]) –

    the object to call when obj is a Selection.

  • on_tuple (Callable[[], T]) –

    the object to call when obj is an instance of tuple.

Raises:

Returns:

  • S | T

    the return value of either on_selection or on_tuple.

Source code in src/elva/widgets/ytextarea/selection.py
def _on_type(
    self,
    obj: tuple | Self,
    on_selection: Callable[[], S],
    on_tuple: Callable[[], T],
) -> S | T:
    """
    Perform defined actions depending on the type of the object to compare to.

    Arguments:
        obj: the object to compare to.
        on_selection: the object to call when `obj` is a [`Selection`][elva.widgets.ytextarea.Selection].
        on_tuple: the object to call when `obj` is an instance of [`tuple`][tuple].

    Raises:
        TypeError: if `obj` is not an instance of [`tuple`][tuple].

    Returns:
        the return value of either `on_selection` or `on_tuple`.
    """
    if type(obj) is type(self):
        # `obj` is of the *exact* same type as `self`
        return on_selection()
    elif isinstance(obj, tuple):
        # `obj` is a type of or of subtype of `tuple`
        return on_tuple()
    else:
        # something else was passed
        raise TypeError(
            (
                "comparison not supported between instances of "
                f"'{type(self)}' and '{type(obj)}'"
            )
        )

__contains__(obj)

Hook called on the in operator.

Parameters:

  • obj (tuple | Self) –

    the object to compare to.

Raises:

Returns:

  • bool

    True if the tuple or selection is within the top and bottom location of this selection, else False.

Source code in src/elva/widgets/ytextarea/selection.py
def __contains__(self, obj: tuple | Self) -> bool:
    """
    Hook called on the `in` operator.

    Arguments:
        obj: the object to compare to.

    Raises:
        TypeError: if `obj` is not an instance of [`tuple`][tuple].

    Returns:
        `True` if the tuple or selection is within the top and bottom location of this selection, else `False`.
    """
    return self.top <= obj <= self.bottom

__gt__(obj)

Hook called on the > operator.

Parameters:

  • obj (tuple | Self) –

    the object to compare to.

Raises:

Returns:

  • bool

    True if the tuple or selection is before the top location, else False.

Source code in src/elva/widgets/ytextarea/selection.py
def __gt__(self, obj: tuple | Self) -> bool:
    """
    Hook called on the `>` operator.

    Arguments:
        obj: the object to compare to.

    Raises:
        TypeError: if `obj` is not an instance of [`tuple`][tuple].

    Returns:
        `True` if the tuple or selection is before the top location, else `False`.
    """
    return self._on_type(
        obj,
        lambda: obj.bottom < self.top,
        lambda: obj < self.top,
    )

__ge__(obj)

Hook called on the >= operator.

Parameters:

  • obj (tuple | Self) –

    the object to compare to.

Raises:

Returns:

  • bool

    True if the tuple or selection is before or equal to the top location, else False.

Source code in src/elva/widgets/ytextarea/selection.py
def __ge__(self, obj: tuple | Self) -> bool:
    """
    Hook called on the `>=` operator.

    Arguments:
        obj: the object to compare to.

    Raises:
        TypeError: if `obj` is not an instance of [`tuple`][tuple].

    Returns:
        `True` if the tuple or selection is before or equal to the top location, else `False`.
    """
    return self._on_type(
        obj,
        lambda: obj.bottom <= self.top,
        lambda: obj <= self.top,
    )

__lt__(obj)

Hook called on the < operator.

Parameters:

  • obj (tuple | Self) –

    the object to compare to.

Raises:

Returns:

  • bool

    True if the tuple or selection is after the bottom location, else False.

Source code in src/elva/widgets/ytextarea/selection.py
def __lt__(self, obj: tuple | Self) -> bool:
    """
    Hook called on the `<` operator.

    Arguments:
        obj: the object to compare to.

    Raises:
        TypeError: if `obj` is not an instance of [`tuple`][tuple].

    Returns:
        `True` if the tuple or selection is after the bottom location, else `False`.
    """
    return self._on_type(
        obj,
        lambda: self.bottom < obj.top,
        lambda: self.bottom < obj,
    )

__le__(obj)

Hook called on the <= operator.

Parameters:

  • obj (tuple | Self) –

    the object to compare to.

Raises:

Returns:

  • bool

    True if the tuple or selection is after or equal to the bottom location, else False.

Source code in src/elva/widgets/ytextarea/selection.py
def __le__(self, obj: tuple | Self) -> bool:
    """
    Hook called on the `<=` operator.

    Arguments:
        obj: the object to compare to.

    Raises:
        TypeError: if `obj` is not an instance of [`tuple`][tuple].

    Returns:
        `True` if the tuple or selection is after or equal to the bottom location, else `False`.
    """
    return self._on_type(
        obj,
        lambda: self.bottom <= obj.top,
        lambda: self.bottom <= obj,
    )

__eq__(obj)

Hook called on the == operator.

Parameters:

  • obj (tuple | Self) –

    the object to compare to.

Returns:

  • bool

    True if the start and end locations are the same, else False.

Source code in src/elva/widgets/ytextarea/selection.py
def __eq__(self, obj: tuple | Self) -> bool:
    """
    Hook called on the `==` operator.

    Arguments:
        obj: the object to compare to.

    Returns:
        `True` if the start and end locations are the same, else `False`.
    """
    if type(obj) is type(self):
        return obj.start == self.start and obj.end == self.end
    else:
        return False

__ne__(obj)

Hook called on the != operator.

Parameters:

  • obj (Self) –

    the object to compare to.

Returns:

  • bool

    False if start and end locations are the same, else True.

Source code in src/elva/widgets/ytextarea/selection.py
def __ne__(self, obj: Self) -> bool:
    """
    Hook called on the `!=` operator.

    Arguments:
        obj: the object to compare to.

    Returns:
        `False` if start and end locations are the same, else `True`.
    """
    return not self == obj