Tsviki Hirsh | 5 Jan 07:53
Picon

docstring of decorated functions

Dear list,

The magic ? and ?? commands are among the most useful features in ipython (at least for me),
I'm also using them to read my selfdocumentation on my own functions.
Therefore I was surprised to learn that you cannot read the doc string of a decorated function using ? or ??

for example, consider this decorator and function


def deco(f):
'''decorator docstring'''
def _deco(*args,**kwargs):
'''decorator inner function docstring'''
print 'args=',args
return f(*args,**kwargs)
return _deco

<at> deco
def myfunc(x,y):
'''function docstring'''
return x+y


if I try to call myfunc docstring using ? I get:


In [18]: exem.myfunc?
Type: function
Base Class: <type 'function'>
String Form: <function _deco at 0x868a5a4>
Namespace: Interactive
File: /home/tsviki/exem.py
Definition: exem.myfunc(*args, **kwargs)
Docstring:
    decorator inner function docstring


and if I try to call ??, it just gives me the decorator inner function.

Is there a way to overlap this?
linking the decorator __doc__ to myfunc.__doc__ won't help in case that I have several functions that use the same decorator.

Any help?

Thanks a lot

Tsviki Hirsh

_______________________________________________
IPython-user mailing list
IPython-user <at> scipy.org
http://mail.scipy.org/mailman/listinfo/ipython-user
Brent Pedersen | 5 Jan 08:08
Picon
Gravatar

Re: docstring of decorated functions

On Mon, Jan 4, 2010 at 10:53 PM, Tsviki Hirsh <tsviki.hirsh <at> gmail.com> wrote:
> Dear list,
> The magic ? and ?? commands are among the most useful features in ipython
> (at least for me),
> I'm also using them to read my selfdocumentation on my own functions.
> Therefore I was surprised to learn that you cannot read the doc string of a
> decorated function using ? or ??
> for example, consider this decorator and function
>
> def deco(f):
> '''decorator docstring'''
> def _deco(*args,**kwargs):
> '''decorator inner function docstring'''
> print 'args=',args
> return f(*args,**kwargs)
> return _deco
> @deco
> def myfunc(x,y):
> '''function docstring'''
> return x+y
>
> if I try to call myfunc docstring using ? I get:
>
> In [18]: exem.myfunc?
> Type: function
> Base Class: <type 'function'>
> String Form: <function _deco at 0x868a5a4>
> Namespace: Interactive
> File: /home/tsviki/exem.py
> Definition: exem.myfunc(*args, **kwargs)
> Docstring:
>     decorator inner function docstring
>
> and if I try to call ??, it just gives me the decorator inner function.
> Is there a way to overlap this?
> linking the decorator __doc__ to myfunc.__doc__ won't help in case that I
> have several functions that use the same decorator.
> Any help?

hi,
decorate your decorator :)
http://pypi.python.org/pypi/decorator

i think something like:

from decorator import decorator

@decorator
def deco(f):
    '''decorator docstring'''
    @decorator
    def _deco(*args,**kwargs):
        '''decorator inner function docstring'''
        print 'args=',args
        return f(*args,**kwargs)
    return _deco

> Thanks a lot
> Tsviki Hirsh
>
> _______________________________________________
> IPython-user mailing list
> IPython-user <at> scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-user
>
>
_______________________________________________
IPython-user mailing list
IPython-user <at> scipy.org
http://mail.scipy.org/mailman/listinfo/ipython-user
Kenneth Arnold | 5 Jan 15:42
Picon

Re: docstring of decorated functions

func? just prints func.__doc__, nothing magic.

There's a simple, standard-library way to make wrapper functions: functools.wraps
http://docs.python.org/library/functools.html#functools.wraps

-Ken


On Tue, Jan 5, 2010 at 2:08 AM, Brent Pedersen <bpederse <at> gmail.com> wrote:
On Mon, Jan 4, 2010 at 10:53 PM, Tsviki Hirsh <tsviki.hirsh <at> gmail.com> wrote:
> Dear list,
> The magic ? and ?? commands are among the most useful features in ipython
> (at least for me),
> I'm also using them to read my selfdocumentation on my own functions.
> Therefore I was surprised to learn that you cannot read the doc string of a
> decorated function using ? or ??
> for example, consider this decorator and function
>
> def deco(f):
> '''decorator docstring'''
> def _deco(*args,**kwargs):
> '''decorator inner function docstring'''
> print 'args=',args
> return f(*args,**kwargs)
> return _deco
> <at> deco
> def myfunc(x,y):
> '''function docstring'''
> return x+y
>
> if I try to call myfunc docstring using ? I get:
>
> In [18]: exem.myfunc?
> Type: function
> Base Class: <type 'function'>
> String Form: <function _deco at 0x868a5a4>
> Namespace: Interactive
> File: /home/tsviki/exem.py
> Definition: exem.myfunc(*args, **kwargs)
> Docstring:
>     decorator inner function docstring
>
> and if I try to call ??, it just gives me the decorator inner function.
> Is there a way to overlap this?
> linking the decorator __doc__ to myfunc.__doc__ won't help in case that I
> have several functions that use the same decorator.
> Any help?

hi,
decorate your decorator :)
http://pypi.python.org/pypi/decorator

i think something like:

from decorator import decorator

<at> decorator
def deco(f):
   '''decorator docstring'''
    <at> decorator
   def _deco(*args,**kwargs):
       '''decorator inner function docstring'''
       print 'args=',args
       return f(*args,**kwargs)
   return _deco




> Thanks a lot
> Tsviki Hirsh
>
> _______________________________________________
> IPython-user mailing list
> IPython-user <at> scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-user
>
>
_______________________________________________
IPython-user mailing list
IPython-user <at> scipy.org
http://mail.scipy.org/mailman/listinfo/ipython-user

_______________________________________________
IPython-user mailing list
IPython-user <at> scipy.org
http://mail.scipy.org/mailman/listinfo/ipython-user
Tsviki Hirsh | 6 Jan 07:15
Picon

Re: docstring of decorated functions

Great!
The functools.wraps is exactly what I needed, and it works perfectly.
Thanks.

On Tue, Jan 5, 2010 at 4:42 PM, Kenneth Arnold <kenneth.arnold <at> gmail.com> wrote:
func? just prints func.__doc__, nothing magic.

There's a simple, standard-library way to make wrapper functions: functools.wraps
http://docs.python.org/library/functools.html#functools.wraps

-Ken


On Tue, Jan 5, 2010 at 2:08 AM, Brent Pedersen <bpederse <at> gmail.com> wrote:
On Mon, Jan 4, 2010 at 10:53 PM, Tsviki Hirsh <tsviki.hirsh <at> gmail.com> wrote:
> Dear list,
> The magic ? and ?? commands are among the most useful features in ipython
> (at least for me),
> I'm also using them to read my selfdocumentation on my own functions.
> Therefore I was surprised to learn that you cannot read the doc string of a
> decorated function using ? or ??
> for example, consider this decorator and function
>
> def deco(f):
> '''decorator docstring'''
> def _deco(*args,**kwargs):
> '''decorator inner function docstring'''
> print 'args=',args
> return f(*args,**kwargs)
> return _deco
> <at> deco
> def myfunc(x,y):
> '''function docstring'''
> return x+y
>
> if I try to call myfunc docstring using ? I get:
>
> In [18]: exem.myfunc?
> Type: function
> Base Class: <type 'function'>
> String Form: <function _deco at 0x868a5a4>
> Namespace: Interactive
> File: /home/tsviki/exem.py
> Definition: exem.myfunc(*args, **kwargs)
> Docstring:
>     decorator inner function docstring
>
> and if I try to call ??, it just gives me the decorator inner function.
> Is there a way to overlap this?
> linking the decorator __doc__ to myfunc.__doc__ won't help in case that I
> have several functions that use the same decorator.
> Any help?

hi,
decorate your decorator :)
http://pypi.python.org/pypi/decorator

i think something like:

from decorator import decorator

<at> decorator
def deco(f):
   '''decorator docstring'''
    <at> decorator
   def _deco(*args,**kwargs):
       '''decorator inner function docstring'''
       print 'args=',args
       return f(*args,**kwargs)
   return _deco




> Thanks a lot
> Tsviki Hirsh
>
> _______________________________________________
> IPython-user mailing list
> IPython-user <at> scipy.org
> http://mail.scipy.org/mailman/listinfo/ipython-user
>
>
_______________________________________________
IPython-user mailing list
IPython-user <at> scipy.org
http://mail.scipy.org/mailman/listinfo/ipython-user


_______________________________________________
IPython-user mailing list
IPython-user <at> scipy.org
http://mail.scipy.org/mailman/listinfo/ipython-user
Philip Gatt | 6 Jan 08:25
Picon

How to use ipython's IPShellEmbed from within a running doctest

Please help me get an embedded ipython console to run inside a doctest. The example code demonstrates the
problem and will hang your terminal. On bash shell I type ctrl-Z and then kill %1 to break out and kill, since
ctrl-C won't work.

def some_function():
    """
    >>> some_function()
    'someoutput'
    """
    # now try to drop into an ipython shell to help 
    # with development
    import IPython.Shell; IPython.Shell.IPShellEmbed(argv=[])()
    return 'someoutput'

if __name__ == '__main__':
    import doctest
    print "Running doctest . . ."
    doctest.testmod()

I like to use ipython to help write code. A common trick is to use ipython as a breakpoint in my code by calling
IPython.Shell.IPShellEmbed. This trick works everywhere I've tried (inside a django manage.py
runserver, unit tests), but it doesn't work within doctests. I think it has to do with doctest controlling stdin/stdout.

Thanks in advance for your help. 
- Philip
Fernando Perez | 6 Jan 08:48
Picon
Gravatar

Re: How to use ipython's IPShellEmbed from within a running doctest

Hi Philip,

On Tue, Jan 5, 2010 at 11:25 PM, Philip Gatt <gattster <at> gmail.com> wrote:
> Please help me get an embedded ipython console to run inside a doctest. The example code demonstrates the
problem and will hang your terminal. On bash shell I type ctrl-Z and then kill %1 to break out and kill, since
ctrl-C won't work.
>
> def some_function():
>    """
>    >>> some_function()
>    'someoutput'
>    """
>    # now try to drop into an ipython shell to help
>    # with development
>    import IPython.Shell; IPython.Shell.IPShellEmbed(argv=[])()
>    return 'someoutput'
>
> if __name__ == '__main__':
>    import doctest
>    print "Running doctest . . ."
>    doctest.testmod()
>
> I like to use ipython to help write code. A common trick is to use ipython as a breakpoint in my code by calling
IPython.Shell.IPShellEmbed. This trick works everywhere I've tried (inside a django manage.py
runserver, unit tests), but it doesn't work within doctests. I think it has to do with doctest controlling stdin/stdout.
>
> Thanks in advance for your help.

Thanks for the report, it's now:
https://bugs.launchpad.net/ipython/+bug/503698

Here's a workaround you can use for now, just put this little subclass
in your utilities:

import sys

from IPython.Shell import IPShellEmbed

class IPShellDoctest(IPShellEmbed):
    def __call__(self, *a, **kw):
        sys_stdout_saved = sys.stdout
        sys.stdout = sys.stderr
        try:
            IPShellEmbed.__call__(self, *a, **kw)
        finally:
            sys.stdout = sys_stdout_saved

def some_function():
   """
   >>> some_function()
   'someoutput'
   """
   # now try to drop into an ipython shell to help
   # with development
   IPShellDoctest()()
   return 'someoutput'

if __name__ == '__main__':
   import doctest
   print "Running doctest . . ."
   doctest.testmod()
Philip Gatt | 6 Jan 09:23
Picon

Re: How to use ipython's IPShellEmbed from within a running doctest

Awesome, thanks for the help. 

On Jan 5, 2010, at 11:48 PM, Fernando Perez wrote:

> Hi Philip,
> 
> On Tue, Jan 5, 2010 at 11:25 PM, Philip Gatt <gattster <at> gmail.com> wrote:
>> Please help me get an embedded ipython console to run inside a doctest. The example code demonstrates the
problem and will hang your terminal. On bash shell I type ctrl-Z and then kill %1 to break out and kill, since
ctrl-C won't work.
>> 
>> def some_function():
>>    """
>>    >>> some_function()
>>    'someoutput'
>>    """
>>    # now try to drop into an ipython shell to help
>>    # with development
>>    import IPython.Shell; IPython.Shell.IPShellEmbed(argv=[])()
>>    return 'someoutput'
>> 
>> if __name__ == '__main__':
>>    import doctest
>>    print "Running doctest . . ."
>>    doctest.testmod()
>> 
>> I like to use ipython to help write code. A common trick is to use ipython as a breakpoint in my code by
calling IPython.Shell.IPShellEmbed. This trick works everywhere I've tried (inside a django
manage.py runserver, unit tests), but it doesn't work within doctests. I think it has to do with doctest
controlling stdin/stdout.
>> 
>> Thanks in advance for your help.
> 
> Thanks for the report, it's now:
> https://bugs.launchpad.net/ipython/+bug/503698
> 
> 
> Here's a workaround you can use for now, just put this little subclass
> in your utilities:
> 
> import sys
> 
> from IPython.Shell import IPShellEmbed
> 
> class IPShellDoctest(IPShellEmbed):
>    def __call__(self, *a, **kw):
>        sys_stdout_saved = sys.stdout
>        sys.stdout = sys.stderr
>        try:
>            IPShellEmbed.__call__(self, *a, **kw)
>        finally:
>            sys.stdout = sys_stdout_saved
> 
> 
> def some_function():
>   """
>>>> some_function()
>   'someoutput'
>   """
>   # now try to drop into an ipython shell to help
>   # with development
>   IPShellDoctest()()
>   return 'someoutput'
> 
> if __name__ == '__main__':
>   import doctest
>   print "Running doctest . . ."
>   doctest.testmod()
John Hunter | 11 Jan 04:04
Picon
Gravatar

recalling indented blocks -- strange bug

I am having a surprising problem on ubuntu karmic (9.10) 32bit using
ipython 0.10 (same in the development branch BTW)   In the session
below, I type::

for i in  range(10):
    print i

relying on ipython to autoindent the print line for me -- this works
fine.  But if I then "up arrow" to recall the entire "for" block, the
block execution fails with an indentation error.  Something about
recalling the block from history is causing it to lose the proper
indentation.  This is something I do a lot (on other machines) and
have not seen this so it may be platform/version specific.

msierig <at> pinchiepie:~> ipython
Python 2.6.4 (r264:75706, Dec  7 2009, 18:45:15)
Type "copyright", "credits" or "license" for more information.

IPython 0.10 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object'. ?object also works, ?? prints more.

In [1]: for i in range(10):
   ...:     print i
   ...:
   ...:
0
1
2
3
4
5
6
7
8
9

# this input clock came from readline history
In [2]: for i in range(10):
    print i
------> print(i)
------------------------------------------------------------
IndentationError: expected an indented block (<ipython console>, line 2)

In [4]: import IPython

In [5]: print IPython.__file__
------> print(IPython.__file__)
/usr/lib/pymodules/python2.6/IPython/__init__.pyc

In [6]:
Fernando Perez | 11 Jan 04:34
Picon
Gravatar

Re: recalling indented blocks -- strange bug

On Sun, Jan 10, 2010 at 7:04 PM, John Hunter <jdh2358 <at> gmail.com> wrote:
> I am having a surprising problem on ubuntu karmic (9.10) 32bit using
> ipython 0.10 (same in the development branch BTW)   In the session
> below, I type::
>
> for i in  range(10):
>    print i
>
> relying on ipython to autoindent the print line for me -- this works
> fine.  But if I then "up arrow" to recall the entire "for" block, the
> block execution fails with an indentation error.  Something about
> recalling the block from history is causing it to lose the proper
> indentation.  This is something I do a lot (on other machines) and
> have not seen this so it may be platform/version specific.

It's this stupid problem:

https://bugs.launchpad.net/ipython/+bug/414967

Your options:

- disable autocall permanently

- start using the future print_function and print() everywhere (except
for single-lines, where autocall works for you)

Here, on dev-branch:

In [2]: for i in range(10):
    print i,
------> print(i,)
------------------------------------------------------------
IndentationError: expected an indented block (<ipython console>, line 2)

In [4]: autocall 0
Automatic calling is: OFF

# now it's ok:
In [5]: for i in range(10):
    print i,
   ...:
0 1 2 3 4 5 6 7 8 9

Since we'll have to get used to print() anyway, might as well do it early...

Cheers,

f
Fernando Perez | 11 Jan 04:54
Picon
Gravatar

Re: recalling indented blocks -- strange bug

> Your options:
>
> - disable autocall permanently
>
> - start using the future print_function and print() everywhere (except
> for single-lines, where autocall works for you)

Or, update to trunk-dev as soon as I commit :) Fixed:

In [1]: for i in range(10):
   ...:     print i,
   ...:
0 1 2 3 4 5 6 7 8 9

In [2]: for i in range(10):
    print i,
   ...:
0 1 2 3 4 5 6 7 8 9

#####

I'll ping when it's committed so you can pull.  I'm afraid I may not
backport this to 0.10.1, at least not right away (no time, I'll take
patches if anyone does the backport, but that part of the code is
substantially rewritten so it's not a simple copy/paste).

Cheers,

f

Gmane