Commits

Jonathan Eunice committed 9fad3dc

more tests, more operations

  • Participants
  • Parent commits 1191235

Comments (0)

Files changed (3)

 
-import sys
+import sys, copy
 from itertools import groupby, count
 
-
 _PY3 = sys.version_info[0] > 2
 if _PY3:
     basestring = str
         
 class intspan(set):
     def __init__(self, initial=None):
-        set.__init__(self)
+        super(intspan, self).__init__()
         if initial:
             self.update(initial)
+            
+    def copy(self):
+        return copy.copy(self)
         
     def update(self, items):
-        set.update(self, self._parse_range(items))
+        super(intspan, self).update(self._parse_range(items))
         
     def discard(self, items):
         for item in self._parse_range(items):
-            set.discard(self, item)
+            super(intspan, self).discard(item)
             
     def remove(self, items):
         for item in self._parse_range(items):
-            set.remove(self, item)
+            super(intspan, self).remove(item)
             
     def add(self, items):
         for item in self._parse_range(items):
-            set.add(self, item)
-                
+            super(intspan, self).add(item)
+    
+    def issubset(self, items):
+        return super(intspan, self).issubset(self._parse_range(items))
+    
+    def issuperset(self, items):
+        return super(intspan, self).issuperset(self._parse_range(items))
+    
+    def union(self, items):
+        return intspan(super(intspan, self).union(self._parse_range(items)))
+
+    def intersection(self, items):
+        return intspan(super(intspan, self).intersection(self._parse_range(items)))
+    
+    def difference(self, items):
+        return intspan(super(intspan, self).difference(self._parse_range(items)))
+    
+    def symmetric_difference(self, items):
+        return intspan(super(intspan, self).symmetric_difference(self._parse_range(items)))
+
+    __le__  = issubset
+    __ge__  = issuperset 
+    __or__  = union
+    __and__ = intersection
+    __sub__ = difference
+    __xor__ = symmetric_difference
+    
+    def __ior__(self, items):
+        return super(intspan, self).__ior__(self._parse_range(items))
+        
+    def __iand__(self, items):
+        return super(intspan, self).__iand__(self._parse_range(items))
+
+    def __isub__(self, items):
+        return super(intspan, self).__isub__(self._parse_range(items))
+        
+    def __ixor__(self, items):
+        return super(intspan, self).__ixor__(self._parse_range(items))
+    
+    def __eq__(self, items):
+        return super(intspan, self).__eq__(self._parse_range(items))
+        
     def __iter__(self):
         """
         Iterate in ascending order.
         """
-        return iter(sorted(set.__iter__(self)))
+        return iter(sorted(super(intspan, self).__iter__()))
     
     @staticmethod
     def _parse_range(datum):
 
 setup(
     name='intspan',
-    version=verno("0.403"),
+    version=verno("0.5"),
     author='Jonathan Eunice',
     author_email='jonathan.eunice@gmail.com',
     description="Sets of integers like 1,3-7,33. Inspired by Perl's Set::IntSpan",

File test/test_intspan.py

 from intspan import intspan
 
 def test_basic():
-    tests = ['1','1-2', '1-3,9-10', '1-3,14,29,92-97']
+    s = intspan()
+    tests = ['', '1','1-2', '1-3,9-10', '1-3,14,29,92-97']
     for t in tests:
         s = intspan(t)
         assert str(s) == t
         
+def test_contains():
+    s = intspan()
+    assert 1 not in s
+    assert 100 not in s
+    assert 0 not in s
+    
+    t = intspan('1,10')
+    assert 1 in t
+    assert 10 in t
+    assert 0 not in t
+    assert 2 not in t
+    
+def test_equals():
+    s = intspan('1,3,5,7,9')
+    assert s == set([1,3,5,7,9])
+    
+def test_copy():
+    t = intspan('1,10')
+    tt = t.copy()
+    assert type(tt) == type(t)
+    assert t == tt
+    assert t is not tt
+    
 def test_merge():
     assert str(intspan('1-4,5')) ==  '1-5'
         
     assert str(s) == '1-3,14,29,92-97'
     
 def test_iteration():
+    s = intspan('92,97,96,95,0,94')
+    assert [ item for item in s ] == [0, 92, 94, 95, 96, 97]
+    assert list(s) == [0, 92, 94, 95, 96, 97]
+    assert set(s) == set([0, 92, 94, 95, 96, 97])
+  
+def test_issubset():
+    s = intspan('92,97,96,95,0,94')
+    assert s.issubset('0-100')
+    assert s.issubset(range(98))
+    assert s.issubset(range(101))
+    assert s.issubset('0, 92-100')
+    assert s.issubset( [0] + list(range(92,101)) )
+    assert s.issubset(intspan('92,97,96,95,0,94'))
+    assert s.issubset([0, 92, 94, 95, 96, 97])
+    assert not s.issubset('0-10')
+    assert not s.issubset(range(20))
+    assert not s.issubset(range(95))
+
+def test_issuperset():
+    s = intspan('0-3,7')
+    assert s.issuperset('0-2')
+    assert s.issuperset([0,1,3])
+    assert not s.issuperset(range(6))
+    assert not s.issuperset('0-6')
     
-    s = intspan('92,97,96,95,0,94')
-    l = [ item for item in s ]
-    assert l == [0, 92, 94, 95, 96, 97]
+    assert s >= intspan('0-2')
+    assert s >= intspan([0,1,3])
+    assert not s >= range(6)
+    assert not s >= intspan('0-6')
+
+def test_union():
+    s = intspan('0-3,7')
+    assert s.union('0-2') == s
+    assert list(s.union('0-2')) == [0, 1, 2, 3, 7]
+    assert list(s.union([99,101])) ==  [0, 1, 2, 3, 7, 99, 101]
+    assert s.union([99,101]) == intspan('0-3,7,99,101')
+    
+    assert s | intspan('0-2') == s.union('0-2')
+    assert s | [99,101] == s.union('99,101')
+
+def test_intersection():
+    s = intspan('1-8')
+    t = intspan('2-5')
+    u = intspan('8,100')
+    assert s.intersection(t) == intspan('2-5')
+    assert t.intersection(u) == intspan()
+    assert s.intersection(u) == intspan('8')
+    
+    assert s & t == s.intersection(t)
+    assert t & u == t.intersection(u) 
+    assert s & u == s.intersection(u) 
+
+def test_difference():
+    s = intspan('1-8')
+    t = intspan('2-5')
+    assert s.difference(t) == intspan('1,6-8')
+    assert t.difference(s) == intspan()
+    
+    assert s - t == s.difference(t)
+    assert t - s == t.difference(s)
+
+def test_symmetric_difference():
+    s = intspan('1-8')
+    t = intspan('2-5')
+    assert s.symmetric_difference(t) == intspan('1,6-8')
+    assert t.symmetric_difference(s) == intspan('1,6-8')
+    assert t.symmetric_difference(t) == intspan()
+    
+    assert s ^ t == s.symmetric_difference(t)
+    assert t ^ s == t.symmetric_difference(s)
+    assert t ^ t == t.symmetric_difference(t)
+    
+def test_increments():
+    s = intspan('50-60')
+    s |= intspan('10-20')
+    assert s == intspan('10-20,50-60')
+    s &= intspan('0-55')
+    assert s == intspan('10-20,50-55')
+    s -= intspan('16-20')
+    assert s == intspan('10-15,50-55')
+    s ^= intspan('10,99')
+    assert s == intspan('11-15,50-55,99')