[IPython-dev] ipipe news
Walter Dörwald
walter at livinglogic.de
Fri Mar 3 03:34:38 EST 2006
Fernando Perez wrote:
> Walter Dörwald wrote:
>
>> [...]
>> But as you get '0 objects' this probably means that you don't have any
>> Python source files in your current directory.
>
> No, that's not the case:
>
> In [4]: ls *.py
> argv.py* cf.py env.py* exit2.py* sciweave.py vars.py
> avg.py* die.py error.py* exit.py* scopes.py* xplot.py*
> bar.py div.py err.py ramptest.py* strings.py
>
> And in addition, I thought it was recursive, so I thought it would work
> even if the parent directory had no .py files (as long as subdirs had
> them).
It does.
>> What happens if you do a simple "iwalk"?
>
> That works fine. But
>
> iwalk | ifilter("name.endswith('.py')")
>
> gives an empty browser, and
>
> iwalk | isort("size")
>
> produces a lovely traceback (here's just the end of it, let me know if
> you want a full one in verbose mode, I'll attach it).
>
> [...]
> /home/fperez/usr/lib/python2.3/site-packages/IPython/Extensions/ipipe.py
> in key(item)
> 984 else:
> 985 def key(item):
> --> 986 return eval(self.key, globals(),
> _AttrNamespace(item))
> 987 items = sorted(
> 988 xiter(self.input, mode),
>
> TypeError: eval() argument 3 must be dict, not _AttrNamespace
I see: ipipe uses a new feature of eval() that makes it possible to pass
a custom (non-dict) namespace. This makes it possible to directly
reference the attributes of the object. For 2.3 compatibility I see
several options:
1) Simply don't allow expression strings. This would only allow
callables and
iwalk | ifilter("name.endswith('.py')") | isort("size")
would turn into
iwalk | ifilter(lambda _:_.name.endswith('.py')) | isort(lambda _:_size)
IMHO not so nice.
2) Pass a simple dictionary to eval() that just contains the object
itself. The call would then look like this
iwalk | ifilter("_.name.endswith('.py')") | isort("_.size")
3) Pass a dictionary that contains all the attributes from the list.
This has the disadvantage that (for iwalk etc.) there will be many
os.stat() calls, even if the result is never used (and you can't
reference attributes that are not in the field list).
BTW, the reason why you got an empty browser was, because ifilter
ignores all objects where evaluating the expression raises and
Exception. And then the expression for isort never fails, because
there's nothing left to sort.
Anyway, IMHO we should implement 2) and document the possibility to
directly reference attributes as a "2.4 goodie".
Servus,
Walter
More information about the IPython-dev
mailing list