Beispiel #1
0
def magic_print_traits(self, arg):
    """ Print the traits of an object.

"""
    try:
        from IPython.external.pretty import pretty
    except ImportError:
        import pprint
        pretty = pprint.pformat
    args = parse_argstring(magic_print_traits, arg)

    obj = get_variable(self, args.variable)
    if not hasattr(obj, 'trait_names'):
        raise UsageError('variable %r is not a HasTraits instance' % args.variable)
    from traits.has_traits import not_event
    from traits.trait_errors import TraitError
    names = obj.trait_names(type=not_event)
    names.sort()
    key_values = []
    for name in names:
        try:
            value = getattr(obj, name)
        except (AttributeError, TraitError), e:
            pvalue = '<undefined>'
        else:
            pvalue = pretty(value)
        key_values.append((name, pvalue))
Beispiel #2
0
    def __call__(self, otherself, arg):
        """Uber-pretty-printing display hook.

        Called for displaying the result to the user.
        """
        
        if self.shell.pprint:
            out = pretty.pretty(arg, verbose=self.verbose)
            if '\n' in out:
                # So that multi-line strings line up with the left column of
                # the screen, instead of having the output prompt mess up
                # their first line.                
                Term.cout.write('\n')
            print >>Term.cout, out
        else:
            raise TryNext
    def __call__(self, otherself, arg):
        """Uber-pretty-printing display hook.

        Called for displaying the result to the user.
        """

        if self.shell.pprint:
            out = pretty.pretty(arg, verbose=self.verbose)
            if '\n' in out:
                # So that multi-line strings line up with the left column of
                # the screen, instead of having the output prompt mess up
                # their first line.
                Term.cout.write('\n')
            print >> Term.cout, out
        else:
            raise TryNext
Beispiel #4
0
def pretty_result_display(self, arg):
    """ Uber-pretty-printing display hook.

    Called for displaying the result to the user.
    """

    if ip.options.pprint:
        verbose = getattr(ip.options, 'pretty_verbose', False)
        out = pretty.pretty(arg, verbose=verbose)
        if '\n' in out:
            # So that multi-line strings line up with the left column of
            # the screen, instead of having the output prompt mess up
            # their first line.
            Term.cout.write('\n')
        print >> Term.cout, out
    else:
        raise IPython.ipapi.TryNext
Beispiel #5
0
def pretty_result_display(self, arg):
    """ Uber-pretty-printing display hook.

    Called for displaying the result to the user.
    """
    
    if ip.options.pprint:
        verbose = getattr(ip.options, 'pretty_verbose', False)
        out = pretty.pretty(arg, verbose=verbose)
        if '\n' in out:
            # So that multi-line strings line up with the left column of
            # the screen, instead of having the output prompt mess up
            # their first line.                
            Term.cout.write('\n')
        print >>Term.cout, out
    else:
        raise TryNext
Beispiel #6
0
    def print_traits(self, arg):
        """ Print the traits of an object.

    """
        try:
            from IPython.external.pretty import pretty
        except ImportError:
            import pprint
            pretty = pprint.pformat
        args = parse_argstring(self.print_traits, arg)

        obj = self.get_variable(args.variable)
        if not hasattr(obj, 'trait_names'):
            raise UsageError('variable %r is not a HasTraits instance' %
                             args.variable)
        from traits.has_traits import not_event
        from traits.trait_errors import TraitError
        names = obj.trait_names(type=not_event)
        names.sort()
        key_values = []
        for name in names:
            try:
                value = getattr(obj, name)
            except (AttributeError, TraitError):
                pvalue = '<undefined>'
            else:
                pvalue = pretty(value)
            key_values.append((name, pvalue))
        if not args.group:
            print(utils.wrap_key_values(key_values))
        else:
            trait_values = dict(key_values)
            for cls in inspect.getmro(type(obj))[::-1]:
                if hasattr(cls, 'class_trait_names'):
                    local_key_values = []
                    for trait in cls.class_trait_names():
                        if trait in trait_values:
                            local_key_values.append(
                                (trait, trait_values.pop(trait)))
                    if local_key_values:
                        name = getattr(cls, '__name__', repr(cls))
                        print(name)
                        print('-' * len(name))
                        print(utils.wrap_key_values(sorted(local_key_values)))
                        print()
Beispiel #7
0
 def test_for_type(self):
     self.prd.for_type(A, a_pprinter)
     a = A()
     result = pretty.pretty(a)
     self.assertEquals(result, "<A>")
Beispiel #8
0
 def test_for_type(self):
     self.prd.for_type(A, a_pprinter)
     a = A()
     result = pretty.pretty(a)
     self.assertEquals(result, "<A>")