MCPcopy Create free account
hub / github.com/ipython/ipython / var_expand

Method var_expand

IPython/core/interactiveshell.py:3564–3591  ·  view source on GitHub ↗

Expand python variables in a string. The depth argument indicates how many frames above the caller should be walked to look for the local namespace where to expand variables. The global namespace for expansion is always the user's interactive namespace.

(self, cmd, depth=0, formatter=DollarFormatter())

Source from the content-addressed store, hash-verified

3562 #-------------------------------------------------------------------------
3563
3564 def var_expand(self, cmd, depth=0, formatter=DollarFormatter()):
3565 """Expand python variables in a string.
3566
3567 The depth argument indicates how many frames above the caller should
3568 be walked to look for the local namespace where to expand variables.
3569
3570 The global namespace for expansion is always the user's interactive
3571 namespace.
3572 """
3573 ns = self.user_ns.copy()
3574 try:
3575 frame = sys._getframe(depth+1)
3576 except ValueError:
3577 # This is thrown if there aren't that many frames on the stack,
3578 # e.g. if a script called run_line_magic() directly.
3579 pass
3580 else:
3581 ns.update(frame.f_locals)
3582
3583 try:
3584 # We have to use .vformat() here, because 'self' is a valid and common
3585 # name, and expanding **ns for .format() would make it collide with
3586 # the 'self' argument of the method.
3587 cmd = formatter.vformat(cmd, args=[], kwargs=ns)
3588 except Exception:
3589 # if formatter couldn't format, just let it go untransformed
3590 pass
3591 return cmd
3592
3593 def mktempfile(self, data=None, prefix='ipython_edit_'):
3594 """Make a new tempfile and return its filename.

Callers 7

run_line_magicMethod · 0.95
run_cell_magicMethod · 0.95
system_pipedMethod · 0.95
system_rawMethod · 0.95
getoutputMethod · 0.95
test_var_expandMethod · 0.80
test_bad_var_expandMethod · 0.80

Calls 4

DollarFormatterClass · 0.90
vformatMethod · 0.80
copyMethod · 0.45
updateMethod · 0.45

Tested by 2

test_var_expandMethod · 0.64
test_bad_var_expandMethod · 0.64