文档测试中的字符串引用问题
当我在不同的 Python 版本(2.5 与 2.6)和不同的平台(FreeBSD 与 Mac OS)上运行 doctests 时,字符串的引用方式不同:
Failed example:
decode('{"created_by":"test","guid":123,"num":5.00}')
Expected:
{'guid': 123, 'num': Decimal("5.00"), 'created_by': 'test'}
Got:
{'guid': 123, 'num': Decimal('5.00'), 'created_by': 'test'}
因此,在一个盒子上 repr(decimal.Decimal('5.00')) 结果为 'Decimal("5.00" )' 在“Decimal('5.00')”中的另一个。 有没有办法在不创建更复杂的测试逻辑的情况下解决这个问题?
When I run doctests on different Python versions (2.5 vs 2.6) and different plattforms (FreeBSD vs Mac OS) strings get quoted differently:
Failed example:
decode('{"created_by":"test","guid":123,"num":5.00}')
Expected:
{'guid': 123, 'num': Decimal("5.00"), 'created_by': 'test'}
Got:
{'guid': 123, 'num': Decimal('5.00'), 'created_by': 'test'}
So on one box repr(decimal.Decimal('5.00')) results in 'Decimal("5.00")' on the other in "Decimal('5.00')". Is there any way to get arround the issue withyout creating more compliated test logic?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这实际上是因为
decimal
模块的源代码发生了变化:在python 2.4和python2.5中,decimal.Decimal.__repr__
函数包含:而在python2.6中它包含:
因此,在这种情况下,最好的办法就是打印出结果的
str()
并在必要时单独检查类型......This is actually because the
decimal
module's source code has changed: In python 2.4 and python2.5 thedecimal.Decimal.__repr__
function contains:whereas in python2.6 it contains:
So in this case the best thing to do is just to print out
str()
of the result and check the type separately if necessary...在 David Fraser 的点击之后,我发现了此建议,由 Raymond Hettinger 在 Python 邮件列表上提出。
我现在使用这样的东西:
Following the hits by David Fraser i found this suggestion by Raymond Hettinger on the Python mailinglist.
I now use something like this: