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
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
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
f1f7a505e0d0 __contains__ method
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
3 from utils import iterable
0
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5 class GraphModel(object):
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
7 @abstractmethod
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
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
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
15
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
16 @abstractmethod
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
17 def nodes(self):
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
18 """returns a list of all nodes"""
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
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
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27 def edges(self):
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 """returns a list of all edges"""
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
30 def __getitem__(self, key):
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
31 """
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
32 if key is a basestring, return the node of that name;
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
33 if key is a 2-tuple/list, return the edge of that name
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
34 """
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
35
8
f1f7a505e0d0 __contains__ method
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
36 if isinstance(key, basestring) or (not iterable(key)):
f1f7a505e0d0 __contains__ method
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
37 return self.node(key)
f1f7a505e0d0 __contains__ method
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
38 else:
f1f7a505e0d0 __contains__ method
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
39 return self.edge(*key)
f1f7a505e0d0 __contains__ method
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
40
f1f7a505e0d0 __contains__ method
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
41 def __contains__(self, key):
f1f7a505e0d0 __contains__ method
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
42 """
f1f7a505e0d0 __contains__ method
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
43 if key is ..., returns if that node is in the graph
f1f7a505e0d0 __contains__ method
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
44 if key is a 2-tuple/list, returns if the edge is in the graph
f1f7a505e0d0 __contains__ method
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
45 """
f1f7a505e0d0 __contains__ method
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
46 # XXX not necessarily the best implementation!
f1f7a505e0d0 __contains__ method
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
47 if isinstance(key, basestring) or (not iterable(key)):
f1f7a505e0d0 __contains__ method
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
48 return key in self.nodes()
f1f7a505e0d0 __contains__ method
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
49 else:
f1f7a505e0d0 __contains__ method
Jeff Hammel <jhammel@mozilla.com>
parents: 0
diff changeset
50 return tuple(key) in self.edges()
0
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52 class MemoryCache(GraphModel):
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
53
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
54 def __init__(self):
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
55 self._edges = {}
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
56 self._nodes = {}
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
57
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
58 def node(self, name, **values):
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
59 if values:
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
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
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
62 else:
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
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
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
65 return self._nodes.get(name, None)
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
66
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
67 def nodes(self):
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
68 return self._nodes.keys()
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
69
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
70 def edge(self, node1, node2, **values):
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
71 if values:
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
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
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
74 else:
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
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
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
77 return self._edges.get((node1, node2), None)
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
78
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
79 def edges(self):
cfcfa093e4b4 initial commit
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
80 return self._edges.keys()