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())
| 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. |