Making the Zen of Python more useful

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Andrew Henshaw

    Making the Zen of Python more useful

    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
  • Peter Hansen

    #2
    Re: Making the Zen of Python more useful

    Andrew Henshaw wrote:[color=blue]
    > 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).[/color]

    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:
    [color=blue][color=green][color=darkred]
    >>> import StringIO, sys
    >>> s = StringIO.String IO()
    >>> sys.stdout = s
    >>> import this
    >>> sys.stdout = sys.__stdout__
    >>> s.getvalue()[/color][/color][/color]
    "The Zen of Python, by Tim Peters\n\nBeaut iful is ...

    -Peter

    Comment

    • Andrew Henshaw

      #3
      Re: Making the Zen of Python more useful

      In article <BPWdnVtfl7ma5P DdRVn-sw@powergate.ca >, peter@engcorp.c om says...[color=blue]
      >
      >Andrew Henshaw wrote:[color=green]
      >> 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).[/color]
      >
      >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:
      >[color=green][color=darkred]
      > >>> import StringIO, sys
      > >>> s = StringIO.String IO()
      > >>> sys.stdout = s
      > >>> import this
      > >>> sys.stdout = sys.__stdout__
      > >>> s.getvalue()[/color][/color]
      >"The Zen of Python, by Tim Peters\n\nBeaut iful is ...
      >
      >-Peter[/color]

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

      Thanks.

      --
      Andy

      Comment

      • Peter Hansen

        #4
        Re: Making the Zen of Python more useful

        Andrew Henshaw wrote:
        [color=blue]
        > In article <BPWdnVtfl7ma5P DdRVn-sw@powergate.ca >, peter@engcorp.c om says...[color=green]
        >>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:
        >>[color=darkred]
        >>>>>import StringIO, sys
        >>>>>s = StringIO.String IO()
        >>>>>sys.stdo ut = s
        >>>>>import this
        >>>>>sys.stdo ut = sys.__stdout__
        >>>>>s.getvalue ()[/color]
        >>
        >>"The Zen of Python, by Tim Peters\n\nBeaut iful is ...[/color]
        >
        > Much better. Still pretty awkward for the purpose I described. Perhaps, the
        > best solution, overall.[/color]

        Okay then, how about this one? :-)
        [color=blue][color=green][color=darkred]
        >>> import this[/color][/color][/color]
        The Zen of Python, by Tim Peters\n\nBeaut iful is ...[color=blue][color=green][color=darkred]
        >>> this.s.decode(' rot-13')[/color][/color][/color]
        u"The Zen of Python, by Tim Peters\n\nBeaut iful is ...

        -Peter

        Comment

        • Andrew Henshaw

          #5
          Re: Making the Zen of Python more useful

          In article <-5ydnTEmxfuZHvDd RVn-iQ@powergate.ca >, peter@engcorp.c om says...[color=blue]
          >
          >Andrew Henshaw wrote:
          >[color=green]
          >> In article <BPWdnVtfl7ma5P DdRVn-sw@powergate.ca >, peter@engcorp.c om says...[color=darkred]
          >>>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.String IO()
          >>>>>>sys.stdou t = s
          >>>>>>import this
          >>>>>>sys.stdou t = sys.__stdout__
          >>>>>>s.getvalu e()
          >>>
          >>>"The Zen of Python, by Tim Peters\n\nBeaut iful is ...[/color]
          >>
          >> Much better. Still pretty awkward for the purpose I described. Perhaps,[/color][/color]
          the[color=blue][color=green]
          >> best solution, overall.[/color]
          >
          >Okay then, how about this one? :-)
          >[color=green][color=darkred]
          > >>> import this[/color][/color]
          >The Zen of Python, by Tim Peters\n\nBeaut iful is ...[color=green][color=darkred]
          > >>> this.s.decode(' rot-13')[/color][/color]
          >u"The Zen of Python, by Tim Peters\n\nBeaut iful is ...
          >
          >-Peter[/color]

          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

          Comment

          • Joe Mason

            #6
            Re: Making the Zen of Python more useful

            In article <c4k1hb$cdg$1@n ews-int.gatech.edu> , Andrew Henshaw wrote:[color=blue]
            > In article <-5ydnTEmxfuZHvDd RVn-iQ@powergate.ca >, peter@engcorp.c om says...[color=green]
            >>
            >>Andrew Henshaw wrote:
            >>[color=darkred]
            >>> In article <BPWdnVtfl7ma5P DdRVn-sw@powergate.ca >, peter@engcorp.c om says...
            >>>>I'm not sure why you had to do quite all that. The following is
            >>>>sufficien t, and you don't need a custom ROT-13 thingie:
            >>>>
            >>>>>>>import StringIO, sys
            >>>>>>>s = StringIO.String IO()
            >>>>>>>sys.stdo ut = s
            >>>>>>>import this
            >>>>>>>sys.stdo ut = sys.__stdout__
            >>>>>>>s.getval ue()
            >>>>
            >>>>"The Zen of Python, by Tim Peters\n\nBeaut iful is ...
            >>>
            >>> Much better. Still pretty awkward for the purpose I described. Perhaps,[/color][/color]
            > the[color=green][color=darkred]
            >>> best solution, overall.[/color]
            >>
            >>Okay then, how about this one? :-)
            >>[color=darkred]
            >> >>> import this[/color]
            >>The Zen of Python, by Tim Peters\n\nBeaut iful is ...[color=darkred]
            >> >>> this.s.decode(' rot-13')[/color]
            >>u"The Zen of Python, by Tim Peters\n\nBeaut iful is ...
            >>
            >>-Peter[/color]
            >
            > 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.[/color]

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

            Joe

            Comment

            • Peter Hansen

              #7
              Re: Making the Zen of Python more useful

              Andrew Henshaw wrote:
              [color=blue]
              > In article <-5ydnTEmxfuZHvDd RVn-iQ@powergate.ca >, peter@engcorp.c om 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.[/color]

              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

              Comment

              • Andrew Henshaw

                #8
                Re: Making the Zen of Python more useful

                In article <abqdneFj-dP8APDd4p2dnA@p owergate.ca>, peter@engcorp.c om says...[color=blue]
                >
                >Andrew Henshaw wrote:
                >[color=green]
                >> In article <-5ydnTEmxfuZHvDd RVn-iQ@powergate.ca >, peter@engcorp.c om says...
                >> my main
                >> problem was with the automatic printing of the text upon import.[/color][/color]
                Obviously,[color=blue][color=green]
                >> that functionality needs to remain. It would just be nice if there was
                >> some clean way of (sometimes) using the module without it.[/color]
                >
                >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[/color]

                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

                Comment

                • Peter Hansen

                  #9
                  Re: Making the Zen of Python more useful

                  Andrew Henshaw wrote:
                  [color=blue]
                  > 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.[/color]

                  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

                  Comment

                  • Andrew Henshaw

                    #10
                    Re: Making the Zen of Python more useful

                    In article <GqqdnTyLBpT7Mf DdRVn-jg@powergate.ca >, peter@engcorp.c om says...
                    [color=blue]
                    >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
                    >[/color]

                    :-)

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

                    --
                    Andy

                    Comment

                    • Andrei

                      #11
                      Re: Making the Zen of Python more useful

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

                      <snip>[color=blue]
                      > One could expand on that idea as well, such as to satisfy those who
                      > are overly obsessed with performance issues:
                      >
                      > import wxPython rapidly[/color]

                      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):
                      cebwrpg5@jnanqb b.ay. Fcnz-serr! Cyrnfr qb abg hfr va choyvp cbfgf. V ernq
                      gur yvfg, fb gurer'f ab arrq gb PP.

                      Comment

                      Working...