Jonathan Eunice avatar Jonathan Eunice committed 1b6ffc6

completed set methods and operations

Comments (0)

Files changed (4)

 Notes
 =====
 
- *  Some ``set`` operations such as ``update()``, ``add()``, ``discard()``, and
-    ``remove()`` take integer span strings as arguments. In some cases this
-    changes a method that took one item into one that takes multiple. They may
-    also take string specifications of intspans that are not proper ``intspan``
-    objects. But it's not perfectly uniform, and not all
-    ``set`` operations have been so extended.
+ *  ``intspan`` methods and operations such as ``add()`` ``discard()``, and
+    ``>=`` take integer span strings, lists, and sets as arguments, changing
+    facilities that used to take only one item into onces that take multiples,
+    inlcuding arguments that are technically string specifications rather than
+    proper ``intspan`` objects. 
     
  *  String representation based on Jeff Mercado's concise answer to `this
     StackOverflow question <http://codereview.stackexchange.com/questions/5196/grouping-consecutive-numbers-into-ranges-in-python-3-2>`_.
         
     def update(self, items):
         super(intspan, self).update(self._parse_range(items))
+        return self
+        
+    def intersection_update(self, items):
+        super(intspan, self).intersection_update(self._parse_range(items))
+        return self
+
+    def difference_update(self, items):
+        super(intspan, self).difference_update(self._parse_range(items))
+        return self
+
+    def symmetric_difference_update(self, items):
+        super(intspan, self).symmetric_difference_update(self._parse_range(items))
+        return self
         
     def discard(self, items):
         for item in 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))
+    __le__   = issubset
+    __ge__   = issuperset 
+    __or__   = union
+    __and__  = intersection
+    __sub__  = difference
+    __xor__  = symmetric_difference
+    __ior__  = update
+    __iand__ = intersection_update
+    __isub__ = difference_update
+    __ixor__ = symmetric_difference_update
         
-    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 __lt__(self, items):
+        return super(intspan, self).__lt__(self._parse_range(items))
+
+    def __gt__(self, items):
+        return super(intspan, self).__gt__(self._parse_range(items))
+
     def __iter__(self):
         """
         Iterate in ascending order.
 
 setup(
     name='intspan',
-    version=verno("0.504"),
+    version=verno("0.514"),
     author='Jonathan Eunice',
     author_email='jonathan.eunice@gmail.com',
     description="Sets of integers like 1,3-7,33. Inspired by Perl's Set::IntSpan",

test/test_intspan.py

 def test_equals():
     s = intspan('1,3,5,7,9')
     assert s == set([1,3,5,7,9])
+
+def test_strict_super_or_subset():
+    s = intspan('1,3,5,7,9')
+    t = intspan('1,3,5')
+    u = intspan('0,1,3,5')
+    assert s > t
+    assert not s > u
+    assert t < s
+    assert t < u
+    assert u > t
+    assert not s < u
+    assert not s > u
     
+def test_isdisjoint():
+    s = intspan('1,3,5,7,9')
+    t = intspan('33-44')
+    u = intspan('1,3,99,299')
+    assert s.isdisjoint(t)
+    assert not s.isdisjoint(u)
+    assert t.isdisjoint(u)
+
 def test_copy():
     t = intspan('1,10')
     tt = t.copy()
     assert t == tt
     assert t is not tt
     
+def test_clear():
+    s = intspan('1,2,3,5,8,13,21')
+    s.clear()
+    assert s == intspan()
+    
+def test_len():
+    s = intspan('1,2,3,5,8,13,21')
+    assert len(s) == 7
+    s.pop()
+    assert len(s) == 6
+    s.clear()
+    assert len(s) == 0
+    
 def test_merge():
     assert str(intspan('1-4,5')) ==  '1-5'
         
     assert t ^ s == t.symmetric_difference(s)
     assert t ^ t == t.symmetric_difference(t)
     
-def test_increments():
+def test_augmented_assignments():
     s = intspan('50-60')
     s |= intspan('10-20')
     assert s == intspan('10-20,50-60')
     s ^= intspan('10,99')
     assert s == intspan('11-15,50-55,99')
     
+    t = intspan('50-60')
+    t.update('10-20')
+    assert t == intspan('10-20,50-60')
+    t.intersection_update('0-55')
+    assert t == intspan('10-20,50-55')
+    t.difference_update('16-20')
+    assert t == intspan('10-15,50-55')
+    t.symmetric_difference_update('10,99')
+    assert t == intspan('11-15,50-55,99')
+    
 def test_pop():
     s = intspan('100-110')
     assert s.pop() == 100
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.