def pattern_to_statements(pattern):
if isinstance(pattern, template):
return lists(just(pattern), min_size=1, max_size=1)
rule, value = pattern
if rule == 'sequence':
return tuples(*map(pattern_to_statements, value)).map(unpack_list).map(list)
elif rule == 'alternates':
return one_of(*map(pattern_to_statements, value))
elif rule == 'zeroOrMore':
return lists(pattern_to_statements(value)).map(unpack_list).map(list)
elif rule == 'oneOrMore':
return lists(pattern_to_statements(value), min_size=1).map(unpack_list).map(list)
elif rule == 'optional':
return lists(pattern_to_statements(value), min_size=0, max_size=1).map(unpack_list).map(list)
else:
raise Exception("impossible!", rule)
# this replicates the current scorm pattern, a realistic example of medium
# complexity. Note it has repeated elements, just not in ambiguous ways.
python类one_of()的实例源码
def optional(strategy):
"""Return an optional version of the supplied strategy."""
return st.one_of(st.none(), strategy)
def enums_of_primitives(draw):
"""Generate enum classes with primitive values."""
if is_py2:
names = draw(st.sets(st.text(alphabet=string.ascii_letters,
min_size=1),
min_size=1))
else:
names = draw(st.sets(st.text(min_size=1), min_size=1))
n = len(names)
vals = draw(st.one_of(st.sets(st.one_of(
st.integers(),
st.floats(allow_nan=False),
st.text(min_size=1)),
min_size=n, max_size=n)))
return Enum('HypEnum', list(zip(names, vals)))
def create_generic_dict_type(type1, type2):
"""Create a strategy for generating parameterized dict types."""
return st.one_of(dict_types,
dict_types.map(lambda t: t[type1, type2]),
dict_types.map(lambda t: t[Any, type2]),
dict_types.map(lambda t: t[type1, Any]))
def strategy(self):
'Returns resulting strategy that generates configured char set'
max_codepoint = None if self._unicode else 127
strategies = []
if self._negate:
if self._categories or self._whitelist_chars:
strategies.append(
hs.characters(
blacklist_categories=self._categories | set(['Cc', 'Cs']),
blacklist_characters=self._whitelist_chars,
max_codepoint=max_codepoint,
)
)
if self._blacklist_chars:
strategies.append(
hs.sampled_from(
list(self._blacklist_chars - self._whitelist_chars)
)
)
else:
if self._categories or self._blacklist_chars:
strategies.append(
hs.characters(
whitelist_categories=self._categories,
blacklist_characters=self._blacklist_chars,
max_codepoint=max_codepoint,
)
)
if self._whitelist_chars:
strategies.append(
hs.sampled_from(
list(self._whitelist_chars - self._blacklist_chars)
)
)
return hs.one_of(*strategies) if strategies else hs.just(u'')
def slice_node(draw):
lower = draw(hs.one_of(const_node(hs.integers()), hs.none()))
upper = draw(hs.one_of(const_node(hs.integers()), hs.none()))
step = draw(hs.one_of(const_node(hs.integers()), hs.none()))
node = astroid.Slice()
node.postinit(lower, upper, step)
return node
def unaryop_node(draw, op=hs.one_of(non_bool_unary_op, unary_bool_operator),
operand=const_node()):
op = draw(op)
operand = draw(operand)
node = astroid.UnaryOp(op)
node.postinit(operand)
return node