By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,548 Members | 1,496 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,548 IT Pros & Developers. It's quick & easy.

Making the Zen of Python more useful

P: n/a
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
Jul 18 '05 #1
Share this Question
Share on Google+
10 Replies


P: n/a
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
Jul 18 '05 #2

P: n/a
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

Jul 18 '05 #3

P: n/a
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
Jul 18 '05 #4

P: n/a
In article <-5********************@powergate.ca>, pe***@engcorp.com says...

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


Hey, that's very slick! But ... (starting to feel like a whiner), my main
problem was with the automatic printing of the text upon import. Obviously,
that functionality needs to remain. It would just be nice if there was
some clean way of (sometimes) using the module without it.
Thanks again!

--
Andy

Jul 18 '05 #5

P: n/a
In article <c4**********@news-int.gatech.edu>, Andrew Henshaw wrote:
In article <-5********************@powergate.ca>, pe***@engcorp.com says...

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


Hey, that's very slick! But ... (starting to feel like a whiner), my main
problem was with the automatic printing of the text upon import. Obviously,
that functionality needs to remain. It would just be nice if there was
some clean way of (sometimes) using the module without it.


Personally, I'd just make a "zen" module which imports "this" after
redirecting stdout as described above, and give that to your students.

Joe
Jul 18 '05 #6

P: n/a
Andrew Henshaw wrote:
In article <-5********************@powergate.ca>, pe***@engcorp.com says...
my main
problem was with the automatic printing of the text upon import. Obviously,
that functionality needs to remain. It would just be nice if there was
some clean way of (sometimes) using the module without it.


I don't think your particular use case was known to the developers
when they implemented that module. Maybe you should suggest it as
an enhancement on Sourceforge. Be sure to include a patch! ;-)

-Peter
Jul 18 '05 #7

P: n/a
In article <ab********************@powergate.ca>, pe***@engcorp.com says...

Andrew Henshaw wrote:
In article <-5********************@powergate.ca>, pe***@engcorp.com says...
my main
problem was with the automatic printing of the text upon import. Obviously, that functionality needs to remain. It would just be nice if there was
some clean way of (sometimes) using the module without it.


I don't think your particular use case was known to the developers
when they implemented that module. Maybe you should suggest it as
an enhancement on Sourceforge. Be sure to include a patch! ;-)

-Peter


I'm sure that is true. Unfortunately, I can't write a patch since I can't
think of a way to provide both functions; except, by wrapping in another
module - which I mentioned in my original post and was repeated by Joe Mason.
It's not a big deal, obviously. It would just be nice if there was a way to
do both using the one module.

For my use, wrapping it in another module is okay. I just thought that since
we had the 'this' module as part of the standard distribution, and, I suspect,
that lots of coders have unittests that could use some line-oriented text,
then it would have reasonably broad applicability.
--
Andy

Jul 18 '05 #8

P: n/a
Andrew Henshaw wrote:
I'm sure that is true. Unfortunately, I can't write a patch since I can't
think of a way to provide both functions; except, by wrapping in another
module - which I mentioned in my original post and was repeated by Joe Mason.
It's not a big deal, obviously. It would just be nice if there was a way to
do both using the one module.


Maybe (in the spirit of a couple of days ago) what is needed is
*adverbial* keywords. Then you could do:

import this quietly

One could expand on that idea as well, such as to satisfy those who
are overly obsessed with performance issues:

import wxPython rapidly

or those with intermittent problems in their own code:

import flakymodule reliably

-Peter
Jul 18 '05 #9

P: n/a
In article <Gq********************@powergate.ca>, pe***@engcorp.com says...
Maybe (in the spirit of a couple of days ago) what is needed is
*adverbial* keywords. Then you could do:

import this quietly

One could expand on that idea as well, such as to satisfy those who
are overly obsessed with performance issues:

import wxPython rapidly

or those with intermittent problems in their own code:

import flakymodule reliably


:-)

I think I could make use of all of those examples.

--
Andy

Jul 18 '05 #10

P: n/a
Peter Hansen wrote on Fri, 02 Apr 2004 13:09:42 -0500:

<snip>
One could expand on that idea as well, such as to satisfy those who
are overly obsessed with performance issues:

import wxPython rapidly


Hey, that's a pretty neat idea :). A dummy 'rapidly' keyword would be worth
it even if just for the psychological effects it would have. But I'd
suggest to make it more versatile, not tie it to import: you should be
allowed to optimize bottlenecks by adding 'rapidly' to them!

--
Yours,

Andrei

=====
Real contact info (decode with rot13):
ce******@jnanqbb.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
gur yvfg, fb gurer'f ab arrq gb PP.
Jul 18 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.