Commits

Jonathan Eunice committed c3d663b

refactoring equals_or_in test logic

Comments (0)

Files changed (3)

             clone.exclude.append(other)
         return clone
     
+def equals_or_in(item, collection):
+    """
+    Return true if the item is the collection, or is in the collection.
+    Guard against possible TypeError exceptions (ie, testing if `int` is
+    in `str` can raise this).
+    """
+    if item == collection:
+        return True
+    if hasattr(collection, '__contains__'):
+        try:
+            if item in collection:
+                return True
+        except TypeError:
+            pass
+    return False
+
+    
 class Any(IntensionalSet):
     """
     An item is in an Any if it is or is in any member of the set.
     def __contains__(self, item):
         if item in self.items:
             return True
-        
         for i in self.items:
-            if hasattr(i, '__contains__'):
-                if item in i:
-                    return True
-            else:
-                if item == i:
-                    return True
+            if equals_or_in(item, i):
+                return True
         return False
     
 class Every(IntensionalSet):
 
     def __contains__(self, item):
         for i in self.items:
-            if hasattr(i, '__contains__'):
-                if item not in i:
-                    return False
-            else:
-                if item != i:
-                    return False
+            if not equals_or_in(item, i):
+                return False
         return True
 
 class ButNot(IntensionalSet):
         self.items = items
         self.exclusion = exclusion
 
-    def __contains__(self, item):        # why the == self.items? 
+    def __contains__(self, item):        # why the == self.items?
         if item == self.items or item in self.items:
             if item != self.exclusion and item not in self.exclusion:
                 return True
         return False
     
+    # probably need to guard against type errors here
+    
 class EitherOr(IntensionalSet):
     """
     An item is in an EitherOr if it's in subseta or subset b, but not both.
 
 setup(
     name='intensional',
-    version=verno("0.201"),
+    version=verno("0.204"),
     author='Jonathan Eunice',
     author_email='jonathan.eunice@gmail.com',
     description='Intensional sets in Python',
     assert "laboom" not in a2
     assert 33 not in a2
     
+    any2 = Any(1,2,3, "this", "that")
+    in_out(any2,
+          [1,2,3, "this", "that"],
+          [44, 45, 'lovely', 'bones'])
+
+    
 def test_Every():
     e = Every(1)
     assert 1 in e