Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Making the Zen of Python more useful

Reply
Thread Tools

Making the Zen of Python more useful

 
 
Andrew Henshaw
Guest
Posts: n/a
 
      04-02-2004
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
 
Reply With Quote
 
 
 
 
Peter Hansen
Guest
Posts: n/a
 
      04-02-2004
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
 
Reply With Quote
 
 
 
 
Andrew Henshaw
Guest
Posts: n/a
 
      04-02-2004
In article <(E-Mail Removed)>, http://www.velocityreviews.com/forums/(E-Mail Removed) 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

 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      04-02-2004
Andrew Henshaw wrote:

> In article <(E-Mail Removed)>, (E-Mail Removed) 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
 
Reply With Quote
 
Andrew Henshaw
Guest
Posts: n/a
 
      04-02-2004
In article <(E-Mail Removed)>, (E-Mail Removed) says...
>
>Andrew Henshaw wrote:
>
>> In article <(E-Mail Removed)>, (E-Mail Removed) 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.


Thanks again!

--
Andy

 
Reply With Quote
 
Joe Mason
Guest
Posts: n/a
 
      04-02-2004
In article <c4k1hb$cdg$(E-Mail Removed)>, Andrew Henshaw wrote:
> In article <(E-Mail Removed)>, (E-Mail Removed) says...
>>
>>Andrew Henshaw wrote:
>>
>>> In article <(E-Mail Removed)>, (E-Mail Removed) 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
 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      04-02-2004
Andrew Henshaw wrote:

> In article <(E-Mail Removed)>, (E-Mail Removed) 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
 
Reply With Quote
 
Andrew Henshaw
Guest
Posts: n/a
 
      04-02-2004
In article <(E-Mail Removed)>, (E-Mail Removed) says...
>
>Andrew Henshaw wrote:
>
>> In article <(E-Mail Removed)>, (E-Mail Removed) 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

 
Reply With Quote
 
Peter Hansen
Guest
Posts: n/a
 
      04-02-2004
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
 
Reply With Quote
 
Andrew Henshaw
Guest
Posts: n/a
 
      04-02-2004
In article <(E-Mail Removed)>, (E-Mail Removed) 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
useful setting in device manager (may be useful to know!) jameshanley39@yahoo.co.uk Computer Information 2 07-07-2008 04:28 PM
creative zen/zen micro Thomas Computer Information 0 12-24-2004 04:58 AM
RE: What's the meaning of Dutch in "The Zen of Python" Tim Peters Python 1 05-20-2004 03:11 AM
What's the meaning of Dutch in "The Zen of Python" Li Daobing Python 2 05-19-2004 01:33 PM
Re: Making the Zen of Python more useful Dave Python 4 04-03-2004 04:07 AM



Advertisments