comparison wsgraph/model.py @ 37:f17a6577cc0d

make default type configurable
author Jeff Hammel <jhammel@mozilla.com>
date Sat, 15 Dec 2012 23:22:19 -0800
parents 16673636dcb6
children df2a719a9b6e
comparison
equal deleted inserted replaced
36:5ea58a6ea820 37:f17a6577cc0d
83 return key in self.nodes() 83 return key in self.nodes()
84 else: 84 else:
85 return tuple(key) in self.edges() 85 return tuple(key) in self.edges()
86 86
87 87
88 class DirectedGraph(Graph):
89 """mix-in class for directed graphs"""
90 # TODO: is this possible without super or other black magicks?
91
92
88 class MemoryCache(Graph): 93 class MemoryCache(Graph):
89 """volatile in-memory representation of a graph""" 94 """volatile in-memory representation of a graph"""
90 95
91 def __init__(self): 96 def __init__(self, node_type=dict):
92 self._edges = {} 97 self._edges = {}
93 self._nodes = {} 98 self._nodes = {}
99 self.node_type = node_type
94 100
95 def node(self, name, value=None): 101 def node(self, name, value=None):
96 if value is not None: 102 if value is not None:
97 # setter 103 # setter
98 self._nodes[name] = deepcopy(value) 104 self._nodes[name] = deepcopy(value)
107 def edge(self, node1, node2, value=None): 113 def edge(self, node1, node2, value=None):
108 if value is not None: 114 if value is not None:
109 # setter 115 # setter
110 self._edges[(node1, node2)] = deepcopy(value) 116 self._edges[(node1, node2)] = deepcopy(value)
111 for node in node1, node2: 117 for node in node1, node2:
112 self._nodes.setdefault(node, {}) 118 self._nodes.setdefault(node, self.node_type())
113 else: 119 else:
114 # getter 120 # getter
115 # TODO: deepcopy 121 # TODO: deepcopy
116 return deepcopy(self._edges.get((node1, node2), None)) 122 return deepcopy(self._edges.get((node1, node2), None))
117 123
118 def edges(self): 124 def edges(self):
119 return self._edges.keys() 125 return self._edges.keys()
126
120 127
121 class FileCache(MemoryCache): 128 class FileCache(MemoryCache):
122 """on-disk JSON file cache""" 129 """on-disk JSON file cache"""
123 130
124 def __init__(self, filename): 131 def __init__(self, filename):