将doctest与gettext _()混合的问题 [英] Problem with mixing doctest with gettext _()

查看:74
本文介绍了将doctest与gettext _()混合的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当这些

模块使用gettext _('''''')具有国际化字符串时,我在使用doctest编写自测模块时遇到问题。


- 应用程序的主模块(比如app.py)调用gettext.install()

来安装Python内置的特殊_函数。其他模块,

取自通用的Python模块集合,也支持

国际化和doctest测试。


例如:


utstring.py会包含这样的代码:


def onOffStr(isOn):

" ;""返回ON字符串表示True,OFF表示假的。


**示例**

onOffStr(真)
u''ON''onOffStr(False)
u''OFF''



""

if isnn:

return _(uON)#注意下划线

else:

return _(u" OFF")

utstring模块不会调用任何gettext调用,因为有些

其他模块在应用程序时执行运行。所以doctest失败了:


********************************* ***************** ***************

示例失败:onOffStr(True)

异常引发:

回溯(最近一次调用最后一次):

文件C:\Python23 \Lib\doctest.py,第442行,在_run_examples_inner中

compileflags,1)in globs

文件"< string> ;",第1行,在?

文件" C:\ dev \ python \utstring.py",第513行,在onOffStr

返回_ (uON)

TypeError:''tuple''对象不可调用

***************** ********************************* *************** />

我在使用下面的代码进行测试时尝试定义一个_()函数但

doctest仍然失败。以下代码位于我的

utstring.py模块的末尾

def _test():

"" " _test()执行文档字符串测试""


导入doctest,utstring

返回doctest.testmod(utstring)


if __name__ ==" __ main __":

def _(aString):

#dummy _()试图让doctest通过。

返回aString


_test()

----------


当我定义虚拟_()

函数时,有谁知道为什么doctest仍然失败?

提前谢谢。


Pierre

解决方案



Pierre>我试着用代码测​​试时定义一个_()函数

Pierre>下面但doctest仍然失败。以下代码位于

Pierre>我的utstring.py模块的结尾

...


我怀疑这是因为你的假_不在内置。为什么不直接调用

gettext.install(),你当前正在定义_?


Skip

< br>

Skip Montanaro写道:

Pierre>我试着用代码测​​试时定义一个_()函数
Pierre>下面但doctest仍然失败。以下代码在
Pierre>我的utstring.py模块结束


我怀疑这是因为你的假_不在内置。为什么不直接调用
gettext.install(),你当前正在定义_?




跳过,这看起来像是要走的路。


我试图避免它,因为这些库的翻译文件

模块是在其他地方的应用程序级别构建的。但我还是在考虑为这些模块创建一个翻译库,所以

我想这是另一个动机...

谢谢


Pierre


我怀疑这是因为你的假_不在内置。为什么不直接调用gettext.install()来定义_?


皮埃尔>跳过,这看起来像是要走的路。


皮埃尔>我试图避免它,因为这些翻译文件

Pierre>库模块在应用程序级别构建

Pierre>别的地方。但我也在考虑创建一个

Pierre>这些模块的翻译库,所以我猜是一个

皮埃尔>这样做更有动力......


如果你真的想要一个假_(),你也可以将你的版本填入

内置:
进口__builtin__
DEF FOO(S):返回小号
... __builtin __._ = FOO
_
将函数foo在0x1d6670> _(" hi")



''hi''


Skip


I have a problem writing self-testable modules using doctest when these
modules have internationalized strings using gettext _(''...'').

- The main module of an application (say app.py) calls gettext.install()
to install the special _ function inside Python builtin. Other modules,
taken from a general purpose collection of Python modules, also support
internationalisation and doctest testing.

For example:

utstring.py would contain some code like this:

def onOffStr(isOn) :
"""Return the "ON" string for True, "OFF" for False.

**Example**

onOffStr(True) u''ON'' onOffStr(False) u''OFF''


"""
if isOn:
return _(u"ON") # notice the underscore
else:
return _(u"OFF")

The utstring module does not call any of the gettext calls, because some
other module does it when the application runs. So the doctest fails:

************************************************** ***************
Failure in example: onOffStr(True)
from line #4 of utstring.onOffStr
Exception raised:
Traceback (most recent call last):
File "C:\Python23\Lib\doctest.py", line 442, in _run_examples_inner
compileflags, 1) in globs
File "<string>", line 1, in ?
File "C:\dev\python\utstring.py", line 513, in onOffStr
return _(u"ON")
TypeError: ''tuple'' object is not callable
************************************************** ***************

I tried to define a _() function when testing with the code below but
the doctest still fails. The following code is at the end of my
utstring.py module

def _test():
"""_test() perform docstring test"""

import doctest, utstring
return doctest.testmod(utstring)

if __name__ == "__main__":
def _(aString):
# dummy _() attempting to get doctest to pass.
return aString

_test()
----------

Does anyone know why the doctest still fails when I define the dummy _()
function?
Thanks in advance.

Pierre

解决方案


Pierre> I tried to define a _() function when testing with the code
Pierre> below but the doctest still fails. The following code is at the
Pierre> end of my utstring.py module
...

I suspect it''s because your dummy _ is not in builtins. Why not just call
gettext.install() where you are currently defining _?

Skip


Skip Montanaro wrote:

Pierre> I tried to define a _() function when testing with the code
Pierre> below but the doctest still fails. The following code is at the
Pierre> end of my utstring.py module
...

I suspect it''s because your dummy _ is not in builtins. Why not just call
gettext.install() where you are currently defining _?



Skip, this looks like the way to go.

I was trying to avoid it because the translation files for these library
modules are constructed at the application level somewhere else. But I
was also considering creating a translation library for these module, so
I guess that is one more incentive to do so...

Thanks

Pierre


I suspect it''s because your dummy _ is not in builtins. Why not just
call gettext.install() where you are currently defining _?
Pierre> Skip, this looks like the way to go.

Pierre> I was trying to avoid it because the translation files for these
Pierre> library modules are constructed at the application level
Pierre> somewhere else. But I was also considering creating a
Pierre> translation library for these module, so I guess that is one
Pierre> more incentive to do so...

If you really want a dummy _() you can also stuff your version into
builtins:

import __builtin__
def foo(s): return s ... __builtin__._ = foo
_ <function foo at 0x1d6670> _("hi")


''hi''

Skip


这篇关于将doctest与gettext _()混合的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆