[IPython-user] [Python-Dev] a quit that actually quits

Walter Dörwald walter at livinglogic.de
Tue Jan 3 08:28:57 CST 2006


Ville Vainio wrote:

> On 1/3/06, Walter Dörwald <walter at livinglogic.de> wrote:
> 
>>>BTW, is the curses even necessary for this task? Why not just dump the
>>>stuff directly to stdout?
>>
>>That's another option of course. But moving backwards in the output is
>>impossible (or at least ackward) in this version.
> 
> I think old versions of "more" did this tolerably... it doesn't look
> good on screen (because it scrolls down while you are actually moving
> up) but the ease of implementation (and fast redraw these days) eases
> the pain a bit.

OK, so should we pipe the data to more or implement our own 
ouput/keyboard handling?

>>Well if you want to give the user the option to go back to the first
>>object, you'd have to tee() the iterator before you fetch any objects
>>from it, which mean that you'd keep a list of all objects in memory
>>anyway. And when you do that, you can go straight to a list without
>>using itertools.tee().
> 
> Not quite, you'll still fetch the upcoming items "lazily", so that you
> don't need to prefetch 1000000 lines from a long generator or choke on
> an infinite one...

With "all objects" I meant "all objects you've output so far". I.e. you 
only fetch more object once the user goes beyond the current last one.

>>But how would %page recognise which columns/attributes to display?
>>Furthermore I'd like to be able to manipulate this generator (filtering,
>>sorting, etc.). As an example take a look at
>>
>>    http://www.livinglogic.de/Python/sh.py
> 
> 
> Yes, the filter idea is certainly interesting and useful.
> 
> 
>>This implements env (as a wrapper for os.environ) and pwd (as a wrapper
>>for the pwd module (if you're on Linux)). Just do a "from sh import *"
> 
> 
> The special case for es.environ might be a bit too specific - why not
> just do general wrapper for dicts and always use "key" and "val" for
> field names? Then you could do
> 
> tabulatedict(os.environ) / contains(key='PY') / sort(val=True) / WalterPager

This can be done with the following filter that transforms a dictionary 
(well anything that has a iteritems()) into a browseable filter:

class tabulatedict(Filter):
    def operate(self, iterator):
       class dictentry(Object):
          __shattrs__ = ("key", "val")

          def __init__(self, key, val):
             self.key = key
             self.val = val

       for item in iterator.iteritems():
          yield dictentry(*item)

With this you can do:

 >>> os.environ / tabulatedict() / contains(key="HOME") / sort(key=False)
key        |val                |
HOME       |/var/home/walter   |
JAVA_HOME  |/opt/j2sdk1.4.1_02 |
ORACLE_HOME|/oracle/Client     |
TOMCAT_HOME|/opt/jakarta-tomcat|

Bye,
    Walter Dörwald




More information about the IPython-user mailing list