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

Walter Dörwald walter at livinglogic.de
Sat Jan 7 06:39:45 CST 2006


Ville Vainio wrote:

> On 1/5/06, Walter Dörwald <walter at livinglogic.de> wrote:
>
>> I'm not sure how much time I can devote to this, but I'm certainly willing to help.
>
> I guess you should "take your time" and produce the simplest possible *library* (genfilters.py, or whatever you choose to
> call it), put it up on the web and then we can focus on the connection points
> necessary/useful for ipython integration. I'm sure many connection points deemed useful could be implemented by factoring out
> the
> functionality to be replaced to hooks.py and then implementing the replacements as ipython hooks.
>
> Most use cases of such a filter system do overlap the use cases of ipython, but the basic solution can be done in
> plain-old-python space and can be harnessed by ipython by just doing "from genfilters import *".

OK I started from scratch now. The result can be downloaded here (updated once a day):
http://www.livinglogic.de/viewcvs/index.cgi/LivingLogic/Python/core/ipipe.py
Currently when you do a "from ll.ipipe import *" the displayhook is replaced completely.

A few examples:
>>> ils("~")
gives you a listing of your home directory.

>>> ils
gives you a listing of the current directory.

>>> ill("~")
gives a long listing (i.e. more columns).

You can also do
>>> ils("~") / iattrs("mode", "blocks", "basename")
to specify which attributes you what.

Filtering is done like you suggested:
>>> ill("~") / ifilter("size > 10000")

For referencing attributes that aren't exposed by the input pipe, the object itself is available as _:

>>> ils / ifilter("_.blocks > 10")

Alternatively you can pass a callable to ifilter:
>>> ils / ifilter(lambda f: f.blocks > 10)

ix allows you to execute shell commands:
>>> ix("find -type f -name '*.py'")

All files returned are instances of ifile. You can use the ifile class in a pipe to create ifile objects from the input pipe:
>>> ix("locate foo") / ifile / ifilter("_.user == 'root'")

isort allows sorting, it has arguments key and reverse with the same meaning like for the builtin sort:
>>> ils / isort("name", False)
>>> ils / isort("len(_.name), size")

Again you can pass a callable:
>>> ils / isort(lambda f:len(f.abspath), False)

I wonder if it makes sense to implement commands as pipes, i.e. something like:
>>> ix("find") / ifile / ifilter("_user != 'foo') / ichown(user='foo')

But as a pipe is a normal iterable this can be done with a simple loop:
>>> for f in ix("find") / ifile / ifilter("_user != 'foo'): os.chown(f, pwd.getpwnam('foo').pw_uid, f.gid)

Or maybe those commands should be implemented as ifile methods:
>>> for f in ix("find") / ifile / ifilter("_user != 'foo'): f.chown(user='foo')

(This looks similar to Jason Orendorf's path module).

Bye,
   Walter Dörwald






More information about the IPython-user mailing list