On Mar 6, 3:02 pm, Alasdair <amc...@gmail.com> wrote:
> However, it doesn't work.  In console mode show_parent() should turn
> the display of parents on, and hide_parent() should turn it off.  It
> seems to work once - but not again.  That is, parent display can be
> turned on, and then off, but not on again - show_parent() has no
> effect after one "cycle". I'm not quite sure what's going on - my
> python knowledge is limited.  And sometimes when parent display is on,
> hide_parent() has no effect.
>
> Any ideas?

This is because .set_hook() pushes your function onto a list of
handlers; the first handler in the list that does not raise a TryNext
exception is used.  So you keep adding more and more copies of
repr_and_parent and repr_only onto the list.

Here's a variant that works for me (in limited testing).

import IPython
from IPython import ipapi
from IPython.genutils import Term
from pprint import PrettyPrinter

pformat = PrettyPrinter().pformat

_show_parent_mode = False

def _maybe_show_parent(self, arg):
    if _show_parent_mode and hasattr(arg, 'parent'):
        try:
            print >>Term.cout, "%s\n\nParent: %s"%(repr(arg),repr
(arg.parent()))
            return
        except:
            pass
    raise ipapi.TryNext

ipapi.get().set_hook("result_display", _maybe_show_parent)

def show_parent():
    global _show_parent_mode
    _show_parent_mode = True

def hide_parent():
    global _show_parent_mode
    _show_parent_mode = False

--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to sage-support@googlegroups.com
To unsubscribe from this group, send email to 
sage-support-unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sage-support
URLs: http://www.sagemath.org
-~----------~----~----~----~------~----~------~--~---

Reply via email to