Mercurial > hg > WSGraph
comparison wsgraph/model.py @ 39:d1e9602145fa
rudimentary deletion and notes to self
| author | Jeff Hammel <jhammel@mozilla.com> |
|---|---|
| date | Tue, 25 Dec 2012 12:46:39 -0800 |
| parents | df2a719a9b6e |
| children |
comparison
equal
deleted
inserted
replaced
| 38:df2a719a9b6e | 39:d1e9602145fa |
|---|---|
| 102 class DirectedGraph(Graph): | 102 class DirectedGraph(Graph): |
| 103 """mix-in class for directed graphs""" | 103 """mix-in class for directed graphs""" |
| 104 # TODO: is this possible without super or other black magicks? | 104 # TODO: is this possible without super or other black magicks? |
| 105 | 105 |
| 106 | 106 |
| 107 class MemoryCache(Graph): | 107 class MemoryCache(Graph): # TODO -> MemoryCacheGraph |
| 108 """volatile in-memory representation of a graph""" | 108 """volatile in-memory representation of a graph""" |
| 109 | |
| 110 # TODO: a subclass that pegs the type(s?) to something specific | |
| 109 | 111 |
| 110 def __init__(self, node_type=dict): | 112 def __init__(self, node_type=dict): |
| 111 self._edges = {} | 113 self._edges = {} |
| 112 self._nodes = {} | 114 self._nodes = {} |
| 113 self.node_type = node_type | 115 self.node_type = node_type |
| 136 return deepcopy(self._edges.get((node1, node2), None)) | 138 return deepcopy(self._edges.get((node1, node2), None)) |
| 137 | 139 |
| 138 def edges(self): | 140 def edges(self): |
| 139 return self._edges.keys() | 141 return self._edges.keys() |
| 140 | 142 |
| 143 def delete(self, node1, node2=None): | |
| 144 if node2 is not None: | |
| 145 # delete an edge | |
| 146 key = node1, node2 | |
| 147 del self._edges[key] | |
| 148 return | |
| 149 | |
| 150 # delete a node | |
| 151 # TODO: if a node is deleted, all edges to it should be deleted | |
| 152 del self._nodes[node1] | |
| 153 | |
| 141 | 154 |
| 142 class FileCache(MemoryCache): | 155 class FileCache(MemoryCache): |
| 143 """on-disk JSON file cache""" | 156 """on-disk JSON file cache""" |
| 144 | 157 |
| 145 def __init__(self, filename): | 158 def __init__(self, filename): |
