Welcome, guest | Sign In | My Account | Store | Cart

Notice! PyPM is being replaced with the ActiveState Platform, which enhances PyPM’s build and deploy capabilities. Create your free Platform account to download ActivePython or customize Python with the packages you require and get automatic updates.

Download
ActivePython
INSTALL>
pypm install intspan

How to install intspan

  1. Download and install ActivePython
  2. Open Command Prompt
  3. Type pypm install intspan
 Python 2.7Python 3.2Python 3.3
Windows (32-bit)
Windows (64-bit)
Mac OS X (10.5+)
Linux (32-bit)
Linux (64-bit)
0.701 Available View build log
0.701 Available View build log
 
Imports
Lastest release
version 0.701 on May 17th, 2013

Subset of set designed to conveniently hold sets of integers. It creates them from, and displays them as, integer spans (e.g. 1-3,14,29,92-97). When iterating, pop()-ing an item, or converting to a list, intspan behaves as if it were an ordered collection.

The main draw is that this provides a convenient way to specify ranges--for example, ranges of rows to be processed in a spreadsheet.

Usage

from intspan import intspan

s = intspan('1-3,14,29,92-97')
s.discard('2,13,92')
print s
print repr(s)
print list(s)

yields:

1,3,14,29,93-97
intspan('1,3,14,29,93-97')
[1, 3, 14, 29, 93, 94, 95, 96, 97]

While:

for n in intspan('1-3,5'):
    print n                 # Python 2

yields:

1
2
3
5

Most set operations such as intersection, union, and so on are available just as they are in Python's set. In addition, if you wish to extract the contiguous ranges:

for r in intspan('1-3,5,7-9,10,21-22,23,24').ranges():
    print r                 # Python 2

yields:

(1, 3)
(5, 5)
(7, 10)
(21, 24)

Performance

intspan piggybacks Python's set, so it stores every integer individually. Unlike Perl's Set::IntSpan it is not optimized for long contiguous runs. For sets of several hundred or thousand members, you will probably never notice the difference.

On the other hand, if you're doing lots of processing of large sets (e.g. with 100,000 or more elements), or doing lots of set operations on them (e.g. union, intersection), a data structure based on lists of ranges, run length encoding, or Judy arrays would probably perform / scale better.

Alternatives

There are several modules you might want to consider as alternatives or supplements. AFAIK, none of them provide the convenient integer span specification that intspan does, but they have other virtues:

  • cowboy provides generalized ranges and multi-ranges. Bonus points for the package tagline: "It works on ranges."
  • rangeset is a generalized range set module. It also supports infinite ranges.
  • judy a Python wrapper around Judy arrays that are implemented in C. No docs or tests to speak of.

Notes

  • Version 0.7 fixed parsing of spans including negative numbers, and added the ranges() method.
  • Though inspired by Perl's Set::IntSpan, that's where the similarity stops. intspan supports only finite sets, and it follows the methods and conventions of Python's set.
  • 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 and ranges() method based on Jeff Mercado's concise answer to this StackOverflow question. Thank you, Jeff!
  • Automated multi-version testing managed with the wonderful pytest and tox. Successfully packaged for, and tested against, all late-model verions of Python: 2.6, 2.7, 3.2, and 3.3, as well as PyPy 1.9 (based on 2.7.2).
  • The author, Jonathan Eunice or @jeunice on Twitter welcomes your comments and suggestions.

Installation

To install the latest version:

pip install -U intspan

To easy_install under a specific Python version (3.3 in this example):

python3.3 -m easy_install --upgrade intspan

(You may need to prefix these with "sudo " to authorize installation.)

Subscribe to package updates

Last updated May 17th, 2013

Download Stats

Last month:1

What does the lock icon mean?

Builds marked with a lock icon are only available via PyPM to users with a current ActivePython Business Edition subscription.

Need custom builds or support?

ActivePython Enterprise Edition guarantees priority access to technical support, indemnification, expert consulting and quality-assured language builds.

Plan on re-distributing ActivePython?

Get re-distribution rights and eliminate legal risks with ActivePython OEM Edition.