Making the Zen of Python more useful

Andrew Henshaw andrew.henshaw at gtri.gatech.edu
Fri Apr 2 10:03:31 EST 2004


In article <BPWdnVtfl7ma5PDdRVn-sw at powergate.ca>, peter at 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




More information about the Python-list mailing list