From 8759e935491a79d0b6c9caeaf242f88704b871c8 2006-07-12 21:29:55 From: walter.doerwald Date: 2006-07-12 21:29:55 Subject: [PATCH] Compile expression strings into code objects. This should speed up ifilter and friends somewhat. --- diff --git a/IPython/Extensions/ipipe.py b/IPython/Extensions/ipipe.py index 7f119db..e9bd118 100644 --- a/IPython/Extensions/ipipe.py +++ b/IPython/Extensions/ipipe.py @@ -210,7 +210,10 @@ except TypeError: Python 2.3's eval. """ - code = compile(codestring, "_eval", "eval") + if isinstance(codestring, basestring): + code = compile(codestring, "_eval", "eval") + else: + code = codestring newlocals = {} for name in code.co_names: try: @@ -1361,12 +1364,12 @@ class ifilter(Pipe): def __xiter__(self, mode): if callable(self.expr): - def test(item): - return self.expr(item) + test = self.expr else: g = getglobals(self.globals) + expr = compile(self.expr, "ipipe-expression", "eval") def test(item): - return eval(self.expr, g, AttrNamespace(item)) + return eval(expr, g, AttrNamespace(item)) ok = 0 exc_info = None @@ -1434,12 +1437,12 @@ class ieval(Pipe): def __xiter__(self, mode): if callable(self.expr): - def do(item): - return self.expr(item) + do = self.expr else: g = getglobals(self.globals) + expr = compile(self.expr, "ipipe-expression", "eval") def do(item): - return eval(self.expr, g, AttrNamespace(item)) + return eval(expr, g, AttrNamespace(item)) ok = 0 exc_info = None @@ -1527,11 +1530,12 @@ class isort(Pipe): ) else: g = getglobals(self.globals) - def key(item): - return eval(self.key, g, AttrNamespace(item)) + key = compile(self.key, "ipipe-expression", "eval") + def realkey(item): + return eval(key, g, AttrNamespace(item)) items = sorted( xiter(self.input, mode), - key=key, + key=realkey, reverse=self.reverse ) for item in items: diff --git a/doc/ChangeLog b/doc/ChangeLog index c71b1f4..b825e26 100644 --- a/doc/ChangeLog +++ b/doc/ChangeLog @@ -1,3 +1,9 @@ +2006-07-12 Walter Doerwald + + * IPython/Extensions/ipipe.py (eval, ifilter, isort, ieval): + Compile expression strings into code objects. This should speed + up ifilter and friends somewhat. + 2006-07-08 Ville Vainio * Magic.py: %cpaste now strips > from the beginning of lines