I would like to improve Gaphas 0.5.0 canvas connections API.
First of all I would settle on terminology. At the moment canvas operates
in terms of
- hitem - item (line at the moment) connecting with a handle to another
item
- pitem - item to which a line is connected to
I would change above to
- item - "connecting item" shall be used in documentation (usually a line
at the moment)
- connected - "connected item" shall be used in documentation
There is a concept of namedtuple introduced in Python 2.6
I would change gaphas.table.Table to accept namedtuple instead of a tuple
and I would change connections cache declaration
Connection = namedtuple('Connection', 'item handle connected port constraint callback')
self._connections = table.Table(Connection, (0, 1, 2, 3))
Then I would remove methods
- Canvas.get_connected_to
- Canvas.get_connected_data
- Canvas.get_connected_items
and replace them with
Canvas.get_connection(handle)
Canvas.get_connections(item=None, handle=None, connected=None, port=None)
To get connected item to a handle (former Handle.connected_to)
c = canvas.get_connection(handle)
if c != None:
print c.connected
print c.port
print c.constraint
To get all connected items (i.e. items on both sides of a line)
classes = (i.connected for i in canvas.get_connections(item=line))
To get connecting items (i.e. all lines connected to a class)
lines = (c.item for c in canvas.get_connections(connected=item, port=port))
Any thoughts?
Best regards,
wrobell <wro...@pld-linux.org>
On 4 Oct 2009, at 01:56, wrobell wrote:
>
> Hi,
>
> I would like to improve Gaphas 0.5.0 canvas connections API.
>
> First of all I would settle on terminology. At the moment canvas
> operates
> in terms of
>
> - hitem - item (line at the moment) connecting with a handle to
> another
> item
> - pitem - item to which a line is connected to
>
> I would change above to
>
> - item - "connecting item" shall be used in documentation (usually a
> line
> at the moment)
> - connected - "connected item" shall be used in documentation
>
>
> There is a concept of namedtuple introduced in Python 2.6
>
> http://docs.python.org/dev/library/collections.html#namedtuple-factory-function-for-tuples-with-named-fields
>
> I would change gaphas.table.Table to accept namedtuple instead of a
> tuple
> and I would change connections cache declaration
>
> Connection = namedtuple('Connection', 'item handle connected port
> constraint callback')
> self._connections = table.Table(Connection, (0, 1, 2, 3))
That's a good idea.
> Then I would remove methods
>
> - Canvas.get_connected_to
> - Canvas.get_connected_data
> - Canvas.get_connected_items
>
> and replace them with
>
> Canvas.get_connection(handle)
> Canvas.get_connections(item=None, handle=None, connected=None,
> port=None)
>
> To get connected item to a handle (former Handle.connected_to)
>
> c = canvas.get_connection(handle)
> if c != None:
> print c.connected
> print c.port
> print c.constraint
>
>
> To get all connected items (i.e. items on both sides of a line)
>
> classes = (i.connected for i in canvas.get_connections(item=line))
>
>
> To get connecting items (i.e. all lines connected to a class)
>
> lines = (c.item for c in canvas.get_connections(connected=item,
> port=port))
>
>
> Any thoughts?
This sounds good! I was not aware of namedtuple when I wrote the
connection code. The get_connected_* methods were introduced partly
because returning a tuple of about 5 items is not something you want
client code to deal with.
Using namedtuple takes away this problem.
Regards,
Arjan