Style: apply black

This commit is contained in:
2019-05-14 06:55:56 +02:00
parent 7097801306
commit 09b04d2703
35 changed files with 533 additions and 354 deletions

View File

@@ -14,11 +14,8 @@ import random
__all__ = ["reject_random", "filter_random", "FilterRandom"]
def reject_random(min_value = -10,
max_value = 10,
rejected = [0, 1],
accept_callbacks=[],
):
def reject_random(min_value=-10, max_value=10, rejected=[0, 1], accept_callbacks=[]):
""" Generate a random integer with the rejection method
:param name: name of the Integer
@@ -61,11 +58,8 @@ def reject_random(min_value = -10,
return candidate
def filter_random(min_value = -10,
max_value = 10,
rejected = [0, 1],
accept_callbacks=[],
):
def filter_random(min_value=-10, max_value=10, rejected=[0, 1], accept_callbacks=[]):
""" Generate a random integer by filtering then choosing a candidate
:param name: name of the Integer
@@ -99,37 +93,44 @@ def filter_random(min_value = -10,
>>> filter_random()
6
"""
candidates = set(range(min_value, max_value+1))
candidates = set(range(min_value, max_value + 1))
candidates = {c for c in candidates if c not in rejected}
candidates = [candidate for candidate in candidates \
if all(c(candidate) for c in accept_callbacks)]
candidates = [
candidate
for candidate in candidates
if all(c(candidate) for c in accept_callbacks)
]
if len(candidates) == 0:
raise OverflowError("There is no candidates for this range and those conditions")
raise OverflowError(
"There is no candidates for this range and those conditions"
)
return random.choice(candidates)
class FilterRandom(object):
""" Integer random generator which filter then choose candidate
"""
# TODO: Faire un cache pour éviter de reconstruire les listes à chaque fois |ven. déc. 21 19:07:42 CET 2018
def __init__(self,
rejected = [0, 1],
accept_callbacks=[],
min_value = -10,
max_value = 10,
):
def __init__(
self, rejected=[0, 1], accept_callbacks=[], min_value=-10, max_value=10
):
self.conditions = (lambda x: x not in rejected,) + tuple(accept_callbacks)
self._min = min_value
self._max = max_value
candidates = set(range(self._min, self._max+1))
candidates = set(range(self._min, self._max + 1))
self._candidates = { candidate for candidate in candidates \
if all(c(candidate) for c in self.conditions) }
self._candidates = {
candidate
for candidate in candidates
if all(c(candidate) for c in self.conditions)
}
def add_candidates(self, low, high):
""" Add candidates between low and high to _candidates """
@@ -144,13 +145,16 @@ class FilterRandom(object):
else:
useless_high = True
if not(useless_low and useless_high):
candidates = set(range(low, high+1))
if not (useless_low and useless_high):
candidates = set(range(low, high + 1))
self._candidates = self._candidates.union({
candidate for candidate in candidates \
if all(c(candidate) for c in self.conditions) \
})
self._candidates = self._candidates.union(
{
candidate
for candidate in candidates
if all(c(candidate) for c in self.conditions)
}
)
def candidates(self, min_value=-10, max_value=10):
""" Return candidates between min_value and max_value """