本文整理汇总了Python中hypothesis.strategies.lists方法的典型用法代码示例。如果您正苦于以下问题:Python strategies.lists方法的具体用法?Python strategies.lists怎么用?Python strategies.lists使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类hypothesis.strategies
的用法示例。
在下文中一共展示了strategies.lists方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: pattern_to_statements
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import lists [as 别名]
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.
示例2: _array_strategy
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import lists [as 别名]
def _array_strategy(
abi_type: BasicType,
min_length: ArrayLengthType = 1,
max_length: ArrayLengthType = 8,
unique: bool = False,
**kwargs: Any,
) -> SearchStrategy:
if abi_type.arrlist[-1]:
min_len = max_len = abi_type.arrlist[-1][0]
else:
dynamic_len = len([i for i in abi_type.arrlist if not i])
min_len = _get_array_length("min_length", min_length, dynamic_len)
max_len = _get_array_length("max_length", max_length, dynamic_len)
if abi_type.item_type.is_array:
kwargs.update(min_length=min_length, max_length=max_length, unique=unique)
base_strategy = strategy(abi_type.item_type.to_type_str(), **kwargs)
strat = st.lists(base_strategy, min_size=min_len, max_size=max_len, unique=unique)
# swap 'size' for 'length' in the repr
repr_ = "length".join(strat.__repr__().rsplit("size", maxsplit=2))
strat._LazyStrategy__representation = repr_ # type: ignore
return strat
示例3: arguments_node
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import lists [as 别名]
def arguments_node(draw, annotated=False):
n = draw(hs.integers(min_value=1, max_value=5))
args = draw(hs.lists(name_node(None), min_size=n, max_size=n))
if annotated:
annotations = draw(hs.lists(name_node(annotation), min_size=n, max_size=n))
else:
annotations = None
node = astroid.Arguments()
node.postinit(
args,
None,
None,
None,
annotations
)
return node
示例4: from_schema
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import lists [as 别名]
def from_schema(schema):
"""Returns a strategy for objects that match the given schema."""
check_schema(schema)
# TODO: actually handle constraints on number/string/array schemas
return dict(
null=st.none(),
bool=st.booleans(),
number=st.floats(allow_nan=False),
string=st.text(),
array=st.lists(st.nothing()),
)[schema["type"]]
# `@st.composite` is one way to write this - another would be to define a
# bare function, and `return st.one_of(st.none(), st.booleans(), ...)` so
# each strategy can be defined individually. Use whichever seems more
# natural to you - the important thing in tests is usually readability!
示例5: node_uuid_pool_strategy
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import lists [as 别名]
def node_uuid_pool_strategy(draw, min_number_of_nodes=1):
"""
A strategy to create a pool of node uuids.
:param min_number_of_nodes: The minimum number of nodes to create.
:returns: A strategy to create an iterable of node uuids.
"""
max_number_of_nodes = max(min_number_of_nodes, 10)
return draw(
st.lists(
uuids(),
min_size=min_number_of_nodes,
max_size=max_number_of_nodes
)
)
示例6: same_len_lists
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import lists [as 别名]
def same_len_lists(draw, min_value = None, max_value = None):
"""Draw random arrays of equal lengths
One precondition of the list version of spherical() is that its inputs must
be of equal length.
"""
n = draw(integers(min_value = 0, max_value = 50))
fixlen = lists(
floats(
min_value = min_value,
max_value = max_value,
allow_nan = False,
allow_infinity = False
),
min_size = n,
max_size = n,
)
fixnp = fixlen.map(np.array)
return (draw(fixnp), draw(fixnp), draw(fixnp))
示例7: _fuzz_array
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import lists [as 别名]
def _fuzz_array(
parameter: Dict[str, Any],
required: bool = False,
) -> SearchStrategy:
item = parameter['items']
required = parameter.get('required', required)
# TODO: Handle `oneOf`
strategy = st.lists(
elements=_fuzz_parameter(item, required=required),
min_size=parameter.get(
'minItems',
0 if not required else 1,
),
max_size=parameter.get('maxItems', None),
)
if not required:
return st.one_of(st.none(), strategy)
return strategy
示例8: action_structures
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import lists [as 别名]
def action_structures(draw):
"""
A Hypothesis strategy that creates a tree of L{ActionStructure} and
L{unicode}.
"""
tree = draw(st.recursive(labels, st.lists, max_leaves=20))
def to_structure(tree_or_message):
if isinstance(tree_or_message, list):
return ActionStructure(
type=draw(labels),
failed=draw(st.booleans()),
children=[to_structure(o) for o in tree_or_message],
)
else:
return tree_or_message
return to_structure(tree)
示例9: test_concat_basic
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import lists [as 别名]
def test_concat_basic(nums: tp.List[int]):
nums_py = list(map(lambda x: x + 1, nums))
nums_py1 = list(map(lambda x: x ** 2, nums_py))
nums_py2 = list(map(lambda x: -x, nums_py))
nums_py = nums_py1 + nums_py2
nums_pl = pl.process.map(lambda x: x + 1, nums)
nums_pl1 = pl.process.map(lambda x: x ** 2, nums_pl)
nums_pl2 = pl.process.map(lambda x: -x, nums_pl)
nums_pl = pl.process.concat([nums_pl1, nums_pl2])
assert sorted(nums_pl) == sorted(nums_py)
# @hp.given(nums=st.lists(st.integers()))
# @hp.settings(max_examples=MAX_EXAMPLES)
示例10: test_concat_basic_2
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import lists [as 别名]
def test_concat_basic_2(nums: tp.List[int]):
nums_py = list(map(lambda x: x + 1, nums))
nums_py1 = list(map(lambda x: x ** 2, nums_py))
nums_py2 = list(map(lambda x: -x, nums_py))
nums_py = nums_py1 + nums_py2
nums_pl = pl.task.map(lambda x: x + 1, nums)
nums_pl1 = pl.task.map(lambda x: x ** 2, nums_pl)
nums_pl2 = pl.task.map(lambda x: -x, nums_pl)
nums_pl = await pl.task.concat([nums_pl1, nums_pl2])
assert sorted(nums_pl) == sorted(nums_py)
# @hp.given(nums=st.lists(st.integers()))
# @hp.settings(max_examples=MAX_EXAMPLES)
示例11: test_concat_basic
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import lists [as 别名]
def test_concat_basic(nums: tp.List[int]):
nums_py = list(map(lambda x: x + 1, nums))
nums_py1 = list(map(lambda x: x ** 2, nums_py))
nums_py2 = list(map(lambda x: -x, nums_py))
nums_py = nums_py1 + nums_py2
nums_pl = pl.thread.map(lambda x: x + 1, nums)
nums_pl1 = pl.thread.map(lambda x: x ** 2, nums_pl)
nums_pl2 = pl.thread.map(lambda x: -x, nums_pl)
nums_pl = pl.thread.concat([nums_pl1, nums_pl2])
assert sorted(nums_pl) == sorted(nums_py)
# @hp.given(nums=st.lists(st.integers()))
# @hp.settings(max_examples=MAX_EXAMPLES)
示例12: func_wrap_strategy
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import lists [as 别名]
def func_wrap_strategy(args, func):
min_size = func.arity[0]
max_size = func.arity[1] and func.arity[0] or 4
return st.lists(args, min_size=min_size, max_size=max_size).map(lambda a: func(*a))
示例13: _draw_capabilities
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import lists [as 别名]
def _draw_capabilities(self, data, sensor):
if len(sensor.allowed_combo) > 0:
# test capabilities 1 by 1,
# or some combination of those in the allowed_combo list
capabilities = data.draw(
st.one_of(
st.lists(st.sampled_from([cap.name for cap in list(sensor.capability)]), min_size=1, max_size=1),
st.lists(st.sampled_from(sensor.capability), min_size=1, max_size=1),
st.lists(st.sampled_from(sensor.allowed_combo), min_size=1, unique=True)
)
)
else:
# if no combos allowed, then just test 1 by 1
capabilities = data.draw(st.lists(st.sampled_from(sensor.capability), min_size=1, max_size=1))
return capabilities
示例14: test_port_value_message
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import lists [as 别名]
def test_port_value_message(self, data):
port = data.draw(st.integers(0,255))
width = data.draw(st.integers(1,3))
nbytes = 1<<(width-1)
values = data.draw(st.lists(st.integers(0,255),min_size=nbytes,max_size=nbytes ))
msg_type = 0x45
msg = bytearray([msg_type, port]+values)
l = self.m.parse(self._with_header(msg))
self.hub.peripheral_queue.put.assert_called_with(('value_change', (port,values)))
示例15: test_attach_message
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import lists [as 别名]
def test_attach_message(self, data, port, event):
msg_type = 0x04
msg = bytearray([msg_type, port, event])
if event == 0: #detach
l = self.m.parse(self._with_header(msg))
assert l == f'Detached IO Port:{port}'
elif event == 1: #attach
# Need 10 bytes
#dev_id = data.draw(st.integers(0,255))
dev_id = data.draw(st.sampled_from(sorted(DEVICES.keys())))
fw_version = data.draw(st.lists(st.integers(0,255), min_size=8, max_size=8))
msg = msg + bytearray([dev_id, 0])+ bytearray(fw_version)
l = self.m.parse(self._with_header(msg))
self.hub.peripheral_queue.put.assert_any_call(('update_port', (port, self.m.port_info[port])))
self.hub.peripheral_queue.put.assert_any_call(('port_detected', port))
# ALso need to make sure the port info is added to dispatch
assert self.m.port_info[port]['name'] == DEVICES[dev_id]
elif event == 2: # virtual attach
dev_id = data.draw(st.sampled_from(sorted(DEVICES.keys())))
v_port_a = data.draw(st.integers(0,255))
v_port_b = data.draw(st.integers(0,255))
msg = msg + bytearray([dev_id, 0, v_port_a, v_port_b])
l = self.m.parse(self._with_header(msg))
self.hub.peripheral_queue.put.assert_any_call(('update_port', (port, self.m.port_info[port])))
self.hub.peripheral_queue.put.assert_any_call(('port_detected', port))
assert l == f'Attached VirtualIO Port:{port} {self.m.port_info[port]["name"]} Port A: {v_port_a}, Port B: {v_port_b}'
assert self.m.port_info[port]['virtual'] == (v_port_a, v_port_b)
assert self.m.port_info[port]['name'] == DEVICES[dev_id]