#! /usr/bin/env python # -*- coding: utf-8 -*- # vim:fenc=utf-8 # # Copyright © 2017 lafrite # # Distributed under terms of the MIT license. """ Function to create random things """ from random import randint, choice __all__ = ["reject_random", "filter_random", "FilterRandom"] 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 :param min_value: minimum value :param max_value: maximum value :param rejected: rejected values :param accept_callbacks: list of function for value rejection """ conditions = [lambda x: x not in rejected] + accept_callbacks candidate = randint(min_value, max_value) while not all(c(candidate) for c in conditions): candidate = randint(min_value, max_value) return candidate 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 :param min_value: minimum value :param max_value: maximum value :param rejected: rejected values :param accept_callbacks: list of function for value rejection """ 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)] if len(candidates) == 0: raise OverflowError("There is no candidates for this range and those conditions") return 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, ): 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)) 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 """ 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 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) \ }) def candidates(self, min_value=-10, max_value=10): """ Return candidates between min_value and max_value """ return [c for c in self._candidates if (c > min_value and c < max_value)] def __call__(self, min_value=-10, max_value=10): """ Randomly choose on candidate """ self.add_candidates(min_value, max_value) return choice(self.candidates(min_value, max_value)) # ----------------------------- # Reglages pour 'vim' # vim:set autoindent expandtab tabstop=4 shiftwidth=4: # cursor: 16 del