Mercurial > hg > WSGraph
annotate wsgraph/model.py @ 10:81d68388ec97
some nonsense about how this model is full of holes and how it should be better
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Mon, 10 Dec 2012 17:14:49 -0800 |
parents | 0affca1f4dc0 |
children | 7b8e40eda563 |
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 |
8 | 3 from utils import iterable |
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 """ |
0 | 15 |
16 @abstractmethod | |
17 def nodes(self): | |
18 """returns a list of all nodes""" | |
19 | |
20 @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
|
21 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
|
22 """ |
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
|
23 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
|
24 """ |
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 @abstractmethod |
0 | 27 def edges(self): |
28 """returns a list of all edges""" | |
29 | |
30 def __getitem__(self, key): | |
31 """ | |
32 if key is a basestring, return the node of that name; | |
33 if key is a 2-tuple/list, return the edge of that name | |
34 """ | |
35 | |
8 | 36 if isinstance(key, basestring) or (not iterable(key)): |
37 return self.node(key) | |
38 else: | |
39 return self.edge(*key) | |
40 | |
41 def __contains__(self, key): | |
42 """ | |
43 if key is ..., returns if that node is in the graph | |
44 if key is a 2-tuple/list, returns if the edge is in the graph | |
45 """ | |
46 # XXX not necessarily the best implementation! | |
47 if isinstance(key, basestring) or (not iterable(key)): | |
48 return key in self.nodes() | |
49 else: | |
50 return tuple(key) in self.edges() | |
0 | 51 |
52 class MemoryCache(GraphModel): | |
53 | |
54 def __init__(self): | |
55 self._edges = {} | |
56 self._nodes = {} | |
57 | |
58 def node(self, name, **values): | |
59 if values: | |
60 # setter | |
9
0affca1f4dc0
start using deepcopy since lord knows we cant trust users
Jeff Hammel <jhammel@mozilla.com>
parents:
8
diff
changeset
|
61 self._nodes[name] = deepcopy(values) |
0 | 62 else: |
63 # getter | |
9
0affca1f4dc0
start using deepcopy since lord knows we cant trust users
Jeff Hammel <jhammel@mozilla.com>
parents:
8
diff
changeset
|
64 # TODO: deepcopy |
0 | 65 return self._nodes.get(name, None) |
66 | |
67 def nodes(self): | |
68 return self._nodes.keys() | |
69 | |
70 def edge(self, node1, node2, **values): | |
71 if values: | |
72 # setter | |
9
0affca1f4dc0
start using deepcopy since lord knows we cant trust users
Jeff Hammel <jhammel@mozilla.com>
parents:
8
diff
changeset
|
73 self._edges[(node1, node2)] = deepcopy(values) |
0 | 74 else: |
75 # getter | |
9
0affca1f4dc0
start using deepcopy since lord knows we cant trust users
Jeff Hammel <jhammel@mozilla.com>
parents:
8
diff
changeset
|
76 # TODO: deepcopy |
0 | 77 return self._edges.get((node1, node2), None) |
78 | |
79 def edges(self): | |
80 return self._edges.keys() |