使Python的Zen更有用 [英] Making the Zen of Python more useful

查看:56
本文介绍了使Python的Zen更有用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

昨天,我正在为一个模块编写一些测试代码,我想要一些

面向行的文本用作数据源。我想过从其中一个标准模块中使用

docstring;但是,在我看来,来自''this''模块的Python文本的禅宗将会更好*,因为它更具可读性,并且会更好对我们的学生来说是一件好事。


不幸的是,实际的文字难以使用:

o文本是ROT-13编码和模块'的翻译例程是

inline

o导入时将文本打印到stdout。我理解Tim为什么会这样做,

但它确实干扰了我的目的。


所以,为了将模块用于我的测试代码,我有实现一个简短的

ROT-13转换器,导入sys模块,将stdout重定向到一个提供空写方法的简单

对象,导入它,恢复标准输出,以及

然后rot13(this.s)。


我建议''this''模块可用于提供标准,

面向行,测试文本。我希望看到未编码的文本直接可用,例如
,例如this.zen()。


不幸的是,我的建议有一个障碍。我想不出一个方便的方式

来提供''导入这个''功能,模块是or b
$ b or orininally创建的,而不强迫测试程序员通过重定向stdout的

回转。我们可以从另一个提供所需行为的模块中间接导入

;但是,这看起来像是b $ b矫枉过正。有什么想法吗?


-

Andy

解决方案

Andrew Henshaw写道:

昨天,我正在为一个模块编写一些测试代码,我想要一些
面向行的文本作为数据源使用。我想过使用其中一个标准模块的
文档字符串;但是,我突然意识到来自''this''模块的Python文本的Zen *会更好*,因为它更具可读性,对我们的学生来说是一件好事。不幸的是,实际文本很难使用:
o文本是ROT-13编码的,模块的翻译例程是内联的
o导入时将文本打印到stdout。我理解为什么蒂姆这样做了,
但确实干扰了我的目的。

所以,为了把模块用于我的测试代码,我不得不实现一个简短的
ROT-13转换器,导入sys模块,将stdout重定向到一个提供空写方法的简单对象,导入它,恢复标准输出,然后
然后rot13(this.s)。




我不知道你为什么要这么做。以下是

足够了,你不需要定制的ROT-13东西:

import StringIO,sys
s = StringIO.StringIO()
sys.stdout = s
导入此
sys.stdout = sys .__ stdout__
s.getvalue()



禅宗的Python,蒂姆·彼得斯\ n \\ n \ nnBeautiful是......


-Peter


在文章< BP ******************** @ powergate.ca>, pe***@engcorp.com 说...


Andrew Henshaw写道:

昨天,我正在为一个模块编写一些测试代码,我想要一些
面向行的文本用作数据源。我想过使用其中一个标准模块的
文档字符串;但是,我突然意识到来自''this''模块的Python文本的Zen *会更好*,因为它更具可读性,对我们的学生来说是一件好事。不幸的是,实际文本很难使用:
o文本是ROT-13编码的,模块的翻译例程是内联的
o导入时将文本打印到stdout。我理解为什么蒂姆这样做了,
但确实干扰了我的目的。

所以,为了把模块用于我的测试代码,我不得不实现一个简短的
ROT-13转换器,导入sys模块,将stdout重定向到一个提供空写方法的简单对象,导入它,恢复标准输出,然后
然后rot13(this.s)。



我不知道你为什么要这么做。以下是足够的,你不需要自定义的ROT-13东西:

import StringIO,sys
s = StringIO.StringIO()
sys.stdout = s
导入此
sys.stdout = sys .__ stdout__
s.getvalue()


禅宗的蟒蛇,由蒂姆·彼得斯\
n'nBeautiful是......

-Peter




好多了。我描述的目的仍然很尴尬。也许,总的来说,

最佳解决方案。


谢谢。


-

Andy


Andrew Henshaw写道:

在文章< BP ******* *************@powergate.ca> ;, pe***@engcorp.com 说。 ..

我不知道你为什么要这么做。以下是足够的,你不需要自定义的ROT-13东西:

> import StringIO,sys
> s = StringIO .StringIO()
> sys.stdout = s
>导入此
> sys.stdout = sys .__ stdout__
> s.getvalue()


禅宗的荆棘,由蒂姆·彼得斯提出来了......



好多了。我描述的目的仍然很尴尬。也许,最好的解决方案,总体而言。




那么,这个怎么样? :-)

import this
Python的禅宗,由Tim Peters提供。 nBeautiful是... this.s.decode(''rot-13'')



u禅宗的Python,由Tim Peters提供\\ n \ nBeautiful是...


-Peter


Yesterday, I was writing some test code for a module and I wanted some
line-oriented text to use as a data source. I thought about using a
docstring from one of the standard modules; but, it occurred to me that the
Zen of Python text from the ''this'' module would be *much* better, as it is
more readable and would be a nice thing for our students to see.

Unfortunately, the actual text is difficult to use:
o the text is ROT-13 encoded and the module''s translation routine is
inline
o the text is printed to stdout on import. I understand why Tim did this,
but it did interfere with my purpose.

So, in order to use the module for my test code, I had to implement a short
ROT-13 translator, import the sys module, redirect stdout to a simple
object that provided a null write method, import this, restore stdout, and
then rot13(this.s).

I suggest that the ''this'' module could be used to provide a standard,
line-oriented, test text. I would like to see the unencoded text made
available directly, e.g. this.zen().

Unfortunately, my suggestion has a snag. I can''t think of a convenient way
to provide the ''import this'' functionality for which the module was
oringinally created, without forcing the test programmer to go through the
gyrations of redirecting stdout. We could do an indirect import from
another module that provides the desired behavior; but, this seems like
overkill. Any thoughts?

--
Andy

解决方案

Andrew Henshaw wrote:

Yesterday, I was writing some test code for a module and I wanted some
line-oriented text to use as a data source. I thought about using a
docstring from one of the standard modules; but, it occurred to me that the
Zen of Python text from the ''this'' module would be *much* better, as it is
more readable and would be a nice thing for our students to see.

Unfortunately, the actual text is difficult to use:
o the text is ROT-13 encoded and the module''s translation routine is
inline
o the text is printed to stdout on import. I understand why Tim did this,
but it did interfere with my purpose.

So, in order to use the module for my test code, I had to implement a short
ROT-13 translator, import the sys module, redirect stdout to a simple
object that provided a null write method, import this, restore stdout, and
then rot13(this.s).



I''m not sure why you had to do quite all that. The following is
sufficient, and you don''t need a custom ROT-13 thingie:

import StringIO, sys
s = StringIO.StringIO()
sys.stdout = s
import this
sys.stdout = sys.__stdout__
s.getvalue()


"The Zen of Python, by Tim Peters\n\nBeautiful is ...

-Peter


In article <BP********************@powergate.ca>, pe***@engcorp.com says...


Andrew Henshaw wrote:

Yesterday, I was writing some test code for a module and I wanted some
line-oriented text to use as a data source. I thought about using a
docstring from one of the standard modules; but, it occurred to me that the
Zen of Python text from the ''this'' module would be *much* better, as it is
more readable and would be a nice thing for our students to see.

Unfortunately, the actual text is difficult to use:
o the text is ROT-13 encoded and the module''s translation routine is
inline
o the text is printed to stdout on import. I understand why Tim did this,
but it did interfere with my purpose.

So, in order to use the module for my test code, I had to implement a short
ROT-13 translator, import the sys module, redirect stdout to a simple
object that provided a null write method, import this, restore stdout, and
then rot13(this.s).



I''m not sure why you had to do quite all that. The following is
sufficient, and you don''t need a custom ROT-13 thingie:

import StringIO, sys
s = StringIO.StringIO()
sys.stdout = s
import this
sys.stdout = sys.__stdout__
s.getvalue()


"The Zen of Python, by Tim Peters\n\nBeautiful is ...

-Peter



Much better. Still pretty awkward for the purpose I described. Perhaps, the
best solution, overall.

Thanks.

--
Andy


Andrew Henshaw wrote:

In article <BP********************@powergate.ca>, pe***@engcorp.com says...

I''m not sure why you had to do quite all that. The following is
sufficient, and you don''t need a custom ROT-13 thingie:

>import StringIO, sys
>s = StringIO.StringIO()
>sys.stdout = s
>import this
>sys.stdout = sys.__stdout__
>s.getvalue()



"The Zen of Python, by Tim Peters\n\nBeautiful is ...



Much better. Still pretty awkward for the purpose I described. Perhaps, the
best solution, overall.



Okay then, how about this one? :-)

import this The Zen of Python, by Tim Peters\n\nBeautiful is ... this.s.decode(''rot-13'')


u"The Zen of Python, by Tim Peters\n\nBeautiful is ...

-Peter


这篇关于使Python的Zen更有用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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