Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何使用假设从给定列表生成可变大小的列表?_Python_Python Hypothesis - Fatal编程技术网

Python 如何使用假设从给定列表生成可变大小的列表?

Python 如何使用假设从给定列表生成可变大小的列表?,python,python-hypothesis,Python,Python Hypothesis,对于基于属性的测试,给定一个固定的值列表,我需要生成一个大小可变的列表,其中顺序很重要,并且允许重复。例如,如果我的固定列表是 texts = ['t1', 't2', 't3', 't4'] 我希望生成不同的变体,例如 ['t2'] ['t4', 't1'] # Subset and different order [] ['t3', 't1', 't2'] # Different order ['t4', 't4', 't4', 't1'] # Repetition of t4 ['t1

对于基于属性的测试,给定一个固定的值列表,我需要生成一个大小可变的列表,其中顺序很重要,并且允许重复。例如,如果我的固定列表是

texts = ['t1', 't2', 't3', 't4']

我希望生成不同的变体,例如

['t2']
['t4', 't1'] # Subset and different order
[]
['t3', 't1', 't2'] # Different order
['t4', 't4', 't4', 't1'] # Repetition of t4
['t1', 't2', 't1'] # Repetition but at different location
['t1', 't2']
['t2', 't1'] # different order from the one above and considered different.

我目前使用的是
排列
策略

from hypothesis import given, strategies as st

@given(st.permutations(texts))
def test_x(some_text):
   ...
   pass

但这并没有给我可变的大小,重复

其他要求:

  • 如何指定最大变量列表为20

由于您最多需要20个项目,请生成1到20之间的随机数:

import random
size = random.randint(1,20)
然后使用该数字从源列表中选择N个独立选项:

texts = ['t1', 't2', 't3', 't4']
random_texts = []
for _ in range(size):
    random_texts.append(random.choice(texts))

由于您最多需要20个项目,请生成1到20之间的随机数:

import random
size = random.randint(1,20)
然后使用该数字从源列表中选择N个独立选项:

texts = ['t1', 't2', 't3', 't4']
random_texts = []
for _ in range(size):
    random_texts.append(random.choice(texts))

您正在寻找和策略的组合:

from hypothesis import strategies as st

texts = ['t1', 't2', 't3', 't4']
lists_from_texts = st.lists(st.sampled_from(texts), max_size=20)

...

@given(lists_from_texts)
def test_x(some_text):
    ...
或者,如果您希望能够更改不同测试的源列表:

from typing import List


def lists_from_texts(source: List[str]) -> st.SearchStrategy[List[str]]:
    return st.lists(st.sampled_from(source), max_size=20)

...

@given(lists_from_texts(texts))
def test_x(some_text):
    ...

您正在寻找和策略的组合:

from hypothesis import strategies as st

texts = ['t1', 't2', 't3', 't4']
lists_from_texts = st.lists(st.sampled_from(texts), max_size=20)

...

@given(lists_from_texts)
def test_x(some_text):
    ...
或者,如果您希望能够更改不同测试的源列表:

from typing import List


def lists_from_texts(source: List[str]) -> st.SearchStrategy[List[str]]:
    return st.lists(st.sampled_from(source), max_size=20)

...

@given(lists_from_texts(texts))
def test_x(some_text):
    ...

列表(从(文本)中采样,最大大小=20)
?我只是不知道你所说的“订单很重要”是什么意思。你能澄清一下吗?@Georgy谢谢!我的意思是策略应该服务于
['t1','t2']
以及
[“t2”,“t1”]
即排列。
列表(从(文本)中取样,最大大小=20)
?我只是不知道你所说的“订单很重要”是什么意思。你能澄清一下吗?@Georgy谢谢!我的意思是策略应该服务于
['t1',t2']
以及
[“t2”,“t1”]
即排列。我希望我也能选择这个答案。虽然我使用这个问题作为学习python假设的一种方法,但您向我展示了一些我也可以在其他地方使用的东西。谢谢我希望我也能选择这个答案。虽然我使用这个问题作为学习python假设的一种方法,但您向我展示了一些我也可以在其他地方使用的东西。谢谢向上投票。