本文整理汇总了Python中hypothesis.strategies.tuples方法的典型用法代码示例。如果您正苦于以下问题:Python strategies.tuples方法的具体用法?Python strategies.tuples怎么用?Python strategies.tuples使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类hypothesis.strategies
的用法示例。
在下文中一共展示了strategies.tuples方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: pattern_to_statements
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import tuples [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: test_padding
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import tuples [as 别名]
def test_padding(ndim: int, data: st.DataObject):
"""Ensure that convolving a padding-only image with a commensurate kernel yields the single entry: 0"""
padding = data.draw(
st.integers(1, 3) | st.tuples(*[st.integers(1, 3)] * ndim), label="padding"
)
x = Tensor(
data.draw(
hnp.arrays(shape=(1, 1) + (0,) * ndim, dtype=float, elements=st.floats()),
label="x",
)
)
pad_tuple = padding if isinstance(padding, tuple) else (padding,) * ndim
kernel = data.draw(
hnp.arrays(
shape=(1, 1) + tuple(2 * p for p in pad_tuple),
dtype=float,
elements=st.floats(allow_nan=False, allow_infinity=False),
)
)
out = conv_nd(x, kernel, padding=padding, stride=1)
assert out.shape == (1,) * x.ndim
assert out.item() == 0.0
out.sum().backward()
assert x.grad.shape == x.shape
示例3: test_upcast_roundtrip
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import tuples [as 别名]
def test_upcast_roundtrip(type_strategy, data: st.DataObject):
thin, wide = data.draw(
st.tuples(type_strategy, type_strategy).map(
lambda x: sorted(x, key=lambda y: np.dtype(y).itemsize)
)
)
orig_tensor = data.draw(
hnp.arrays(
dtype=thin,
shape=hnp.array_shapes(),
elements=hnp.from_dtype(thin).filter(np.isfinite),
).map(Tensor)
)
roundtripped_tensor = orig_tensor.astype(wide).astype(thin)
assert_array_equal(orig_tensor, roundtripped_tensor)
示例4: ds_vars_dims_mixed
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import tuples [as 别名]
def ds_vars_dims_mixed():
def build_it(vars_to_dims_):
all_dims = list(set(sum((list(dd) for dd in vars_to_dims_.values()), [])))
ds = fixed_datasets(vars_to_dims_)
dims = subset_lists(all_dims)
vars_ = sampled_from(list(vars_to_dims_.keys()))
vars_dict = dictionaries(vars_, dims, dict_class=OrderedDict)
vars_dict = vars_dict.map(OrderedDict.items).map(list)
return tuples(ds, vars_dict, just(all_dims))
vars_to_dims_st = vars_to_dims_dicts(min_vars=0, min_dims=0)
S = vars_to_dims_st.flatmap(build_it)
return S
示例5: sig_pair
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import tuples [as 别名]
def sig_pair():
def separate(D):
signatures, signatures_ref = {}, {}
for kk in D:
if len(D[kk]) == 1:
v_ref, = D[kk]
signatures_ref[kk] = np.asarray(v_ref)
elif len(D[kk]) == 2:
v, v_ref = D[kk]
signatures[kk] = np.asarray(v)
signatures_ref[kk] = np.asarray(v_ref)
else:
assert False
return signatures, signatures_ref
sig_dict = dictionaries(text(), tuples(bsigs()) | tuples(bsigs(), bsigs()))
S = sig_dict.map(separate)
return S
示例6: fcall
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import tuples [as 别名]
def fcall(fn, args=None, kwargs=None):
"""
Call function with given positional and keyword args.
"""
if args == () or args is None:
args = st.just(())
elif isinstance(args, (tuple, list)):
args = st.tuples(*args)
if kwargs == {} or kwargs is None:
kwargs = st.just({})
elif isinstance(kwargs, dict):
ks = list(kwargs.keys())
kwargs = st.builds(lambda *xs: dict(zip(ks, xs)), *kwargs.values())
return st.builds(lambda xs, kw: fn(*xs, **kw), args, kwargs)
示例7: trees
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import tuples [as 别名]
def trees(*args, max_depth=None, allow_attrs=True, **kwargs):
"""
Return random trees.
"""
attrs = st.just([])
kwargs["allow_attrs"] = allow_attrs
if allow_attrs:
keys = identifiers(allow_private=False, exclude=("children", "parent"))
attr = st.tuples(keys, kwargs.get("attrs") or atoms())
attrs = st.lists(attr)
fn = partial(shape_tree, max_depth)
return st.builds(fn, attrs, st.lists(leaves(*args, **kwargs)))
#
# Utility functions
#
示例8: _create_hyp_class
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import tuples [as 别名]
def _create_hyp_class(attrs_and_strategy):
"""
A helper function for Hypothesis to generate attrs classes.
The result is a tuple: an attrs class, and a tuple of values to
instantiate it.
"""
def key(t):
return t[0].default is not NOTHING
attrs_and_strat = sorted(attrs_and_strategy, key=key)
attrs = [a[0] for a in attrs_and_strat]
for i, a in enumerate(attrs):
a.counter = i
vals = tuple((a[1]) for a in attrs_and_strat)
return st.tuples(
st.just(
make_class("HypClass", OrderedDict(zip(gen_attr_names(), attrs)))
),
st.tuples(*vals),
)
示例9: _create_hyp_nested_strategy
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import tuples [as 别名]
def _create_hyp_nested_strategy(simple_class_strategy):
"""
Create a recursive attrs class.
Given a strategy for building (simpler) classes, create and return
a strategy for building classes that have as an attribute:
* just the simpler class
* a list of simpler classes
* a dict mapping the string "cls" to a simpler class.
"""
# A strategy producing tuples of the form ([list of attributes], <given
# class strategy>).
attrs_and_classes = st.tuples(
lists_of_attrs(defaults=True), simple_class_strategy
)
return (
attrs_and_classes.flatmap(just_class)
| attrs_and_classes.flatmap(just_class_with_type)
| attrs_and_classes.flatmap(list_of_class)
| attrs_and_classes.flatmap(list_of_class_with_type)
| attrs_and_classes.flatmap(dict_of_class)
)
示例10: _tuple_strategy
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import tuples [as 别名]
def _tuple_strategy(abi_type: TupleType) -> SearchStrategy:
strategies = [strategy(i.to_type_str()) for i in abi_type.components]
return st.tuples(*strategies)
示例11: init_default_strategies
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import tuples [as 别名]
def init_default_strategies() -> None:
"""Register all default "format" strategies."""
register_string_format("binary", st.binary())
register_string_format("byte", st.binary().map(lambda x: b64encode(x).decode()))
def make_basic_auth_str(item: Tuple[str, str]) -> str:
return _basic_auth_str(*item)
register_string_format("_basic_auth", st.tuples(st.text(), st.text()).map(make_basic_auth_str)) # type: ignore
register_string_format("_bearer_auth", st.text().map("Bearer {}".format))
示例12: remove_strategy
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import tuples [as 别名]
def remove_strategy(self):
def get_address(service_name):
return st.tuples(st.just(service_name), st.sampled_from(
self.fake.services[service_name]))
return st.tuples(st.just("remove"), (
st.sampled_from(list(self.fake.services.keys())).flatmap(get_address)))
示例13: steps
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import tuples [as 别名]
def steps(self):
result = add_strategy | replace_strategy
# Replace or add to a known service cluster:
if self.fake.services:
result |= st.tuples(st.just("replace"),
st.tuples(st.sampled_from(list(self.fake.services.keys())),
st.lists(nice_strings)))
result |= st.tuples(st.just("add"),
st.tuples(st.sampled_from(list(self.fake.services.keys())),
nice_strings))
# Remove a known address from known cluster:
if not self.fake.is_empty():
result |= self.remove_strategy()
return result
示例14: add_bools
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import tuples [as 别名]
def add_bools(list_of_lists):
"""
Given recursive list that can contain other lists, return tuple of that plus
a booleans strategy for each list.
"""
l = []
def count(recursive):
l.append(1)
for child in recursive:
if isinstance(child, list):
count(child)
count(list_of_lists)
return st.tuples(st.just(list_of_lists), st.tuples(*[st.sampled_from([True, False]) for i in l]))
示例15: random_reference
# 需要导入模块: from hypothesis import strategies [as 别名]
# 或者: from hypothesis.strategies import tuples [as 别名]
def random_reference(draw, prefix = random_prefix()):
number = st.integers(min_value = 0)
return draw(st.tuples(prefix, number))