[IPython-dev] ipipe news

Walter Dörwald walter at livinglogic.de
Thu Mar 2 15:45:22 EST 2006


John Hunter wrote:
>>>>>> "Walter" == Walter Dörwald <walter at livinglogic.de> writes:
>     >> This morning I was thinking about the usefulness of an object
>     >> browser class "iobj", that does a dir on an object and reports
>     >> the attribute names in the first class and the attribute values
>     >> in the right.  Something like myobj | ifilter("not
>     >> attr.startswith('_')") | ibrowse
> 
>     Walter> You should be able to do: myobj.__dict__ | ifilter("not
>     Walter> key.startswith('_')") | ibrowse
> 
>     Walter> Unfortunately this is currently broken, but I sent a patch
>     Walter> to Ville a few minutes ago that fixes this problem.
> 
> Yep, that patch was in response to me playing with o.__dict__|ibrowse,
> I think.

Indeed!

> I was looking for something a bit broader than __dict__ though, namely
> something that uses dir(obj) combined with getattr.

How about something like this:
import ipipe

class iinspect(ipipe.Pipe):
     def __xiter__(self, mode):
         fields = ("key", "value")
         for key in dir(self.input):
             yield ipipe.Fields(
                fields,
                key=key,
                value=getattr(self.input, key)
             )

With this you can do:
 >>> 42 | iinspect

(Although this isn't a real pipe any more, as the input isn't really 
iterable)

> The motivation is that matplotlib has an object introspection
> mechanism by doing a dir on objects and looking for names that have
> set_something or get_something, where something is a property (we
> basically have our own hacked up version of python properties).  A
> custom ifilter could be written to browse these properties, by
> filtering on callable attrs that start with 'set_'

You can hack the iinspect class from above, to give you something like that.

> Here's a little example of using setp, the object inspector, in
> pylab.  I was hoping to hack up a ipipe version...
> 
> In [4]: line, = plot(rand(10))
> 
> In [5]: [a for a in dir(line) if a.startswith('set_') and callable(getattr(line, a))]
> Out[5]:
> ['set_aa', 'set_alpha', 'set_animated', 'set_antialiased', 'set_c',
>  'set_clip_box', 'set_clip_on', 'set_color', 'set_dash_capstyle',
>  'set_dash_joinstyle', 'set_dashes', 'set_data', 'set_figure',
>  'set_label', 'set_linestyle', 'set_linewidth', 'set_lod', 'set_ls',
>  'set_lw', 'set_marker', 'set_markeredgecolor', 'set_markeredgewidth',
>  'set_markerfacecolor', 'set_markersize', 'set_mec', 'set_mew',
>  'set_mfc', 'set_ms', 'set_solid_capstyle', 'set_solid_joinstyle',
>  'set_transform', 'set_visible', 'set_xdata', 'set_ydata',
>  'set_zorder']

The following should do it:

class iinspect(ipipe.Pipe):
     def __xiter__(self, mode):
         fields = ("key", "value")
         for key in dir(self.input):
            if key.startswith("set_"):
                value = getattr(self.input, key)
                    if callable(value):
                        yield ipipe.Fields(fields, key=key, value=value)

> In [6]: setp(line)
>     alpha: float
>     animated: [True | False]
>     antialiased or aa: [True | False]
>     clip_box: a matplotlib.transform.Bbox instance
>     clip_on: [True | False]
>     color or c: any matplotlib color - see help(colors)
>     dash_capstyle: ['butt' | 'round' | 'projecting']
>     dash_joinstyle: ['miter' | 'round' | 'bevel']
>     dashes: sequence of on/off ink in points
>     data: (array xdata, array ydata)
>     figure: a matplotlib.figure.Figure instance
>     label: any string
>     linestyle or ls: [ '-' | '--' | '-.' | ':' | 'steps' | 'None' ]
>     linewidth or lw: float value in points
>     lod: [True | False]
>     marker: [ '+' | ',' | '.' | '1' | '2' | '3' | '4' | '<' | '>' |
>     'D' | 'H' | '^' | '_' | 'd' | 'h' | 'o' | 'p' | 's' | 'v' | 'x' |
>     '|' ]
>     markeredgecolor or mec: any matplotlib color - see help(colors)
>     markeredgewidth or mew: float value in points
>     markerfacecolor or mfc: any matplotlib color - see help(colors)
>     markersize or ms: float
>     solid_capstyle: ['butt' | 'round' |  'projecting']
>     solid_joinstyle: ['miter' | 'round' | 'bevel']
>     transform: a matplotlib.transform transformation instance
>     visible: [True | False]
>     xdata: array
>     ydata: array
>     zorder: any number

I don't know where this comes from, are these the docstrings of the 
properties?

Servus,
    Walter




More information about the IPython-dev mailing list