1

对于这个 doctest:

r'''
>>> uuid_hex_to_binary('8ed2d35f-2911-4c10-ad68-587c96b4686e')
'\x8e\xd2\xd3\x5f\x29\x11\x4c\x10\xad\x68\x58\x7c\x96\xb4\x68\x6e'
'''

我得到这个结果:

Failed example:
    uuid_hex_to_binary('8ed2d35f-2911-4c10-ad68-587c96b4686e')
Expected:
    '\x8e\xd2\xd3\x5f\x29\x11\x4c\x10\xad\x68\x58\x7c\x96\xb4\x68\x6e'
Got:
    '\x8e\xd2\xd3_)\x11L\x10\xadhX|\x96\xb4hn'

测试应该通过,因为字符串是等价的。但是,在“Got:”字符串中,它已将一些\xHH转义转换为相应的 ascii 字符,但对于“Expected:”字符串并没有这样做。

如果我在文档字符串的请求下更改r'''''',我会改为:

Failed example:
    uuid_hex_to_binary('8ed2d35f-2911-4c10-ad68-587c96b4686e')
Expected:
    '???_)L?hX|??hn'
Got:
    '\x8e\xd2\xd3_)\x11L\x10\xadhX|\x96\xb4hn'

如何让两个字符串在 doctest 中匹配?

4

2 回答 2

0

哎呀,我问了10秒后才弄明白。我让它像这样工作:

r'''
>>> a = uuid_hex_to_binary('8ed2d35f-2911-4c10-ad68-587c96b4686e')
>>> b = '\x8e\xd2\xd3\x5f\x29\x11\x4c\x10\xad\x68\x58\x7c\x96\xb4\x68\x6e'
>>> a == b
True
'''
于 2012-03-26T08:52:30.557 回答
0

考虑一下:

>>> '\x8e\xd2\xd3\x5f\x29\x11\x4c\x10\xad\x68\x58\x7c\x96\xb4\x68\x6e'
'\x8e\xd2\xd3_)\x11L\x10\xadhX|\x96\xb4hn'

'\x5f'( ) 之类的字符'_'具有可打印的 ASCII 值,因此在repr()调用中它们被转换为短格式。这不是你想要的,所以如果你想将它与完整版进行比较,你需要类似的东西

>>> uuid_hex_to_binary('8ed2d35f-2911-4c10-ad68-587c96b4686e') == \
... '\x8e\xd2\xd3\x5f\x29\x11\x4c\x10\xad\x68\x58\x7c\x96\xb4\x68\x6e'
True
于 2012-03-26T09:43:22.540 回答