You are not logged in.

#1 2006-05-18 22:56:53

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

less/more module for python?

I've been searching for the longest time to find a python module that behaves like less or more with no luck. Even if I was able to harness the viewer from help() would be great but I can't figure out where that thing is on my system.
Any ideas?

Offline

#2 2006-05-18 23:14:43

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: less/more module for python?

From pydoc.py:
pipepager and tempfilepager are constructed by passing os.environ['PAGER'] as the second arg.

def pipepager(text, cmd):
    """Page through text by feeding it to another program."""
    pipe = os.popen(cmd, 'w')
    try:
        pipe.write(text)
        pipe.close()
    except IOError:
        pass # Ignore broken pipes caused by quitting the pager program.

def tempfilepager(text, cmd):
    """Page through text by invoking a program on a temporary file."""
    import tempfile
    filename = tempfile.mktemp()
    file = open(filename, 'w')
    file.write(text)
    file.close()
    try:
        os.system(cmd + ' ' + filename)
    finally:
        os.unlink(filename)
def ttypager(text):
    """Page through text on a text terminal."""
    lines = split(plain(text), 'n')
    try:
        import tty
        fd = sys.stdin.fileno()
        old = tty.tcgetattr(fd)
        tty.setcbreak(fd)
        getchar = lambda: sys.stdin.read(1)
    except (ImportError, AttributeError):
        tty = None
        getchar = lambda: sys.stdin.readline()[:-1][:1]

    try:
        r = inc = os.environ.get('LINES', 25) - 1
        sys.stdout.write(join(lines[:inc], 'n') + 'n')
        while lines[r:]:
            sys.stdout.write('-- more --')
            sys.stdout.flush()
            c = getchar()

            if c in ['q', 'Q']:
                sys.stdout.write('r          r')
                break
            elif c in ['r', 'n']:
                sys.stdout.write('r          r' + lines[r] + 'n')
                r = r + 1
                continue
            if c in ['b', 'B', 'x1b']:
                r = r - inc - inc
                if r < 0: r = 0
            sys.stdout.write('n' + join(lines[r:r+inc], 'n') + 'n')
            r = r + inc

    finally:
        if tty:
            tty.tcsetattr(fd, tty.TCSAFLUSH, old)

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

Offline

#3 2006-05-19 00:43:09

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: less/more module for python?

well thats some old f'd up code.
I was hoping for something a little more elaborate but maybe I can do something with this.

Thanks smile

Offline

#4 2006-05-19 02:18:42

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: less/more module for python?

Looks like you want something like ttypager.  You can edit it, but it should work ok as a minipager

Offline

#5 2006-05-19 02:32:46

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: less/more module for python?

yeah, I was thinking of ttypager but running that code gives all sorts of errors I guess from old syntax (look at the use of join() and rc = in = line with a string subtracted from an int. I hacked it for a little while and got it running but I really wanted more of the functionality from more and less where /<keyword> and ?<keyword> work for example.

help() has everything I wan't, I just can't figure out how I can manipulate it for the use of random strings like: help('this stringnnext newlinenetcn'). If I could just find that file on my system if it exists...

Offline

#6 2006-05-19 03:55:10

phrakture
Arch Overlord
From: behind you
Registered: 2003-10-29
Posts: 7,879
Website

Re: less/more module for python?

help() is using less

vim /usr/lib/python2.4/pydoc.py

Offline

#7 2006-05-19 19:01:00

T-Dawg
Forum Fellow
From: Charlotte, NC
Registered: 2005-01-29
Posts: 2,736

Re: less/more module for python?

phrakture wrote:

help() is using less

vim /usr/lib/python2.4/pydoc.py

doh!  :oops:

Offline

Board footer

Powered by FluxBB