Mercurial > hg > WSGraph
comparison wsgraph/model.py @ 15:ee45f44394a0
i need a name, Bastian
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Mon, 10 Dec 2012 17:33:44 -0800 |
parents | 7b8e40eda563 |
children | 24d57daaca21 |
comparison
equal
deleted
inserted
replaced
14:3d0430390e72 | 15:ee45f44394a0 |
---|---|
1 from abc import abstractmethod | 1 from abc import abstractmethod |
2 from copy import deepcopy | 2 from copy import deepcopy |
3 from utils import iterable | 3 from utils import isiterable |
4 | 4 |
5 class GraphModel(object): | 5 class GraphModel(object): |
6 | 6 |
7 @abstractmethod | 7 @abstractmethod |
8 def node(self, name, **values): | 8 def node(self, name, **values): |
34 """ | 34 """ |
35 if key is a basestring, return the node of that name; | 35 if key is a basestring, return the node of that name; |
36 if key is a 2-tuple/list, return the edge of that name | 36 if key is a 2-tuple/list, return the edge of that name |
37 """ | 37 """ |
38 | 38 |
39 if isinstance(key, basestring) or (not iterable(key)): | 39 if isinstance(key, basestring) or (not isiterable(key)): |
40 return self.node(key) | 40 return self.node(key) |
41 else: | 41 else: |
42 return self.edge(*key) | 42 return self.edge(*key) |
43 | 43 |
44 def __contains__(self, key): | 44 def __contains__(self, key): |
45 """ | 45 """ |
46 if key is ..., returns if that node is in the graph | 46 if key is ..., returns if that node is in the graph |
47 if key is a 2-tuple/list, returns if the edge is in the graph | 47 if key is a 2-tuple/list, returns if the edge is in the graph |
48 """ | 48 """ |
49 # XXX not necessarily the best implementation! | 49 # XXX not necessarily the best implementation! |
50 if isinstance(key, basestring) or (not iterable(key)): | 50 if isinstance(key, basestring) or (not isiterable(key)): |
51 return key in self.nodes() | 51 return key in self.nodes() |
52 else: | 52 else: |
53 return tuple(key) in self.edges() | 53 return tuple(key) in self.edges() |
54 | 54 |
55 | |
55 class MemoryCache(GraphModel): | 56 class MemoryCache(GraphModel): |
57 """volatile in-memory representation of a graph""" | |
56 | 58 |
57 def __init__(self): | 59 def __init__(self): |
58 self._edges = {} | 60 self._edges = {} |
59 self._nodes = {} | 61 self._nodes = {} |
60 | 62 |