Commits

Jonathan Eunice committed e50624e

added from_ranges constructor

Comments (0)

Files changed (4)

     (5, 5)
     (7, 10)
     (21, 24)
+    
+There is a corresponding constructor::
+
+    print intspan.from_ranges([ (4,6), (10,12) ])
+    
+Gives::
+
+    4-6,10-12
 
 Performance
 ===========
 =====
 
  *  Version 0.7 fixed parsing of spans including negative numbers, and
-    added the ``ranges()`` method.
+    added the ``ranges()`` method. As of 0.71, the ``from_ranges()``
+    constructor appeared.
     
  *  Though inspired by Perl's `Set::IntSpan <http://search.cpan.org/~swmcd/Set-IntSpan-1.16/IntSpan.pm>`_,
     that's where the similarity stops.
 
 import sys, copy
-from itertools import groupby, count
+from itertools import groupby, count, chain
 import re
 
 _PY3 = sys.version_info[0] > 2
     
         # this method required only for PyPy, which otherwise gets the wrong
         # answer (unordered)
+        
+    @classmethod
+    def from_ranges(cls, ranges):
+        return cls( chain( *(range(r[0], r[1]+1) for r in ranges) ) )
     
     @staticmethod
     def _parse_range(datum):
 
 setup(
     name='intspan',
-    version='0.701',
+    version='0.71',
     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

     assert intspan('2').ranges()   == [ (2,2) ]
     assert intspan('1-3').ranges() == [ (1,3) ]
     assert intspan('1-3,5-6').ranges() == [ (1,3), (5,6) ]
+    
+def test_from_ranges():
+    assert intspan.from_ranges([ (1,3), (5,6) ]) == intspan('1-3,5-6')
+    assert intspan.from_ranges([ (1,3) ]) == intspan('1-3')
+    assert intspan.from_ranges([ (2,2) ]) == intspan('2')
+    assert intspan.from_ranges([]) == intspan()
+
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.