Mercurial > hg > WSGraph
diff wsgraph/model.py @ 0:cfcfa093e4b4
initial commit
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Sun, 09 Dec 2012 10:21:43 -0800 |
parents | |
children | f1f7a505e0d0 |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/wsgraph/model.py Sun Dec 09 10:21:43 2012 -0800 @@ -0,0 +1,54 @@ +from abc import abstractmethod + +class GraphModel(object): + + @abstractmethod + def node(self, name, **values): + """get or set a node""" + + @abstractmethod + def nodes(self): + """returns a list of all nodes""" + + @abstractmethod + def edges(self): + """returns a list of all edges""" + + @abstractmethod + def edge(self, node1, node2, **values): + """returns edge from node1 to node2""" + + def __getitem__(self, key): + """ + if key is a basestring, return the node of that name; + if key is a 2-tuple/list, return the edge of that name + """ + + +class MemoryCache(GraphModel): + + def __init__(self): + self._edges = {} + self._nodes = {} + + def node(self, name, **values): + if values: + # setter + self._nodes[name] = values + else: + # getter + return self._nodes.get(name, None) + + def nodes(self): + return self._nodes.keys() + + def edge(self, node1, node2, **values): + if values: + # setter + self._edges[(node1, node2)] = values + else: + # getter + return self._edges.get((node1, node2), None) + + def edges(self): + return self._edges.keys()