utils.cmp
| Ordering and Comparison
DeprecatedOrdering: te.TypeAlias = t.Literal[0, -1, 1]
module-attribute
This type refers to that Python2 uses to represent ordering in, for example, the sorted
function.
SupportsIntoOrdering: te.TypeAlias = t.Union['Ordering', int, float, DeprecatedOrdering]
module-attribute
Type that can be turned into an Ordering
.
Ordering
Bases: IntEnum
Basic ordering enum.
This enum represents an ordering between two items.
Python does not offer a standard way to represent ordering, and has different usages in different versions (e.g. Python2 uses positive/negative number). And this util class is used to standardize the ordering.
Some of the api offered by this library will use this representation, and translations from other types is also available.
Source code in monad_std/utils/cmp.py
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 90 91 92 93 94 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 120 121 122 123 124 125 126 127 128 129 130 |
|
from_cmp(cmp)
staticmethod
Construct the ordering from the deprecated ordering representation.
This has the same internal implementation as the from_num
method.
However, this takes DeprecatedOrdering
as argument instead of anything that can compare with 0
.
The motivation of this specialized method is to guarantee type safety.
It's also recommended to use this method to specify your target and what you want.
But for more general use, you can also choose to use from_num
instead.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
cmp |
DeprecatedOrdering
|
See |
required |
Examples:
Source code in monad_std/utils/cmp.py
from_num(n)
staticmethod
Construct the ordering from the deprecated ordering representation.
This accepts any value that can compare with int
(or more explicitly 0
),
different from from_cmp
.
It's generally discouraged to use this method.
Examples:
Source code in monad_std/utils/cmp.py
is_ge()
is_le()
is_ne()
parse(arg)
staticmethod
Detect an ordering and returns an Ordering
.
Examples:
Source code in monad_std/utils/cmp.py
to_cmp()
Transfer the ordering to the deprecated ordering representation.
Returns:
Type | Description |
---|---|
DeprecatedOrdering
|
See |
Source code in monad_std/utils/cmp.py
compare(a, b)
Compare two item and generate the ordering.
Examples:
Source code in monad_std/utils/cmp.py
max_by(a, b, fn)
Use the given function to calculate the maximum value in a and b.
The fn
parameter should return SupportsIntoOrdering
.
See its documentation for more information.
If the function returns Ordering.Equal
, then b
is returned.
Examples:
The latter case is same as the following:
Source code in monad_std/utils/cmp.py
min_by(a, b, fn)
Use the given function to calculate the minimum value in a and b.
The fn
parameter should return SupportsIntoOrdering
.
See its documentation for more information.
If the function returns Ordering.Equal
, then b
is returned.
Examples:
The latter case is same as the following: