Re: [Tutor] Printing the code of a function

2008-12-29 Thread Alan Gauld


wormwood_3 wormwoo...@yahoo.com wrote

I am wondering if there is a way to print out the code of a defined 
function.


Its not reliable but I think you can use

func.func_code.filename
func.func_code.firstlineno

To find the first line of code in the original source file.
Its up to you to figure out the last line though! I guess checking
for the next line with equivalent indentation might work.

But I'm not sure how much good it would do you... unless you
were writing a debugger maybe?

Alan G


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Printing the code of a function

2008-12-29 Thread spir
On Mon, 29 Dec 2008 09:18:43 -
Alan Gauld alan.ga...@btinternet.com wrote:

 
 wormwood_3 wormwoo...@yahoo.com wrote
 
  I am wondering if there is a way to print out the code of a defined 
  function.
 
 Its not reliable but I think you can use
 
 func.func_code.filename
 func.func_code.firstlineno
 
 To find the first line of code in the original source file.
 Its up to you to figure out the last line though! I guess checking
 for the next line with equivalent indentation might work.
 
 But I'm not sure how much good it would do you... unless you
 were writing a debugger maybe?
 
 Alan G

I would do it so by parsing source file:

* (convert indents to tabs (easier to parse))
* search for a line that start with (n indents) + def func_name
* record all following lines that start with (n+1 indents) or more
* stop where a line starts with (n indents) or less

denis

--
la vida e estranya
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Printing the code of a function

2008-12-29 Thread Kent Johnson
On Sun, Dec 28, 2008 at 8:49 PM, wormwood_3 wormwoo...@yahoo.com wrote:
 Hello all,

 This might be trivially easy, but I was having a hard time searching on it
 since all the component terms are overloaded:-) I am wondering if there is a
 way to print out the code of a defined function.

If the source code is available try inspect.getsource().

Kent
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Printing the code of a function

2008-12-28 Thread wormwood_3
Hello all,

This might be trivially easy, but I was having a hard time searching on it 
since all the component terms are overloaded:-) I am wondering if there is a 
way to print out the code of a defined function. So if I have:

def foo():
print Show me the money.

then I would like to do something like:

 foo.show_code
def foo():
print Show me the money.

I checked out everything in dir(foo), but everything that looked promising 
(namely foo.func_code), didn't end up being anything close.

Thanks for any help!

Cordially,
Sam

 ___
Samuel Huckins


Homepage - http://samuelhuckins.com
Tech blog - http://dancingpenguinsoflight.com/
Photos - http://www.flickr.com/photos/samuelhuckins/
AIM - samushack | Gtalk - samushack | Skype - shuckins
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Printing the code of a function

2008-12-28 Thread Michiel Overtoom


wormwood_3 wrote:

 I am wondering if there is a way to
 print out the code of a defined function.

When Python compiles source code, it doesn't store the source code 
itself; only the compiled intermediate code. With the 'dis' package you 
can disassemble that:


def foo():
print Show me the money.

import dis
dis.dis(foo)


  2   0 LOAD_CONST   1 ('Show me the money.')
  3 PRINT_ITEM
  4 PRINT_NEWLINE
  5 LOAD_CONST   0 (None)
  8 RETURN_VALUE



--
The ability of the OSS process to collect and harness
the collective IQ of thousands of individuals across
the Internet is simply amazing. - Vinod Vallopillil
http://www.catb.org/~esr/halloween/halloween4.html
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Printing the code of a function

2008-12-28 Thread bob gailer




wormwood_3 wrote:

  
  Hello
all,
  
This might be trivially easy, but I was having a hard time searching on
it since all the component terms are overloaded:-) I am wondering if
there is a way to print out the code of a defined function. 


Python does not store the source when compiling things. So the short
answer is NO.

There are some "disassemblers" for Python but I have no experience with
them. They will not be able to reconstruct the exact source.

But ... why do you want to do this? Perhaps if we had more information
about the "use case" we could steer you to a solution.


  So
if I have:
  
def foo():
 print "Show me the money."
  
then I would like to do something like:
  
 foo.show_code
def foo():
 print "Show me the money."
  
I checked out everything in dir(foo), but everything that looked
promising (namely foo.func_code), didn't end up being anything close.
  
Thanks for any help!
  
Cordially,
Sam
  
___
Samuel Huckins
  
  Homepage - http://samuelhuckins.com
Tech blog - http://dancingpenguinsoflight.com/
Photos - http://www.flickr.com/photos/samuelhuckins/
AIM - samushack | Gtalk - samushack | Skype - shuckins
  
  
  
  
  

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor
  



-- 
Bob Gailer
Chapel Hill NC 
919-636-4239


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Printing the code of a function

2008-12-28 Thread wormwood_3
I actually didn't have a definite use case in mind, it was pure curiosity that 
arose while writing a few simple test functions. After having considered it 
more, I can't come up with a case where it would really be necessary, so I 
apologize for that. It makes sense why it wouldn't be possible without a 
disassembler now. Thanks for the info!

 ___
Samuel Huckins


Homepage - http://samuelhuckins.com
Tech blog - http://dancingpenguinsoflight.com/
Photos - http://www.flickr.com/photos/samuelhuckins/
AIM - samushack | Gtalk - samushack | Skype - shuckins





From: bob gailer bgai...@gmail.com
To: wormwood_3 wormwoo...@yahoo.com
Cc: tutor@python.org
Sent: Sunday, December 28, 2008 9:07:12 PM
Subject: Re: [Tutor] Printing the code of a function

wormwood_3 wrote: 
Hello
all,

This might be trivially easy, but I was having a hard time searching on
it since all the component terms are overloaded:-) I am wondering if
there is a way to print out the code of a defined function. 
Python does not store the source when compiling things. So the short
answer is NO.

There are some disassemblers for Python but I have no experience with
them. They will not be able to reconstruct the exact source.

But ... why do you want to do this? Perhaps if we had more information
about the use case we could steer you to a solution.


So
if I have:

def foo():
print Show me the money.

then I would like to do something like:

 foo.show_code
def foo():
print Show me the money.

I checked out everything in dir(foo), but everything that looked
promising (namely foo.func_code), didn't end up being anything close.

Thanks for any help!

Cordially,
Sam

 
___
Samuel Huckins


Homepage - http://samuelhuckins.com
Tech blog - http://dancingpenguinsoflight.com/
Photos - http://www.flickr.com/photos/samuelhuckins/
AIM - samushack | Gtalk - samushack | Skype - shuckins 




___
Tutor maillist  -  Tutor@python.org 
http://mail.python.org/mailman/listinfo/tutor 


-- 
Bob Gailer
Chapel Hill NC 
919-636-4239___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor