2012-03-26 82 views
1

對於此文檔測試:Python的文檔測試字符串轉義問題

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' 

我怎樣才能得到這兩個字符串在文檔測試匹配?

回答

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 
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 
'''