Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.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 当涉及无序输出时,防止doctest错误失败_Python_Python 3.4_Doctest_Unordered - Fatal编程技术网

Python 当涉及无序输出时,防止doctest错误失败

Python 当涉及无序输出时,防止doctest错误失败,python,python-3.4,doctest,unordered,Python,Python 3.4,Doctest,Unordered,假设我们有以下函数: def f(x): """ Turns x into a set. >>> given_x = ['a', 'b', 'c', 'd', 'e', 'f'] >>> f(given_x) {'a', 'b', 'c', 'd', 'e', 'f'} """ return set(x) 运行doctest(通常)会导致以下情况: Failure ****************

假设我们有以下函数:

def f(x):
    """
    Turns x into a set.

    >>> given_x = ['a', 'b', 'c', 'd', 'e', 'f']
    >>> f(given_x)
    {'a', 'b', 'c', 'd', 'e', 'f'}

    """

    return set(x)
运行doctest(通常)会导致以下情况:

Failure
**********************************************************************
File "/home/black/Dev/exp_2.py", line 6, in f
Failed example:
    f(given_x)
Expected:
    {'a', 'b', 'c', 'd', 'e', 'f'}
Got:
    {'d', 'e', 'f', 'c', 'a', 'b'}
显然,这个失败不应该发生,因为函数按预期工作,但它确实发生了,因为结果是无序的


我的实际函数的输出可能比这复杂得多。它可以是一个包含有dict、set和list的dict

我需要一个通用的解决方案(如果有)。仅对所示示例执行
sort()
,并不能解决我的实际问题

问题:

当涉及无序输出时,如何防止doctest(错误地)失败?

python集合不能保证顺序,因此您不能依赖它

我会强迫你拥有你可以信任的东西:

>>> given_x = ['z','a', 'a', 'b', 'c', 'd', 'e', 'f']
>>> type(f(given_x))
<type 'dict'>
>>> sorted(list(f(given(x))) 
['a', 'b', 'c', 'd', 'e', 'f','z']
>>给定的_x=['z','a','a','b','c','d','e','f']
>>>类型(f(给定x))
>>>已排序(列表(f)(给定(x)))
['a','b','c','d','e','f','z']

我测试它是否具有预期的类型,它是否确实实现了集合的“唯一性”,结果是我所期望的,因此不能保证python集合的顺序

我会强迫你拥有你可以信任的东西:

>>> given_x = ['z','a', 'a', 'b', 'c', 'd', 'e', 'f']
>>> type(f(given_x))
<type 'dict'>
>>> sorted(list(f(given(x))) 
['a', 'b', 'c', 'd', 'e', 'f','z']
>>给定的_x=['z','a','a','b','c','d','e','f']
>>>类型(f(给定x))
>>>已排序(列表(f)(给定(x)))
['a','b','c','d','e','f','z']

我测试它是否具有预期类型,它是否确实实现了集合的“唯一性”,结果是我所期望的结果,为什么不将预期输出上移,以便测试是否相等,预期输出是否为“真”

输出:

Trying:
    given_x = ['a', 'b', 'c', 'd', 'e', 'f']
Expecting nothing
ok
Trying:
    f(given_x) == {'a', 'b', 'c', 'd', 'e', 'f'}
Expecting:
    True
ok

为什么不将预期输出向上移动,以便测试是否相等,并将预期输出设置为“True”

输出:

Trying:
    given_x = ['a', 'b', 'c', 'd', 'e', 'f']
Expecting nothing
ok
Trying:
    f(given_x) == {'a', 'b', 'c', 'd', 'e', 'f'}
Expecting:
    True
ok

很抱歉,我应该提到我的
f()
输出要复杂得多。有关更多详细信息,请参阅更新的问题。我正在寻找一般解决方案(如果有),而不是问题中示例的特定解决方案。很抱歉,我应该提到我的
f()
输出要复杂得多。有关更多详细信息,请参阅更新的问题。我正在寻找通用解决方案(如果有),不是特定于问题中示例的解决方案。此解决方案适用于对象,但不适用于测试返回字符串的函数。实际情况:生成html输出。html不关心属性顺序,因此在dict()中表示标记属性是合乎逻辑的,以便以后进行渲染。尝试对render()进行doctest时函数字符串中的属性顺序是不可预测的。此解决方案适用于对象,但不适用于测试返回字符串的函数。实际情况:生成html输出。html不关心属性顺序,因此在dict()中表示标记属性是合乎逻辑的,以便以后进行渲染。尝试doctest render()时函数字符串中的属性顺序不可预测。