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

2016-09-18 Thread Ned Batchelder
On Sunday, September 18, 2016 at 8:29:38 AM UTC-4, Peng Yu wrote:
> On Sunday, September 18, 2016, Ned Batchelder  wrote:
> 
> > On Saturday, September 17, 2016 at 11:09:04 PM UTC-4, Peng Yu wrote:
> > > The manual says the following.
> > >
> > > "The trace function is invoked (with event set to 'call') whenever a
> > > new local scope is entered; it should return a reference to a local
> > > trace function to be used that scope, or None if the scope shouldn’t
> > > be traced."
> > >
> > > It means that one can not somehow settrace in one line and expect to
> > > get the trace function being called in the next line.
> > >
> > > So something like `set -v` in bash sounds not possible. Is it so?
> >
> > You've found a good reason why "set -v" would be very difficult if
> > not impossible in Python.
> >
> > I'm curious though, why you would want to trace every line in a
> > program every time you (or anyone else) ran it?
> 
> 
> This is for debugging not for normal run. But I need the ability to control
> the range of the code in which the debug message is printed.

There is a programmatic interface: 
https://docs.python.org/2/library/trace.html#programmatic-interface

If the section you want to trace is a function call, then you can make
it work.

--Ned.
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2016-09-18 Thread Peng Yu
On Sunday, September 18, 2016, Ned Batchelder  wrote:

> On Saturday, September 17, 2016 at 11:09:04 PM UTC-4, Peng Yu wrote:
> > The manual says the following.
> >
> > "The trace function is invoked (with event set to 'call') whenever a
> > new local scope is entered; it should return a reference to a local
> > trace function to be used that scope, or None if the scope shouldn’t
> > be traced."
> >
> > It means that one can not somehow settrace in one line and expect to
> > get the trace function being called in the next line.
> >
> > So something like `set -v` in bash sounds not possible. Is it so?
>
> You've found a good reason why "set -v" would be very difficult if
> not impossible in Python.
>
> I'm curious though, why you would want to trace every line in a
> program every time you (or anyone else) ran it?


This is for debugging not for normal run. But I need the ability to control
the range of the code in which the debug message is printed.


> I view tracing lines
> as a debugging technique. Once the program is behaving properly, why
> do you want all the noise of the traced lines?
>
> In Bash scripts we do it because some scripts are light automation
> where the person running the script should have a clear idea of what
> actions it is taking.  The actions in the script are commands that the
> person might run themselves at other times, and so the difference
> between running the script and running the commands is not great.
>
> But in a Python program, presumably the difference is greater. I cannot
> type out single Python lines at my shell prompt to perform the actions
> the Python program does.
>
> Perhaps you want to define an alias for "python -m trace -t $*" ?
>
> --Ned.
> --
> https://mail.python.org/mailman/listinfo/python-list
>


-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2016-09-18 Thread Ned Batchelder
On Saturday, September 17, 2016 at 11:09:04 PM UTC-4, Peng Yu wrote:
> The manual says the following.
> 
> "The trace function is invoked (with event set to 'call') whenever a
> new local scope is entered; it should return a reference to a local
> trace function to be used that scope, or None if the scope shouldn’t
> be traced."
> 
> It means that one can not somehow settrace in one line and expect to
> get the trace function being called in the next line.
> 
> So something like `set -v` in bash sounds not possible. Is it so?

You've found a good reason why "set -v" would be very difficult if
not impossible in Python.

I'm curious though, why you would want to trace every line in a
program every time you (or anyone else) ran it? I view tracing lines
as a debugging technique. Once the program is behaving properly, why
do you want all the noise of the traced lines?

In Bash scripts we do it because some scripts are light automation
where the person running the script should have a clear idea of what
actions it is taking.  The actions in the script are commands that the
person might run themselves at other times, and so the difference
between running the script and running the commands is not great.

But in a Python program, presumably the difference is greater. I cannot
type out single Python lines at my shell prompt to perform the actions
the Python program does.

Perhaps you want to define an alias for "python -m trace -t $*" ?

--Ned.
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2016-09-17 Thread Chris Angelico
On Sun, Sep 18, 2016 at 1:08 PM, Peng Yu  wrote:
> The manual says the following.
>
> "The trace function is invoked (with event set to 'call') whenever a
> new local scope is entered; it should return a reference to a local
> trace function to be used that scope, or None if the scope shouldn’t
> be traced."
>
> It means that one can not somehow settrace in one line and expect to
> get the trace function being called in the next line.
>
> So something like `set -v` in bash sounds not possible. Is it so?

Can you predict in advance that you might be using this? If so, you
can define a trace function, and then activate and deactivate it on
command (in any way you like).

ChrisA
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2016-09-17 Thread Peng Yu
The manual says the following.

"The trace function is invoked (with event set to 'call') whenever a
new local scope is entered; it should return a reference to a local
trace function to be used that scope, or None if the scope shouldn’t
be traced."

It means that one can not somehow settrace in one line and expect to
get the trace function being called in the next line.

So something like `set -v` in bash sounds not possible. Is it so?

On Sat, Sep 17, 2016 at 5:28 PM, Ned Batchelder  wrote:
> On Saturday, September 17, 2016 at 4:41:32 PM UTC-4, Peng Yu wrote:
>> > python -m trace -t yourprogram.py
>>
>> If I want to add some command in yourprogram.py to show the commands
>> used it instead of calling trace from the command line, can it be
>> done?
>
> I don't know of a way to do that, but it might be possible.
>
> --Ned.
> --
> https://mail.python.org/mailman/listinfo/python-list



-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2016-09-17 Thread Ned Batchelder
On Saturday, September 17, 2016 at 4:41:32 PM UTC-4, Peng Yu wrote:
> > python -m trace -t yourprogram.py
> 
> If I want to add some command in yourprogram.py to show the commands
> used it instead of calling trace from the command line, can it be
> done?

I don't know of a way to do that, but it might be possible.

--Ned.
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2016-09-17 Thread Peng Yu
> python -m trace -t yourprogram.py

If I want to add some command in yourprogram.py to show the commands
used it instead of calling trace from the command line, can it be
done?

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2016-09-17 Thread Ned Batchelder
On Saturday, September 17, 2016 at 2:37:42 AM UTC-4, Steve D'Aprano wrote:
> On Sat, 17 Sep 2016 12:31 pm, Peng Yu wrote:
> 
> > 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?
> 
> 
> There is no built-in command for this, but it would be an interesting
> project for an advanced user to write a pre-processor that inserts calls to
> print after every line.

The trace module in the stdlib is little-known, but can do this.  This
will run your program and print each line before executing it:

python -m trace -t yourprogram.py

--Ned.
-- 
https://mail.python.org/mailman/listinfo/python-list


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

2016-09-17 Thread Thorsten Kampe
* Thorsten Kampe (Sat, 17 Sep 2016 12:25:05 +0200)
> 
> * 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:

Oops, I mixed it up with `set -x`, sorry.

Thorsten

-- 
https://mail.python.org/mailman/listinfo/python-list


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

2016-09-17 Thread Thorsten Kampe
* 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

-- 
https://mail.python.org/mailman/listinfo/python-list


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

2016-09-16 Thread Steve D'Aprano
On Sat, 17 Sep 2016 12:31 pm, Peng Yu wrote:

> 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?


There is no built-in command for this, but it would be an interesting
project for an advanced user to write a pre-processor that inserts calls to
print after every line.




-- 
Steve
“Cheer up,” they said, “things could be worse.” So I cheered up, and sure
enough, things got worse.

-- 
https://mail.python.org/mailman/listinfo/python-list


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

2016-09-16 Thread Peng Yu
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?

-- 
Regards,
Peng
-- 
https://mail.python.org/mailman/listinfo/python-list