python - 使用假设在 fixed_dictionaries 中生成两个等长列表

标签 python python-hypothesis

我正在尝试使用 fixed_dictionaries 策略生成样本数据,其中两个键的值必须等长,例如:

{'ids': [1, 2, 3],
 'words': ['foo', 'bar', 'baz']}

我如何强制执行此约束?我想我可以将一个定义为另一个的复合,但我不确定该怎么做。像这样的东西:

import hypothesis.strategies as st

ids = st.lists(elements=st.integers())

@st.composite
def words(draw, elements=st.text()):
    draw(sample_ids) # ???

最佳答案

这里有一种方法可以解决这个问题:

@composite
def my_dicts(draw):
    size = draw(st.integers(min_value=0))
    ids = st.lists(min_size=size, max_size=size)
    words = st.lists(min_size=size, max_size=size)
    dicts = st.fixed_dictionaries({'ids': ids, 'words': words})

    return draw(st.lists(dicts))

关于python - 使用假设在 fixed_dictionaries 中生成两个等长列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52447279/

相关文章:

python - 尝试在 Python 3.4.3 上通过 pip 安装 PyGame 时出现 HTTP 错误 400

python - 更改 tkinter 菜单栏的颜色

python - 平面图假设未评估

python - 使用 pytest 使单一假设案例失败?

python - 使用 pytest 和假设进行异常处理和测试

python - 如何查看Python的假设库的输出

python - 如何从 HTTP 响应接收 zip 文件

python - 插入/删除列表之间的距离

python - 通过自定义 AMI 获取 EC2 实例的 AWS CloudFormation 中的用户数据不起作用

python-hypothesis - 使用 pytest.raises 的假设状态测试不报告步骤顺序