[Python-ideas] Cleaner separation of help() and interactive help.

Ron Adam rrr at ronadam.com
Tue Feb 8 02:50:55 CET 2011



On 02/07/2011 04:08 PM, Steven D'Aprano wrote:
> Greg Ewing wrote:
>> Ron Adam wrote:
>>
>>> It also can be annoying (to me) when I use help and the result is
>>> cleared after the pager is done with it.
>>
>> Yeah, that annoys me too. I can't imagine why anyone thought it was
>> a good idea to design a pager that way. Come to think of it, the
>> whole concept of a pager is something of an anti-feature nowadays,
>> when most people's "terminal" is really a gui window with its own
>> scrolling facilities that are considerably better than the pager's.

Ok, I'm not the only one. ;-)


> Do you have a reliable source for that claim about "most" people that is
> relevant to Python coders? We're not all using Microsoft VisualStudio :)
>
> This is open source, and people scratch their own itch. I daresay help()
> was written to suit the working processes of the creator. That suits me
> fine, because I like help() just the way it is, I like the pager just the
> way it is, and I don't want it to change. So -1 on any change to the
> default behaviour.

I don't want to remove it.  Just change where it lives and make non-paged 
output easy to get.  So I'm looking for the best way's to do that, and then 
of out those ways, find the one with the least objections. :-)


> What you're describing *is* a pager. The default pager is the (almost)
> lowest common denominator which should work for anyone anywhere. (Possibly
> not if they're using a teletype.) Perhaps help() should come with some more
> pagers and an easy interface for setting which one is used.

I'm not sure what you mean by default pager. I guess that would be the 
plainpager. Which is just writing to the stdout. But it's not what you get 
if anything else works.

The way it works, is when you first call help, it calls the pager function 
that then tries to create a pager from various options including looking at 
on environment variables PAGER and TERM.

It's easier to post the code so you can see for yourself.  Look in pydoc 
for the rest of it.  In any case, I'm not proposing any changes to the pager.

Cheers,
    Ron




def pager(text):
     """The first time this is called, determine what kind of pager to use."""
     global pager
     pager = getpager()
     pager(text)

def getpager():
     """Decide what method to use for paging through text."""
     if not hasattr(sys.stdout, "isatty"):
         return plainpager
     if not sys.stdin.isatty() or not sys.stdout.isatty():
         return plainpager
     if 'PAGER' in os.environ:
         if sys.platform == 'win32': # pipes completely broken in Windows
             return lambda text: tempfilepager(plain(text), 
os.environ['PAGER'])
         elif os.environ.get('TERM') in ('dumb', 'emacs'):
             return lambda text: pipepager(plain(text), os.environ['PAGER'])
         else:
             return lambda text: pipepager(text, os.environ['PAGER'])
     if os.environ.get('TERM') in ('dumb', 'emacs'):
         return plainpager
     if sys.platform == 'win32' or sys.platform.startswith('os2'):
         return lambda text: tempfilepager(plain(text), 'more <')
     if hasattr(os, 'system') and os.system('(less) 2>/dev/null') == 0:
         return lambda text: pipepager(text, 'less')

     import tempfile
     (fd, filename) = tempfile.mkstemp()
     os.close(fd)
     try:
         if hasattr(os, 'system') and os.system('more "%s"' % filename) == 0:
             return lambda text: pipepager(text, 'more')
         else:
             return ttypager
     finally:
         os.unlink(filename)


[skip  various pager alternatives ....]


def plainpager(text):
     """Simply print unformatted text.  This is the ultimate fallback."""
     sys.stdout.write(plain(text))










More information about the Python-ideas mailing list