Re: Retracing your steps in an interactive python env

2009-09-15 Thread Bernhard Voigt
On Sep 14, 9:52 pm, Jack Norton  wrote:

> Anyway, I have created a function using def, and well, I like the way it
> is working, however...  I have already filled the command line history
> buffer (the com.exe buffer?) so _what_ I actually filled this def with
> is lost.  Now, it isn't that complicated, and I can easily re-write the
> function off the top of my head, however it would be really nice to be
> able to _ask_ python what makes up a def.

IPython has it's own history. Accessible using the %hist magic
command, the default is 1000 lines but can be changed in the ipythonrc
file.
There are many other magic commands which are very useful for
interactive development. For example there's a %log command that
enables logging of your session in a file. Another example is %save to
store parts of your history into a file and %edit to launch an
external editor to edit a bunch of commands from the history or enter
longer blocks of code. Take a look at the documentation of the magic
commands by entering %magic.

Bernhard
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Retracing your steps in an interactive python env

2009-09-14 Thread TerryP
Under unix and cygwin, it's also possible to use GNU Screen, along
with a much larger then default defscrollback value.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Retracing your steps in an interactive python env

2009-09-14 Thread Robert Kern

Steven D'Aprano wrote:

I wonder whether there's a third party module which will take the output 
of dis.dis and try to reverse engineer Python code from it?


There used to be decompyle, but it hasn't been kept up-to-date, at least not 
publicly. There used to be a service that would use an updated decompyle on your 
uploaded bytecode through the web for a fee, but I don't know of anyone who used 
it. It always seemed a little shady to me.


--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco

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


Re: Retracing your steps in an interactive python env

2009-09-14 Thread Steven D'Aprano
On Mon, 14 Sep 2009 14:52:34 -0500, Jack Norton wrote:

> it would be really nice to be
> able to _ask_ python what makes up a def. Something like this (remember
> I am using IPython interactive interpreter session):
> In [0]: def func(input):
> .:>>>print "im in this function!" + str(input)
> .:>>>print "doing some stuff" .:>>>sleep(10)
> 
> Then later on while still in this interactive shell session I could do
> something like:
> In [1]: what_is_in(func)
> "The def for func(input) is:"
> print "im in this function!" + str(input) print "doing some stuff"
> sleep(10)
> 
> and therefore be able to recount what I just did.

Can't be done in the CPython interactive interpreter, because code is 
compiled before being executed. It doesn't save the source code when it 
compiles the function definition, so there's no way it can show you the 
source code. That's why interactive tracebacks don't show you the 
offending line that failed (with the exception of syntax errors) unless 
you have imported the function from a .py file.

You could probably mess about with the readline library to save a copy of 
everything to a history file.

Another alternative is to disassemble the compiled function, and try to 
determine what it does from that. See the dis module.

I wonder whether there's a third party module which will take the output 
of dis.dis and try to reverse engineer Python code from it?



-- 
Steven
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Retracing your steps in an interactive python env

2009-09-14 Thread TerryP
I'm not sure what the best way to do this is, other then that it would
mean asking the interp to explain it in a format we understand ;).


In the (c)python library reference, I see an inspect module that
sounds like it should be useful, but it doesn't work on my test case
here:

>>> def foo(x, y):
...  print(x+y)
...
>>> inspect.getsource(foo)
(throws IOError: could not get source code)

Perhaps someone else has more experience on the matter.

--

TerryP.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Retracing your steps in an interactive python env

2009-09-14 Thread Mensanator
On Sep 14, 2:52 pm, Jack Norton  wrote:
> Hello all,
>
> I am playing around in a python shell (IPython on win32 right now
> actually).  I am writing some code on the fly to interface to a rotary
> encoder (not important in this scope).
>
> Anyway, I have created a function using def, and well, I like the way it
> is working, however...  I have already filled the command line history
> buffer (the com.exe buffer?)

I usually have my buffer default to 8000x132.

> so _what_ I actually filled this def with
> is lost.  Now, it isn't that complicated, and I can easily re-write the
> function off the top of my head, however it would be really nice to be
> able to _ask_ python what makes up a def.
> Something like this (remember I am using IPython interactive interpreter
> session):
> In [0]: def func(input):
> .:>>>print "im in this function!" + str(input)
> .:>>>print "doing some stuff"
> .:>>>sleep(10)
>
> Then later on while still in this interactive shell session I could do
> something like:
> In [1]: what_is_in(func)
> "The def for func(input) is:"
> print "im in this function!" + str(input)
> print "doing some stuff"
> sleep(10)
>
> and therefore be able to recount what I just did.
>
> Cheers,
>
> Jack

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


Re: Retracing your steps in an interactive python env

2009-09-14 Thread r
On Sep 14, 2:52 pm, Jack Norton  wrote:
> Hello all,
>
> I am playing around in a python shell (IPython on win32 right now
> actually).  I am writing some code on the fly to interface to a rotary
> encoder (not important in this scope).
>
> Anyway, I have created a function using def, and well, I like the way it
> is working, however...  I have already filled the command line history
> buffer (the com.exe buffer?) so _what_ I actually filled this def with
> is lost.  Now, it isn't that complicated, and I can easily re-write the
> function off the top of my head, however it would be really nice to be
> able to _ask_ python what makes up a def.
> Something like this (remember I am using IPython interactive interpreter
> session):
> In [0]: def func(input):
> .:>>>print "im in this function!" + str(input)
> .:>>>print "doing some stuff"
> .:>>>sleep(10)
>
> Then later on while still in this interactive shell session I could do
> something like:
> In [1]: what_is_in(func)
> "The def for func(input) is:"
> print "im in this function!" + str(input)
> print "doing some stuff"
> sleep(10)
>
> and therefore be able to recount what I just did.
>
> Cheers,
>
> Jack

You could put the entire def in a multi line doc string...?

>>> print func.__doc__

Considering you created a doc string.
-- 
http://mail.python.org/mailman/listinfo/python-list


Retracing your steps in an interactive python env

2009-09-14 Thread Jack Norton

Hello all,

I am playing around in a python shell (IPython on win32 right now 
actually).  I am writing some code on the fly to interface to a rotary 
encoder (not important in this scope).


Anyway, I have created a function using def, and well, I like the way it 
is working, however...  I have already filled the command line history 
buffer (the com.exe buffer?) so _what_ I actually filled this def with 
is lost.  Now, it isn't that complicated, and I can easily re-write the 
function off the top of my head, however it would be really nice to be 
able to _ask_ python what makes up a def.
Something like this (remember I am using IPython interactive interpreter 
session):

In [0]: def func(input):
.:>>>print "im in this function!" + str(input)
.:>>>print "doing some stuff"
.:>>>sleep(10)

Then later on while still in this interactive shell session I could do 
something like:

In [1]: what_is_in(func)
"The def for func(input) is:"
print "im in this function!" + str(input)
print "doing some stuff"
sleep(10)

and therefore be able to recount what I just did.

Cheers,

Jack 


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