Inject a group of variables into the IPython user namespace. Parameters ---------- variables : dict, str or list/tuple of str The variables to inject into the user's namespace. If a dict, a simple update is done. If a str, the string is assumed to h
(self, variables, interactive=True)
| 1531 | del ns[var] |
| 1532 | |
| 1533 | def push(self, variables, interactive=True): |
| 1534 | """Inject a group of variables into the IPython user namespace. |
| 1535 | |
| 1536 | Parameters |
| 1537 | ---------- |
| 1538 | variables : dict, str or list/tuple of str |
| 1539 | The variables to inject into the user's namespace. If a dict, a |
| 1540 | simple update is done. If a str, the string is assumed to have |
| 1541 | variable names separated by spaces. A list/tuple of str can also |
| 1542 | be used to give the variable names. If just the variable names are |
| 1543 | give (list/tuple/str) then the variable values looked up in the |
| 1544 | callers frame. |
| 1545 | interactive : bool |
| 1546 | If True (default), the variables will be listed with the ``who`` |
| 1547 | magic. |
| 1548 | """ |
| 1549 | vdict = None |
| 1550 | |
| 1551 | # We need a dict of name/value pairs to do namespace updates. |
| 1552 | if isinstance(variables, dict): |
| 1553 | vdict = variables |
| 1554 | elif isinstance(variables, (str, list, tuple)): |
| 1555 | if isinstance(variables, str): |
| 1556 | vlist = variables.split() |
| 1557 | else: |
| 1558 | vlist = variables |
| 1559 | vdict = {} |
| 1560 | cf = sys._getframe(1) |
| 1561 | for name in vlist: |
| 1562 | try: |
| 1563 | vdict[name] = eval(name, cf.f_globals, cf.f_locals) |
| 1564 | except: |
| 1565 | print('Could not get variable %s from %s' % |
| 1566 | (name,cf.f_code.co_name)) |
| 1567 | else: |
| 1568 | raise ValueError('variables must be a dict/str/list/tuple') |
| 1569 | |
| 1570 | # Propagate variables to user namespace |
| 1571 | self.user_ns.update(vdict) |
| 1572 | |
| 1573 | # And configure interactive visibility |
| 1574 | user_ns_hidden = self.user_ns_hidden |
| 1575 | if interactive: |
| 1576 | for name in vdict: |
| 1577 | user_ns_hidden.pop(name, None) |
| 1578 | else: |
| 1579 | user_ns_hidden.update(vdict) |
| 1580 | |
| 1581 | def drop_by_id(self, variables): |
| 1582 | """Remove a dict of variables from the user namespace, if they are the |