Retracing your steps in an interactive python env

J

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
 
R

r

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

Considering you created a doc string.
 
M

Mensanator

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
 
T

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:
.... print(x+y)
....(throws IOError: could not get source code)

Perhaps someone else has more experience on the matter.
 
S

Steven D'Aprano

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

Robert Kern

Steven said:
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
 
T

TerryP

Under unix and cygwin, it's also possible to use GNU Screen, along
with a much larger then default defscrollback value.
 
B

Bernhard Voigt

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,077
Latest member
SangMoor21

Latest Threads

Top