Making the Zen of Python more useful

A

Andrew Henshaw

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?
 
P

Peter Hansen

Andrew said:
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:
"The Zen of Python, by Tim Peters\n\nBeautiful is ...

-Peter
 
A

Andrew Henshaw

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:

"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.
 
P

Peter Hansen

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

Okay then, how about this one? :)
u"The Zen of Python, by Tim Peters\n\nBeautiful is ...

-Peter
 
A

Andrew Henshaw

Okay then, how about this one? :)

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!
 
J

Joe Mason

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
 
P

Peter Hansen

Andrew said:
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
 
A

Andrew Henshaw

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.
 
P

Peter Hansen

Andrew said:
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
 
A

Andrew Henshaw

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.
 
A

Andrei

Peter Hansen wrote on Fri, 02 Apr 2004 13:09:42 -0500:

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):
(e-mail address removed). Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
gur yvfg, fb gurer'f ab arrq gb PP.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top