Making the Zen of Python more useful

Discussion in 'Python' started by Andrew Henshaw, Apr 2, 2004.

  1. 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, Apr 2, 2004
    #1
    1. Advertising

  2. Andrew Henshaw

    Peter Hansen Guest

    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
     
    Peter Hansen, Apr 2, 2004
    #2
    1. Advertising

  3. In article <>, 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, Apr 2, 2004
    #3
  4. Andrew Henshaw

    Peter Hansen Guest

    Andrew Henshaw wrote:

    > In article <>, 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
     
    Peter Hansen, Apr 2, 2004
    #4
  5. In article <>, says...
    >
    >Andrew Henshaw wrote:
    >
    >> In article <>, 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
     
    Andrew Henshaw, Apr 2, 2004
    #5
  6. Andrew Henshaw

    Joe Mason Guest

    In article <c4k1hb$cdg$>, Andrew Henshaw wrote:
    > In article <>, says...
    >>
    >>Andrew Henshaw wrote:
    >>
    >>> In article <>, 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
     
    Joe Mason, Apr 2, 2004
    #6
  7. Andrew Henshaw

    Peter Hansen Guest

    Andrew Henshaw wrote:

    > In article <>, 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
     
    Peter Hansen, Apr 2, 2004
    #7
  8. In article <>, says...
    >
    >Andrew Henshaw wrote:
    >
    >> In article <>, 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
     
    Andrew Henshaw, Apr 2, 2004
    #8
  9. Andrew Henshaw

    Peter Hansen Guest

    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
     
    Peter Hansen, Apr 2, 2004
    #9
  10. In article <>, 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
     
    Andrew Henshaw, Apr 2, 2004
    #10
  11. Andrew Henshaw

    Andrei Guest

    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):
    . Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
    gur yvfg, fb gurer'f ab arrq gb PP.
     
    Andrei, Apr 2, 2004
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Dave
    Replies:
    4
    Views:
    333
    Peter Hansen
    Apr 3, 2004
  2. Li Daobing
    Replies:
    2
    Views:
    2,863
    Skip Montanaro
    May 19, 2004
  3. Tim Peters
    Replies:
    1
    Views:
    394
    Steve Williams
    May 20, 2004
  4. Mark Jackson
    Replies:
    2
    Views:
    290
    Mark Jackson
    Jun 4, 2004
  5. Timothy Fitz

    Zen of Python

    Timothy Fitz, Jan 19, 2005, in forum: Python
    Replies:
    6
    Views:
    400
    Antoon Pardon
    Jan 20, 2005
Loading...

Share This Page