##// END OF EJS Templates
Use IPython's user namespace for the global namespace...
walter.doerwald -
Show More
@@ -154,9 +154,10 b' except ImportError:'
154 154
155 155 import path
156 156 try:
157 from IPython import genutils
157 from IPython import genutils, ipapi
158 158 except ImportError:
159 pass
159 genutils = None
160 ipapi = None
160 161
161 162 import astyle
162 163
@@ -255,6 +256,15 b' def item(iterator, index, default=noitem):'
255 256 return default
256 257
257 258
259 def getglobals(g):
260 if g is None:
261 if ipapi is not None:
262 return ipapi.get().user_ns()
263 else:
264 return globals()
265 return g
266
267
258 268 class Table(object):
259 269 """
260 270 A ``Table`` is an object that produces items (just like a normal Python
@@ -1348,11 +1358,13 b' class ifilter(Pipe):'
1348 1358 >>> sys.modules | ifilter(lambda _:_.value is not None)
1349 1359 """
1350 1360
1351 def __init__(self, expr, errors="raiseifallfail"):
1361 def __init__(self, expr, globals=None, errors="raiseifallfail"):
1352 1362 """
1353 1363 Create an ``ifilter`` object. ``expr`` can be a callable or a string
1354 containing an expression. ``errors`` specifies how exception during
1355 evaluation of ``expr`` are handled:
1364 containing an expression. ``globals`` will be used as the global
1365 namespace for calling string expressions (defaulting to IPython's
1366 user namespace). ``errors`` specifies how exception during evaluation
1367 of ``expr`` are handled:
1356 1368
1357 1369 * ``drop``: drop all items that have errors;
1358 1370
@@ -1366,6 +1378,7 b' class ifilter(Pipe):'
1366 1378 otherwise drop those with errors (this is the default).
1367 1379 """
1368 1380 self.expr = expr
1381 self.globals = globals
1369 1382 self.errors = errors
1370 1383
1371 1384 def __xiter__(self, mode):
@@ -1373,8 +1386,9 b' class ifilter(Pipe):'
1373 1386 def test(item):
1374 1387 return self.expr(item)
1375 1388 else:
1389 g = getglobals(self.globals)
1376 1390 def test(item):
1377 return eval(self.expr, globals(), AttrNamespace(item))
1391 return eval(self.expr, g, AttrNamespace(item))
1378 1392
1379 1393 ok = 0
1380 1394 exc_info = None
@@ -1431,12 +1445,14 b' class ieval(Pipe):'
1431 1445 >>> sys.path | ieval(ifile)
1432 1446 """
1433 1447
1434 def __init__(self, expr, errors="raiseifallfail"):
1448 def __init__(self, expr, globals=None, errors="raiseifallfail"):
1435 1449 """
1436 1450 Create an ``ieval`` object. ``expr`` can be a callable or a string
1437 containing an expression. For the meaning of ``errors`` see ``ifilter``.
1451 containing an expression. For the meaning of ``globals`` and
1452 ``errors`` see ``ifilter``.
1438 1453 """
1439 1454 self.expr = expr
1455 self.globals = globals
1440 1456 self.errors = errors
1441 1457
1442 1458 def __xiter__(self, mode):
@@ -1444,8 +1460,9 b' class ieval(Pipe):'
1444 1460 def do(item):
1445 1461 return self.expr(item)
1446 1462 else:
1463 g = getglobals(self.globals)
1447 1464 def do(item):
1448 return eval(self.expr, globals(), AttrNamespace(item))
1465 return eval(self.expr, g, AttrNamespace(item))
1449 1466
1450 1467 ok = 0
1451 1468 exc_info = None
@@ -1515,13 +1532,14 b' class isort(Pipe):'
1515 1532 >>> ils | isort("_.isdir(), _.lower()", reverse=True)
1516 1533 """
1517 1534
1518 def __init__(self, key, reverse=False):
1535 def __init__(self, key, globals=None, reverse=False):
1519 1536 """
1520 1537 Create an ``isort`` object. ``key`` can be a callable or a string
1521 1538 containing an expression. If ``reverse`` is true the sort order will
1522 be reversed.
1539 be reversed. For the meaning of ``globals`` see ``ifilter``.
1523 1540 """
1524 1541 self.key = key
1542 self.globals = globals
1525 1543 self.reverse = reverse
1526 1544
1527 1545 def __xiter__(self, mode):
@@ -1532,8 +1550,9 b' class isort(Pipe):'
1532 1550 reverse=self.reverse
1533 1551 )
1534 1552 else:
1553 g = getglobals(self.globals)
1535 1554 def key(item):
1536 return eval(self.key, globals(), AttrNamespace(item))
1555 return eval(self.key, g, AttrNamespace(item))
1537 1556 items = sorted(
1538 1557 xiter(self.input, mode),
1539 1558 key=key,
@@ -1811,11 +1830,12 b' else:'
1811 1830 # If we're running under IPython, install an IPython displayhook that
1812 1831 # returns the object from Display.display(), else install a displayhook
1813 1832 # directly as sys.displayhook
1814 try:
1815 from IPython import ipapi
1816 api = ipapi.get()
1817 except (ImportError, AttributeError):
1818 api = None
1833 api = None
1834 if ipapi is not None:
1835 try:
1836 api = ipapi.get()
1837 except AttributeError:
1838 pass
1819 1839
1820 1840 if api is not None:
1821 1841 def displayhook(self, obj):
General Comments 0
You need to be logged in to leave comments. Login now