Util Module

class whalrus.utils.utils.DeleteCacheMixin[source]

Mixin used to delete cached properties.

Cf. decorator cached_property().

Examples

>>> class Example(DeleteCacheMixin):
...     @cached_property
...     def x(self):
...         print('Big computation...')
...         return 6 * 7
>>> a = Example()
>>> a.x
Big computation...
42
>>> a.x
42
>>> a.delete_cache()
>>> a.x
Big computation...
42
class whalrus.utils.utils.NiceDict[source]

A dict that prints in the order of the keys (when they are comparable).

Examples

>>> my_dict = NiceDict({'b': 51, 'a': 42, 'c': 12})
>>> my_dict
{'a': 42, 'b': 51, 'c': 12}
class whalrus.utils.utils.NiceSet[source]

A set that prints in order (when the elements are comparable).

Examples

>>> my_set = NiceSet({'b', 'a', 'c'})
>>> my_set
{'a', 'b', 'c'}
whalrus.utils.utils.cached_property(f)[source]

Decorator used in replacement of @property to put the value in cache automatically.

The first time the attribute is used, it is computed on-demand and put in cache. Later accesses to the attributes will use the cached value.

Cf. DeleteCacheMixin for an example.

whalrus.utils.utils.convert_number(x: numbers.Number)[source]

Try to convert a number to a fraction (or an integer).

Parameters

x (Number) –

Returns

x, trying to convert it into a fraction (or an integer).

Return type

Number

Examples

>>> convert_number(2.5)
Fraction(5, 2)
>>> convert_number(2.0)
2
whalrus.utils.utils.dict_to_items(d: dict) → list[source]

Convert a dict to a list of pairs (key, value).

Parameters

d (dict) –

Returns

The result is similar to d.items(), but if the keys are comparable, they appear in ascending order.

Return type

list of pairs

Examples

>>> dict_to_items({'b': 2, 'c': 0, 'a': 1})
[('a', 1), ('b', 2), ('c', 0)]
whalrus.utils.utils.dict_to_str(d: dict) → str[source]

Convert dict to string.

Parameters

d (dict) –

Returns

The result is similar to str(d), but if the keys are comparable, they appear in ascending order.

Return type

str

Examples

>>> dict_to_str({'b': 2, 'c': 0, 'a': 1})
"{'a': 1, 'b': 2, 'c': 0}"
whalrus.utils.utils.my_division(x: numbers.Number, y: numbers.Number, divide_by_zero: numbers.Number = None)[source]

Division of two numbers, trying to be exact if it is reasonable.

Parameters
  • x (Number) –

  • y (Number) –

  • divide_by_zero (Number) – The value to be returned in case of division by zero. If None (default), then it raises a ZeroDivisionError.

Returns

The division of x by y.

Return type

Number

Examples

>>> my_division(5, 2)
Fraction(5, 2)

If x or y is a float, then the result is a float:

>>> my_division(Fraction(5, 2), 0.1)
25.0
>>> my_division(0.1, Fraction(5, 2))
0.04

If x and y are integers, decimals or fractions, then the result is a fraction:

>>> my_division(2, Fraction(5, 2))
Fraction(4, 5)
>>> my_division(Decimal('0.1'), Fraction(5, 2))
Fraction(1, 25)

You can specify a particular return value in case of division by zero:

>>> my_division(1, 0, divide_by_zero=42)
42
whalrus.utils.utils.parse_weak_order(s: str) → list[source]

Convert a string representing a weak order to a list of sets.

Parameters

s (str) –

Returns

A list of sets, where each set is an indifference class. The first set of the list contains the top (= most liked) candidates, while the last set of the list contains the bottom (= most disliked) candidates.

Return type

list

Examples

>>> s = 'Alice ~ Bob ~ Catherine32 > me > you ~ us > them'
>>> parse_weak_order(s) == [{'Alice', 'Bob', 'Catherine32'}, {'me'}, {'you', 'us'}, {'them'}]
True
whalrus.utils.utils.set_to_list(s: set) → list[source]

Convert a set to a list.

Parameters

s (set) –

Returns

The result is similar to list(s), but if the elements of the set are comparable, they appear in ascending order.

Return type

list

Examples

>>> set_to_list({2, 42, 12})
[2, 12, 42]
whalrus.utils.utils.set_to_str(s: set) → str[source]

Convert a set to a string.

Parameters

s (set) –

Returns

The result is similar to str(s), but if the elements of the set are comparable, they appear in ascending order.

Return type

str

Examples

>>> set_to_str({2, 42, 12})
'{2, 12, 42}'
whalrus.utils.utils.take_closest(my_list, my_number)[source]

In a list, take the closest element to a given number.

From https://stackoverflow.com/questions/12141150/from-list-of-integers-get-number-closest-to-a-given-value .

Parameters
  • my_list (list) – A list sorted in ascending order.

  • my_number (Number) –

Returns

The element of my_list that is closest to my_number. If two numbers are equally close, return the smallest number.

Return type

Number

Examples

>>> take_closest([0, 5, 10], 3)
5