proso package¶
Subpackages¶
Submodules¶
proso.dict module¶
Utility functions for manipulation with Python dictionaries.
-
proso.dict.
group_keys_by_value_lists
(d)[source]¶ Take a dict (A -> [B]( and return another one (B -> [A]). It groups keys from the original dict by their values in lists.
>>> pprint(group_keys_by_value_lists({1: [True], 2: [False], 3: [True], 4: [True, False]})) {False: [2, 4], True: [1, 3, 4]}
Parameters: d (dict) – original dictionary which will be transformed. Returns: new keys are taken from original values, each new key points to a list where all values are original keys pointing to the same value Return type: dict
-
proso.dict.
group_keys_by_values
(d)[source]¶ Take a dict (A -> B( and return another one (B -> [A]). It groups keys from the original dict by their values.
>>> pprint(group_keys_by_values({1: True, 2: False, 3: True, 4: True})) {False: [2], True: [1, 3, 4]}
Parameters: d (dict) – original dictionary which will be transformed. Returns: new keys are taken from original values, each new key points to a list where all values are original keys pointing to the same value Return type: dict
proso.func module¶
-
proso.func.
LAMBDA
()¶
-
proso.func.
fixed_point
(is_zero, plus, minus, f, x)[source]¶ Get the least fixed point when it can be computed piecewise.
>>> sorted(fixed_point( ... is_zero=lambda xs: len(xs) == 0, ... plus=lambda xs, ys: xs + ys, ... minus=lambda xs, ys: [x for x in xs if x not in ys], ... f=lambda xs: [x + 1 for x in xs if x < 10], ... x=[0, 5, 8] ... )) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Parameters: - is_zero – function returning True if the given value is zero
- plus – function taking two values and returning their addition
- minus – function taking two values and returning ther difference
- f – function computing the expected value
- x – initial value
Returns: The least fixed point.
-
proso.func.
is_lambda
(fun)[source]¶ Check whether the given function is a lambda function.
def not_lambda_fun(): return 1 lambda_fun = lambda: 1 print( is_lambda(not_lambda_fun), is_lambda(lambda_fun) )
False True
Parameters: fun (function) – Returns: True if the given function is a lambda function, False otherwise Return type: bool
proso.list module¶
Utility functions for manipulation with Python lists.
-
proso.list.
flatten
(xxs)[source]¶ Take a list of lists and return list of values.
>>> flatten([[1, 2], [3, 4]]) [1, 2, 3, 4]
-
proso.list.
group_by
(what, by)[source]¶ Take a list and apply the given function on each its value, then group the values by the function results.
>>> group_by([i for i in range(10)], by=lambda x: x % 2 == 0) {False: [1, 3, 5, 7, 9], True: [0, 2, 4, 6, 8]}
Parameters: - what – a list which will be transformed
- by – a function which will be applied on values of the given list
Returns: values groupped by the function results
Return type:
proso.rand module¶
-
proso.rand.
roulette
(weights, n)[source]¶ Choose randomly the given number of items. The probability the item is chosen is proportionate to its weight.
print(roulette({'cat': 2, 'dog': 1000}, 1))
['dog']
Parameters: - weights (dict) – item -> weight mapping, non-positive weights are forbidden
- n (int) – number of chosen items
Returns: randomly chosen items
Return type: