def _handle_in(self, value): candidates = list(chain(*(self._handle_state(i) for i in value))) if candidates[0] is False: candidates = set(string.printable).difference(candidates[1:]) return random_choice(list(candidates)) else: return random_choice(candidates)
def __init__(self): super(Xeger, self).__init__() self._cache = dict() self._categories = { "category_digit": lambda: self._alphabets["digits"], "category_not_digit": lambda: self._alphabets["nondigits"], "category_space": lambda: self._alphabets["whitespace"], "category_not_space": lambda: self._alphabets["nonwhitespace"], "category_word": lambda: self._alphabets["word"], "category_not_word": lambda: self._alphabets["nonword"], } self._cases = { "literal": lambda x: unichr(x), "not_literal": lambda x: random_choice(string.printable.replace(unichr(x), "")), "at": lambda x: "", "in": lambda x: self._handle_in(x), "any": lambda x: self.printable(1), "range": lambda x: [unichr(i) for i in xrange(x[0], x[1] + 1)], "category": lambda x: self._categories[x](), "branch": lambda x: "".join(self._handle_state(i) for i in random_choice(x[1])), "subpattern": lambda x: self._handle_group(x), "assert": lambda x: "".join(self._handle_state(i) for i in x[1]), "assert_not": lambda x: "", "groupref": lambda x: self._cache[x], "max_repeat": lambda x: self._handle_repeat(*x), "negate": lambda x: [False], }
def __init__(self): super(Xeger, self).__init__() self._cache = dict() self._categories = { "category_digit": lambda: self._alphabets['digits'], "category_not_digit": lambda: self._alphabets['nondigits'], "category_space": lambda: self._alphabets['whitespace'], "category_not_space": lambda: self._alphabets['nonwhitespace'], "category_word": lambda: self._alphabets['word'], "category_not_word": lambda: self._alphabets['nonword'], } self._cases = { "literal": lambda x: unichr(x), "not_literal": lambda x: random_choice(string.printable.replace(unichr(x), '')), "at": lambda x: '', "in": lambda x: self._handle_in(x), "any": lambda x: self.printable(1), "range": lambda x: [unichr(i) for i in xrange(x[0], x[1] + 1)], "category": lambda x: self._categories[x](), 'branch': lambda x: ''.join( self._handle_state(i) for i in random_choice(x[1])), "subpattern": lambda x: self._handle_group(x), "assert": lambda x: ''.join(self._handle_state(i) for i in x[1]), "assert_not": lambda x: '', "groupref": lambda x: self._cache[x], 'max_repeat': lambda x: self._handle_repeat(*x), 'negate': lambda x: [False], }
def sample_wr(population, k): """Samples k random elements (with replacement) from a population""" return [random_choice(population) for i in itertools.repeat(None, k)]
def test_008_random_choice(self): population = [0, 1, 2, 3, 4, 5] self.assertTrue(utility.random_choice(population) in population)