changeset 8:f1f7a505e0d0

__contains__ method
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 10 Dec 2012 17:08:02 -0800
parents 9016b126aa87
children 0affca1f4dc0
files wsgraph/model.py wsgraph/utils.py
diffstat 2 files changed, 26 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/wsgraph/model.py	Mon Dec 10 13:55:58 2012 -0800
+++ b/wsgraph/model.py	Mon Dec 10 17:08:02 2012 -0800
@@ -1,4 +1,5 @@
 from abc import abstractmethod
+from utils import iterable
 
 class GraphModel(object):
 
@@ -24,6 +25,21 @@
         if key is a 2-tuple/list, return the edge of that name
         """
 
+        if isinstance(key, basestring) or (not iterable(key)):
+            return self.node(key)
+        else:
+            return self.edge(*key)
+
+    def __contains__(self, key):
+        """
+        if key is ..., returns if that node is in the graph
+        if key is a 2-tuple/list, returns if the edge is in the graph
+        """
+        # XXX not necessarily the best implementation!
+        if isinstance(key, basestring) or (not iterable(key)):
+            return key in self.nodes()
+        else:
+            return tuple(key) in self.edges()
 
 class MemoryCache(GraphModel):
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wsgraph/utils.py	Mon Dec 10 17:08:02 2012 -0800
@@ -0,0 +1,10 @@
+"""
+utility functions that should be in core python
+"""
+
+def isiterable(instance):
+    try:
+        iter(instance)
+        return True
+    except TypeError:
+        return False