[IPython-dev] ipipe news

Walter Dörwald walter at livinglogic.de
Wed Mar 1 15:25:03 EST 2006

Ville Vainio wrote:
> On 3/1/06, Walter Dörwald <walter at livinglogic.de> wrote:
>> Then you can do:
>>      t = OnTheFlyTable("foo", "bar")
>>      [t(foo="foo", bar="bar"), t(foo="hello", bar="world")] | ibrowse
> I'd like the Table to hold the sequence, so that ibrowse would be
> launched automatically (which doesn't happen with plain lists and
> tuples).
> For example, I might change %alias, when called w/o args, to return a
> Table, with name & target fields. That way the user would be thrown
> into ibrowse without having to invoke it manually.

OK, understood.

Try something like this:

from IPython.Extensions import ipipe

class Fields(object):
     def __init__(self, table, **fields):
         self.__table = table
         for (key, value) in fields.iteritems():
             setattr(self, key, value)

     def __xattrs__(self, mode):
         return self.__table.fields

class FieldTable(ipipe.Table, list):
      def __init__(self, *fields):
          self.fields = fields

      def add(self, **fields):
          self.append(Fields(self, **fields))

      def __xiter__(self, mode):
          return list.__iter__(self)

      def __xrepr__(self, mode):
          if mode == "header" or mode == "footer":
              return "FieldTable(%r)" % ", ".join(map(repr, self.fields))
          return repr(self)

Then you can do what you requested:

 >>> f = FieldTable("name", "table")
 >>> f.add(foo="foo", bar="bar")
 >>> f.add(foo="hello", bar="world")
 >>> f

I'll something like this to ipipe.py

    Walter Dörwald

More information about the IPython-dev mailing list