MCPcopy Index your code
hub / github.com/ipython/ipython / var_expand

Method var_expand

IPython/core/interactiveshell.py:3908–3935  ·  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

3906 #-------------------------------------------------------------------------
3907
3908 def var_expand(self, cmd, depth=0, formatter=DollarFormatter()):
3909 """Expand python variables in a string.
3910
3911 The depth argument indicates how many frames above the caller should
3912 be walked to look for the local namespace where to expand variables.
3913
3914 The global namespace for expansion is always the user's interactive
3915 namespace.
3916 """
3917 ns = self.user_ns.copy()
3918 try:
3919 frame = sys._getframe(depth+1)
3920 except ValueError:
3921 # This is thrown if there aren't that many frames on the stack,
3922 # e.g. if a script called run_line_magic() directly.
3923 pass
3924 else:
3925 ns.update(frame.f_locals)
3926
3927 try:
3928 # We have to use .vformat() here, because 'self' is a valid and common
3929 # name, and expanding **ns for .format() would make it collide with
3930 # the 'self' argument of the method.
3931 cmd = formatter.vformat(cmd, args=[], kwargs=ns)
3932 except Exception:
3933 # if formatter couldn't format, just let it go untransformed
3934 pass
3935 return cmd
3936
3937 def mktempfile(self, data=None, prefix='ipython_edit_'):
3938 """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