Graph library for Python
Rhodri James
rhodri at wildebst.demon.co.uk
Wed Dec 9 18:04:18 EST 2009
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?
> 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.
--
Rhodri James *-* Wildebeest Herder to the Masses
More information about the Python-list
mailing list