Skip to content

Core

Ordering

Bases: Enum

Represents ordering.

Source code in orderings/core.py
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
class Ordering(Enum):
    """Represents ordering."""

    LESS = -1
    """The left item is *less* than the right item."""

    EQUAL = 0
    """The left item is *equal* to the right item."""

    GREATER = 1
    """The left item is *greater* than the right item."""

    def is_less(self) -> bool:
        """Checks if the ordering is [`LESS`][orderings.core.Ordering.LESS].

        Returns:
            Whether the ordering is [`LESS`][orderings.core.Ordering.LESS].
        """
        return self is type(self).LESS

    def is_equal(self) -> bool:
        """Checks if the ordering is [`EQUAL`][orderings.core.Ordering.EQUAL].

        Returns:
            Whether the ordering is [`EQUAL`][orderings.core.Ordering.EQUAL].
        """
        return self is type(self).EQUAL

    def is_greater(self) -> bool:
        """Checks if the ordering is [`GREATER`][orderings.core.Ordering.GREATER].

        Returns:
            Whether the ordering is [`GREATER`][orderings.core.Ordering.GREATER].
        """
        return self is type(self).GREATER

    def is_less_or_equal(self) -> bool:
        """Checks if the ordering is [`LESS`][orderings.core.Ordering.LESS] or
        [`EQUAL`][orderings.core.Ordering.EQUAL].
        This is equivalent to:

        ```python
        ordering.is_less() or ordering.is_equal()
        ```

        Returns:
            Whether the ordering is [`LESS`][orderings.core.Ordering.LESS]
                or [`EQUAL`][orderings.core.Ordering.EQUAL].
        """
        return self.is_less() or self.is_equal()

    def is_not_equal(self) -> bool:
        """Checks if the ordering is not [`EQUAL`][orderings.core.Ordering.EQUAL].
        This is equivalent to:

        ```python
        not ordering.is_equal()
        ```

        Returns:
            Whether the ordering is not [`EQUAL`][orderings.core.Ordering.EQUAL].
        """
        return not self.is_equal()

    def is_greater_or_equal(self) -> bool:
        """Checks if the ordering is [`GREATER`][orderings.core.Ordering.GREATER] or
        [`EQUAL`][orderings.core.Ordering.EQUAL].
        This is equivalent to:

        ```python
        ordering.is_greater() or ordering.is_equal()
        ```

        Returns:
            Whether the ordering is [`GREATER`][orderings.core.Ordering.GREATER]
                or [`EQUAL`][orderings.core.Ordering.EQUAL].
        """
        return self.is_greater() or self.is_equal()

LESS = -1 class-attribute instance-attribute

The left item is less than the right item.

EQUAL = 0 class-attribute instance-attribute

The left item is equal to the right item.

GREATER = 1 class-attribute instance-attribute

The left item is greater than the right item.

is_less() -> bool

Checks if the ordering is LESS.

Returns:

Type Description
bool

Whether the ordering is LESS.

Source code in orderings/core.py
24
25
26
27
28
29
30
def is_less(self) -> bool:
    """Checks if the ordering is [`LESS`][orderings.core.Ordering.LESS].

    Returns:
        Whether the ordering is [`LESS`][orderings.core.Ordering.LESS].
    """
    return self is type(self).LESS

is_equal() -> bool

Checks if the ordering is EQUAL.

Returns:

Type Description
bool

Whether the ordering is EQUAL.

Source code in orderings/core.py
32
33
34
35
36
37
38
def is_equal(self) -> bool:
    """Checks if the ordering is [`EQUAL`][orderings.core.Ordering.EQUAL].

    Returns:
        Whether the ordering is [`EQUAL`][orderings.core.Ordering.EQUAL].
    """
    return self is type(self).EQUAL

is_greater() -> bool

Checks if the ordering is GREATER.

Returns:

Type Description
bool

Whether the ordering is GREATER.

Source code in orderings/core.py
40
41
42
43
44
45
46
def is_greater(self) -> bool:
    """Checks if the ordering is [`GREATER`][orderings.core.Ordering.GREATER].

    Returns:
        Whether the ordering is [`GREATER`][orderings.core.Ordering.GREATER].
    """
    return self is type(self).GREATER

is_less_or_equal() -> bool

Checks if the ordering is LESS or EQUAL. This is equivalent to:

ordering.is_less() or ordering.is_equal()

Returns:

Type Description
bool

Whether the ordering is LESS or EQUAL.

Source code in orderings/core.py
48
49
50
51
52
53
54
55
56
57
58
59
60
61
def is_less_or_equal(self) -> bool:
    """Checks if the ordering is [`LESS`][orderings.core.Ordering.LESS] or
    [`EQUAL`][orderings.core.Ordering.EQUAL].
    This is equivalent to:

    ```python
    ordering.is_less() or ordering.is_equal()
    ```

    Returns:
        Whether the ordering is [`LESS`][orderings.core.Ordering.LESS]
            or [`EQUAL`][orderings.core.Ordering.EQUAL].
    """
    return self.is_less() or self.is_equal()

is_not_equal() -> bool

Checks if the ordering is not EQUAL. This is equivalent to:

not ordering.is_equal()

Returns:

Type Description
bool

Whether the ordering is not EQUAL.

Source code in orderings/core.py
63
64
65
66
67
68
69
70
71
72
73
74
def is_not_equal(self) -> bool:
    """Checks if the ordering is not [`EQUAL`][orderings.core.Ordering.EQUAL].
    This is equivalent to:

    ```python
    not ordering.is_equal()
    ```

    Returns:
        Whether the ordering is not [`EQUAL`][orderings.core.Ordering.EQUAL].
    """
    return not self.is_equal()

is_greater_or_equal() -> bool

Checks if the ordering is GREATER or EQUAL. This is equivalent to:

ordering.is_greater() or ordering.is_equal()

Returns:

Type Description
bool

Whether the ordering is GREATER or EQUAL.

Source code in orderings/core.py
76
77
78
79
80
81
82
83
84
85
86
87
88
89
def is_greater_or_equal(self) -> bool:
    """Checks if the ordering is [`GREATER`][orderings.core.Ordering.GREATER] or
    [`EQUAL`][orderings.core.Ordering.EQUAL].
    This is equivalent to:

    ```python
    ordering.is_greater() or ordering.is_equal()
    ```

    Returns:
        Whether the ordering is [`GREATER`][orderings.core.Ordering.GREATER]
            or [`EQUAL`][orderings.core.Ordering.EQUAL].
    """
    return self.is_greater() or self.is_equal()

PartialCompare

Bases: PartialOrdered[T], Protocol[T]

Implements partial ordering via delegation to the partial_compare method.

Note

Please note that this protocol does not implement equality methods.

Source code in orderings/core.py
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
@runtime_checkable
class PartialCompare(PartialOrdered[T], Protocol[T]):
    """Implements partial ordering via delegation to the
    [`partial_compare`][orderings.core.PartialCompare.partial_compare] method.

    Note:
        Please note that this protocol does not implement equality methods.
    """

    @required
    def partial_compare(self, value: T) -> Ordering:
        """Defines partial ordering via the [`Ordering`][orderings.core.Ordering] enumeration."""
        ...

    def __lt__(self, value: T) -> bool:
        return self.partial_compare(value).is_less()

    def __gt__(self, value: T) -> bool:
        return self.partial_compare(value).is_greater()

    def __le__(self, value: T) -> bool:
        return self.partial_compare(value).is_less_or_equal()

    def __ge__(self, value: T) -> bool:
        return self.partial_compare(value).is_greater_or_equal()

partial_compare(value: T) -> Ordering

Defines partial ordering via the Ordering enumeration.

Source code in orderings/core.py
104
105
106
107
@required
def partial_compare(self, value: T) -> Ordering:
    """Defines partial ordering via the [`Ordering`][orderings.core.Ordering] enumeration."""
    ...

Compare

Bases: Ordered, Protocol

Implements total ordering via delegation to the compare method.

Source code in orderings/core.py
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
@runtime_checkable
class Compare(Ordered, Protocol):
    """Implements total ordering via delegation to the
    [`compare`][orderings.core.Compare.compare] method.
    """

    @required
    def compare(self, other: Self) -> Ordering:
        """Defines ordering via the [`Ordering`][orderings.core.Ordering] enumeration."""
        ...

    def __lt__(self, other: Self) -> bool:
        return self.compare(other).is_less()

    def __gt__(self, other: Self) -> bool:
        return self.compare(other).is_greater()

    def __le__(self, other: Self) -> bool:
        return self.compare(other).is_less_or_equal()

    def __ge__(self, other: Self) -> bool:
        return self.compare(other).is_greater_or_equal()

    def __eq__(self, other: Any) -> bool:
        return is_same_or_sub_type(other, self) and self.compare(other).is_equal()

    def __ne__(self, other: Any) -> bool:
        return not is_same_or_sub_type(other, self) or self.compare(other).is_not_equal()

compare(other: Self) -> Ordering

Defines ordering via the Ordering enumeration.

Source code in orderings/core.py
128
129
130
131
@required
def compare(self, other: Self) -> Ordering:
    """Defines ordering via the [`Ordering`][orderings.core.Ordering] enumeration."""
    ...

is_compare(item: Any) -> TypeIs[Compare]

Checks if the item implements the Compare protocol.

Parameters:

Name Type Description Default
item Any

The item to check.

required

Returns:

Type Description
TypeIs[Compare]

Whether the item implements the Compare protocol.

Source code in orderings/core.py
152
153
154
155
156
157
158
159
160
161
def is_compare(item: Any) -> TypeIs[Compare]:
    """Checks if the `item` implements the [`Compare`][orderings.core.Compare] protocol.

    Arguments:
        item: The item to check.

    Returns:
        Whether the `item` implements the [`Compare`][orderings.core.Compare] protocol.
    """
    return is_instance(item, Compare)

compare(left: PartialOrdered[T], right: T) -> Ordering

Compares left and right, returning Ordering.

Parameters:

Name Type Description Default
left PartialOrdered[T]

The left value.

required
right T

The right value.

required

Returns:

Type Description
Ordering

The result.

Source code in orderings/core.py
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
def compare(left: PartialOrdered[T], right: T) -> Ordering:
    """Compares `left` and `right`, returning [`Ordering`][orderings.core.Ordering].

    Arguments:
        left: The left value.
        right: The right value.

    Returns:
        The result.
    """
    if left < right:
        return Ordering.LESS

    if left > right:
        return Ordering.GREATER

    return Ordering.EQUAL