ScaleFromSet

class whalrus.ScaleFromSet(levels: set)[source]

Scale derived from a set.

Parameters:levels (set) – A set of comparable objects. It is recommended that they are also hashable.

Examples

Typical usage:

>>> scale = ScaleFromSet({-1, 0, 2})

A more complex example:

>>> class Appreciation:
...     VALUES = {'Excellent': 2, 'Good': 1, 'Medium': 0}
...     def __init__(self, x):
...         self.x = x
...     def __repr__(self):
...         return 'Appreciation(%r)' % self.x
...     def __hash__(self):
...         return hash(self.x)
...     def __lt__(self, other):
...         return Appreciation.VALUES[self.x] < Appreciation.VALUES[other.x]
>>> scale = ScaleFromSet({Appreciation('Excellent'), Appreciation('Good'),
...                      Appreciation('Medium')})
>>> scale.lt(Appreciation('Medium'), Appreciation('Good'))
True
>>> scale.low
Appreciation('Medium')
>>> scale.high
Appreciation('Excellent')
argsort(some_list: list, reverse: bool = False) → list

Examples

>>> scale = ScaleFromList(['Bad', 'Medium', 'Good', 'Very good', 'Excellent'])
>>> scale.argsort(['Good', 'Bad', 'Excellent'])
[1, 0, 2]
compare(one: object, another: object) → int

Compare two levels.

Parameters:
  • one (object) – A level.
  • another (object) – A level.
Returns:

0 if they are equal, a positive number if one is greater than another, a negative number otherwise.

Return type:

int

Examples

>>> Scale().compare('a', 'z')
-1
eq(one: object, another: object) → bool

Test “equal”. Cf. lt().

ge(one: object, another: object) → bool

Test “greater or equal”. Cf. lt().

gt(one: object, another: object) → bool

Test “greater than”. Cf. lt().

high
>>> scale = ScaleFromList(['Bad', 'Medium', 'Good', 'Very good', 'Excellent'])
>>> scale.high
'Excellent'
le(one: object, another: object) → bool

Test “lower or equal”. Cf. lt().

low
>>> scale = ScaleFromList(['Bad', 'Medium', 'Good', 'Very good', 'Excellent'])
>>> scale.low
'Bad'
lt(one: object, another: object) → bool[source]

Examples

>>> scale = ScaleFromSet({-1, 0, 2})
>>> scale.lt(0, 2)
True
max(iterable: Iterable[T_co]) → object

Examples

>>> scale = ScaleFromList(['Bad', 'Medium', 'Good', 'Very good', 'Excellent'])
>>> scale.max(['Good', 'Bad', 'Excellent'])
'Excellent'
min(iterable: Iterable[T_co]) → object

Examples

>>> scale = ScaleFromList(['Bad', 'Medium', 'Good', 'Very good', 'Excellent'])
>>> scale.min(['Good', 'Bad', 'Excellent'])
'Bad'
ne(one: object, another: object) → bool

Test “not equal”. Cf. lt().

sort(some_list: list, reverse: bool = False) → None

Examples

>>> scale = ScaleFromList(['Bad', 'Medium', 'Good', 'Very good', 'Excellent'])
>>> some_list = ['Good', 'Bad', 'Excellent']
>>> scale.sort(some_list)
>>> some_list
['Bad', 'Good', 'Excellent']