Is there something similar to `set -v` of bash in python

Thorsten Kampe thorsten at thorstenkampe.de
Sat Sep 17 06:25:05 EDT 2016


* Peng Yu (Fri, 16 Sep 2016 21:31:37 -0500)
> 
> Hi, `set -v` in bash allows the print of the command before print the
> output of the command.
> 
> I want to do the similar thing --- print a python command and then
> print the output of the command. Is it possible with python?

Rather easily. I've implemented it some time ago like this:

"""
import sys

def _traceit(frame, event, arg):
    if (frame.f_globals['__name__'] == '__main__' and
        event in ['call', 'line']):

        logger.debug('+[{lineno}]: {code}'.format(
            lineno = frame.f_lineno,
            code   = inspect.getframeinfo(frame).code_context
[0].rstrip()))
    return _traceit

sys.settrace(_traceit)
"""

Thorsten




More information about the Python-list mailing list