Mercurial > hg > WSGraph
annotate wsgraph/model.py @ 15:ee45f44394a0
i need a name, Bastian
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Mon, 10 Dec 2012 17:33:44 -0800 |
parents | 7b8e40eda563 |
children | 24d57daaca21 |
rev | line source |
---|---|
0 | 1 from abc import abstractmethod |
9
0affca1f4dc0
start using deepcopy since lord knows we cant trust users
Jeff Hammel <jhammel@mozilla.com>
parents:
8
diff
changeset
|
2 from copy import deepcopy |
15 | 3 from utils import isiterable |
0 | 4 |
5 class GraphModel(object): | |
6 | |
7 @abstractmethod | |
8 def node(self, name, **values): | |
10
81d68388ec97
some nonsense about how this model is full of holes and how it should be better
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
9 """ |
81d68388ec97
some nonsense about how this model is full of holes and how it should be better
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
10 get or set a node |
81d68388ec97
some nonsense about how this model is full of holes and how it should be better
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
11 |
81d68388ec97
some nonsense about how this model is full of holes and how it should be better
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
12 When setting a node, a value of `None` will pop the value from |
81d68388ec97
some nonsense about how this model is full of holes and how it should be better
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
13 the nodal values |
81d68388ec97
some nonsense about how this model is full of holes and how it should be better
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
14 """ |
11 | 15 # TODO: values should not be **kwargs as you could conceivably want |
16 # to set a node or edge to an empty dict | |
17 # Instead, should be (self, name, values=None) | |
0 | 18 |
19 @abstractmethod | |
20 def nodes(self): | |
21 """returns a list of all nodes""" | |
22 | |
23 @abstractmethod | |
10
81d68388ec97
some nonsense about how this model is full of holes and how it should be better
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
24 def edge(self, node1, node2, **values): |
81d68388ec97
some nonsense about how this model is full of holes and how it should be better
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
25 """ |
81d68388ec97
some nonsense about how this model is full of holes and how it should be better
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
26 get or set edge from node1 to node2 |
81d68388ec97
some nonsense about how this model is full of holes and how it should be better
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
27 """ |
81d68388ec97
some nonsense about how this model is full of holes and how it should be better
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
28 |
81d68388ec97
some nonsense about how this model is full of holes and how it should be better
Jeff Hammel <jhammel@mozilla.com>
parents:
9
diff
changeset
|
29 @abstractmethod |
0 | 30 def edges(self): |
31 """returns a list of all edges""" | |
32 | |
33 def __getitem__(self, key): | |
34 """ | |
35 if key is a basestring, return the node of that name; | |
36 if key is a 2-tuple/list, return the edge of that name | |
37 """ | |
38 | |
15 | 39 if isinstance(key, basestring) or (not isiterable(key)): |
8 | 40 return self.node(key) |
41 else: | |
42 return self.edge(*key) | |
43 | |
44 def __contains__(self, key): | |
45 """ | |
46 if key is ..., returns if that node is in the graph | |
47 if key is a 2-tuple/list, returns if the edge is in the graph | |
48 """ | |
49 # XXX not necessarily the best implementation! | |
15 | 50 if isinstance(key, basestring) or (not isiterable(key)): |
8 | 51 return key in self.nodes() |
52 else: | |
53 return tuple(key) in self.edges() | |
0 | 54 |
15 | 55 |
0 | 56 class MemoryCache(GraphModel): |
15 | 57 """volatile in-memory representation of a graph""" |
0 | 58 |
59 def __init__(self): | |
60 self._edges = {} | |
61 self._nodes = {} | |
62 | |
63 def node(self, name, **values): | |
64 if values: | |
65 # setter | |
9
0affca1f4dc0
start using deepcopy since lord knows we cant trust users
Jeff Hammel <jhammel@mozilla.com>
parents:
8
diff
changeset
|
66 self._nodes[name] = deepcopy(values) |
0 | 67 else: |
68 # getter | |
9
0affca1f4dc0
start using deepcopy since lord knows we cant trust users
Jeff Hammel <jhammel@mozilla.com>
parents:
8
diff
changeset
|
69 # TODO: deepcopy |
0 | 70 return self._nodes.get(name, None) |
71 | |
72 def nodes(self): | |
73 return self._nodes.keys() | |
74 | |
75 def edge(self, node1, node2, **values): | |
76 if values: | |
77 # setter | |
9
0affca1f4dc0
start using deepcopy since lord knows we cant trust users
Jeff Hammel <jhammel@mozilla.com>
parents:
8
diff
changeset
|
78 self._edges[(node1, node2)] = deepcopy(values) |
0 | 79 else: |
80 # getter | |
9
0affca1f4dc0
start using deepcopy since lord knows we cant trust users
Jeff Hammel <jhammel@mozilla.com>
parents:
8
diff
changeset
|
81 # TODO: deepcopy |
0 | 82 return self._edges.get((node1, node2), None) |
83 | |
84 def edges(self): | |
85 return self._edges.keys() |