comparison wsgraph/model.py @ 8:f1f7a505e0d0

__contains__ method
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 10 Dec 2012 17:08:02 -0800
parents cfcfa093e4b4
children 0affca1f4dc0
comparison
equal deleted inserted replaced
7:9016b126aa87 8:f1f7a505e0d0
1 from abc import abstractmethod 1 from abc import abstractmethod
2 from utils import iterable
2 3
3 class GraphModel(object): 4 class GraphModel(object):
4 5
5 @abstractmethod 6 @abstractmethod
6 def node(self, name, **values): 7 def node(self, name, **values):
22 """ 23 """
23 if key is a basestring, return the node of that name; 24 if key is a basestring, return the node of that name;
24 if key is a 2-tuple/list, return the edge of that name 25 if key is a 2-tuple/list, return the edge of that name
25 """ 26 """
26 27
28 if isinstance(key, basestring) or (not iterable(key)):
29 return self.node(key)
30 else:
31 return self.edge(*key)
32
33 def __contains__(self, key):
34 """
35 if key is ..., returns if that node is in the graph
36 if key is a 2-tuple/list, returns if the edge is in the graph
37 """
38 # XXX not necessarily the best implementation!
39 if isinstance(key, basestring) or (not iterable(key)):
40 return key in self.nodes()
41 else:
42 return tuple(key) in self.edges()
27 43
28 class MemoryCache(GraphModel): 44 class MemoryCache(GraphModel):
29 45
30 def __init__(self): 46 def __init__(self):
31 self._edges = {} 47 self._edges = {}