2018-12-22 07:42:31 +00:00
|
|
|
#! /usr/bin/env python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
# vim:fenc=utf-8
|
|
|
|
#
|
|
|
|
# Copyright © 2017 lafrite <lafrite@Poivre>
|
|
|
|
#
|
|
|
|
# Distributed under terms of the MIT license.
|
|
|
|
|
|
|
|
"""
|
2019-01-07 08:42:43 +00:00
|
|
|
Function to create random integer
|
2018-12-22 07:42:31 +00:00
|
|
|
|
|
|
|
"""
|
2019-01-07 08:24:19 +00:00
|
|
|
import random
|
2018-12-22 07:42:31 +00:00
|
|
|
|
|
|
|
__all__ = ["reject_random", "filter_random", "FilterRandom"]
|
|
|
|
|
2019-05-14 04:55:56 +00:00
|
|
|
|
|
|
|
def reject_random(min_value=-10, max_value=10, rejected=[0, 1], accept_callbacks=[]):
|
2021-10-09 04:30:38 +00:00
|
|
|
"""Generate a random integer with the rejection method
|
|
|
|
|
|
|
|
:param name: name of the Integer
|
|
|
|
:param min_value: minimum value
|
|
|
|
:param max_value: maximum value
|
|
|
|
:param rejected: rejected values
|
|
|
|
:param accept_callbacks: list of function for value rejection
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> a = reject_random()
|
|
|
|
>>> a not in [0, 1]
|
|
|
|
True
|
|
|
|
>>> a >= -10
|
|
|
|
True
|
|
|
|
>>> a <= 10
|
|
|
|
True
|
|
|
|
>>> a = reject_random(min_value=3, max_value=11, rejected=[5, 7])
|
|
|
|
>>> a not in [5, 7]
|
|
|
|
True
|
|
|
|
>>> a >= 3
|
|
|
|
True
|
|
|
|
>>> a <= 11
|
|
|
|
True
|
|
|
|
>>> a = reject_random(accept_callbacks=[lambda x: x%2])
|
|
|
|
>>> a%2
|
|
|
|
1
|
|
|
|
>>> random.seed(0)
|
|
|
|
>>> reject_random()
|
|
|
|
2
|
|
|
|
>>> random.seed(1)
|
|
|
|
>>> reject_random()
|
|
|
|
-6
|
2019-01-07 08:24:19 +00:00
|
|
|
|
2018-12-22 07:42:31 +00:00
|
|
|
"""
|
|
|
|
conditions = [lambda x: x not in rejected] + accept_callbacks
|
|
|
|
|
2019-01-07 08:24:19 +00:00
|
|
|
candidate = random.randint(min_value, max_value)
|
2018-12-22 07:42:31 +00:00
|
|
|
while not all(c(candidate) for c in conditions):
|
2019-01-07 08:24:19 +00:00
|
|
|
candidate = random.randint(min_value, max_value)
|
2018-12-22 07:42:31 +00:00
|
|
|
|
|
|
|
return candidate
|
|
|
|
|
2019-05-14 04:55:56 +00:00
|
|
|
|
|
|
|
def filter_random(min_value=-10, max_value=10, rejected=[0, 1], accept_callbacks=[]):
|
2021-10-09 04:30:38 +00:00
|
|
|
"""Generate a random integer by filtering then choosing a candidate
|
|
|
|
|
|
|
|
:param name: name of the Integer
|
|
|
|
:param min_value: minimum value
|
|
|
|
:param max_value: maximum value
|
|
|
|
:param rejected: rejected values
|
|
|
|
:param accept_callbacks: list of function for value rejection
|
|
|
|
|
|
|
|
:example:
|
|
|
|
>>> a = filter_random()
|
|
|
|
>>> a not in [0, 1]
|
|
|
|
True
|
|
|
|
>>> a >= -10
|
|
|
|
True
|
|
|
|
>>> a <= 10
|
|
|
|
True
|
|
|
|
>>> a = filter_random(min_value=3, max_value=11, rejected=[5, 7])
|
|
|
|
>>> a not in [5, 7]
|
|
|
|
True
|
|
|
|
>>> a >= 3
|
|
|
|
True
|
|
|
|
>>> a <= 11
|
|
|
|
True
|
|
|
|
>>> a = filter_random(accept_callbacks=[lambda x: x%2])
|
|
|
|
>>> a%2
|
|
|
|
1
|
|
|
|
>>> random.seed(0)
|
|
|
|
>>> filter_random()
|
|
|
|
-7
|
|
|
|
>>> random.seed(1)
|
|
|
|
>>> filter_random()
|
|
|
|
6
|
2018-12-22 07:42:31 +00:00
|
|
|
"""
|
2019-05-14 04:55:56 +00:00
|
|
|
candidates = set(range(min_value, max_value + 1))
|
2018-12-22 07:42:31 +00:00
|
|
|
candidates = {c for c in candidates if c not in rejected}
|
|
|
|
|
2019-05-14 04:55:56 +00:00
|
|
|
candidates = [
|
|
|
|
candidate
|
|
|
|
for candidate in candidates
|
|
|
|
if all(c(candidate) for c in accept_callbacks)
|
|
|
|
]
|
2018-12-22 07:42:31 +00:00
|
|
|
|
|
|
|
if len(candidates) == 0:
|
2019-05-14 04:55:56 +00:00
|
|
|
raise OverflowError(
|
|
|
|
"There is no candidates for this range and those conditions"
|
|
|
|
)
|
2019-01-07 08:24:19 +00:00
|
|
|
return random.choice(candidates)
|
2018-12-22 07:42:31 +00:00
|
|
|
|
2019-05-14 04:55:56 +00:00
|
|
|
|
2018-12-22 07:42:31 +00:00
|
|
|
class FilterRandom(object):
|
|
|
|
|
2021-10-09 04:30:38 +00:00
|
|
|
"""Integer random generator which filter then choose candidate"""
|
2019-05-14 04:55:56 +00:00
|
|
|
|
2018-12-22 07:42:31 +00:00
|
|
|
# TODO: Faire un cache pour éviter de reconstruire les listes à chaque fois |ven. déc. 21 19:07:42 CET 2018
|
|
|
|
|
2019-05-14 04:55:56 +00:00
|
|
|
def __init__(
|
|
|
|
self, rejected=[0, 1], accept_callbacks=[], min_value=-10, max_value=10
|
|
|
|
):
|
2018-12-22 07:42:31 +00:00
|
|
|
self.conditions = (lambda x: x not in rejected,) + tuple(accept_callbacks)
|
|
|
|
|
|
|
|
self._min = min_value
|
|
|
|
self._max = max_value
|
|
|
|
|
2019-05-14 04:55:56 +00:00
|
|
|
candidates = set(range(self._min, self._max + 1))
|
2018-12-22 07:42:31 +00:00
|
|
|
|
2019-05-14 04:55:56 +00:00
|
|
|
self._candidates = {
|
|
|
|
candidate
|
|
|
|
for candidate in candidates
|
|
|
|
if all(c(candidate) for c in self.conditions)
|
|
|
|
}
|
2018-12-22 07:42:31 +00:00
|
|
|
|
|
|
|
def add_candidates(self, low, high):
|
2021-10-09 04:30:38 +00:00
|
|
|
"""Add candidates between low and high to _candidates"""
|
2018-12-22 07:42:31 +00:00
|
|
|
if low < self._min:
|
|
|
|
self._min = low
|
|
|
|
useless_low = False
|
|
|
|
else:
|
|
|
|
useless_low = True
|
|
|
|
if high > self._max:
|
|
|
|
self._max = high
|
|
|
|
useless_high = False
|
|
|
|
else:
|
|
|
|
useless_high = True
|
|
|
|
|
2019-05-14 04:55:56 +00:00
|
|
|
if not (useless_low and useless_high):
|
|
|
|
candidates = set(range(low, high + 1))
|
2018-12-22 07:42:31 +00:00
|
|
|
|
2019-05-14 04:55:56 +00:00
|
|
|
self._candidates = self._candidates.union(
|
|
|
|
{
|
|
|
|
candidate
|
|
|
|
for candidate in candidates
|
|
|
|
if all(c(candidate) for c in self.conditions)
|
|
|
|
}
|
|
|
|
)
|
2018-12-22 07:42:31 +00:00
|
|
|
|
|
|
|
def candidates(self, min_value=-10, max_value=10):
|
2021-10-09 04:30:38 +00:00
|
|
|
"""Return candidates between min_value and max_value"""
|
2018-12-22 07:42:31 +00:00
|
|
|
return [c for c in self._candidates if (c > min_value and c < max_value)]
|
|
|
|
|
|
|
|
def __call__(self, min_value=-10, max_value=10):
|
2021-10-09 04:30:38 +00:00
|
|
|
"""Randomly choose on candidate"""
|
2018-12-22 07:42:31 +00:00
|
|
|
self.add_candidates(min_value, max_value)
|
2019-01-07 08:24:19 +00:00
|
|
|
return random.choice(self.candidates(min_value, max_value))
|
2018-12-22 07:42:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
|
|
|
# cursor: 16 del
|