Graph library for Python

geremy condra debatem1 at gmail.com
Wed Dec 9 18:42:13 EST 2009


On Wed, Dec 9, 2009 at 6:04 PM, Rhodri James
<rhodri at wildebst.demon.co.uk> wrote:
> On Wed, 09 Dec 2009 03:47:03 -0000, geremy condra <debatem1 at gmail.com>
> wrote:
>
>> On Tue, Dec 8, 2009 at 8:42 PM, Rhodri James
>> <rhodri at wildebst.demon.co.uk> wrote:
>>>
>>> g = Graph(
>>>   nodes=[Node("a", colour="red"),
>>>          Node("b", colour="white"),
>>>          Node("c", colour="blue")],
>>>   edges=[Edge("a", "b", "ab", weight=2),
>>>          Edge("a", "c", "ac", is_directed=True),
>>>          Edge("b", "c", "bc", style="dotted")],
>>>   is_directed=True)
>>>
>>> I could see a use for this tracking a database structure using a constant
>>> graph, hence all set up in one go for preference.
>>
>> While I agree with the rationale, I think we need to find another way.
>> Aesthetics aside, directly instantiating edges by giving only node names
>> requires that the edge be aware of what graph its in to provide expected
>> behavior, which creates a bit of a chicken-or-the-egg dilemma.
>
> Oops.  I missed that, sorry.
>
>> How about this: the constructor can take any type of iterable, and
>> assumes that it follows my earlier format unless it specifies a .items()
>> method, in which case it takes the values as follows:
>
> isinstance(x, collections.Mapping) is perhaps the right test?

The code I kludged together last night just tries __getitem__
and it seems to work, so unless theres something I'm missing
I'll probably just leave it at that.

>> g = Graph(
>>    nodes={'a':{'colour':'red'},
>>               'b':{'colour':'white'},
>>               'c':{'colour':'blue'}},
>>    edges={('a', 'b'):{'name':'ab', 'weight':2},
>>               ('a', 'c'):{'name':'ac'},
>>               ('b', 'c'):{'name':'bc', 'style':'dotted'}}
>> )
>
> That's OK for nodes, but for consistency with add_edges I would have
> expected the name to be the optional third element of the key tuples.  It
> works either way, but I can't help feel it's beginning to look a bit ugly.

I have to admit, I prefer it the other way, but patrick (our test guru and
chief bug squasher) likes your proposal better. I'm going to get in touch
with robbie tonight and see what he says. Since this is not a feature I'll
use much, if he agrees with you then I'll go ahead and implement the
change tonight and merge it back into mainline. If not, I'd appreciate
it if you'd take another look at it and figure out if its something you can
live with, or if theres another syntax you'd prefer, etc. Fair enough?

Geremy Condra



More information about the Python-list mailing list