Parsing numeric ranges

Simon Brunning simon at brunningonline.net
Fri Feb 25 12:36:10 EST 2011


On 25 February 2011 09:27, Seldon <seldon at katamail.it> wrote:
> Hi all,
> I have to convert integer ranges expressed in a popular "compact" notation
> (e.g. 2, 5-7, 20-22, 41) to a the actual set of numbers (i.e.
> 2,5,7,20,21,22,41).
>
> Is there any library for doing such kind of things or I have to write it
> from scratch ?

I dredged this out:



def _revision_list_with_ranges_to_list_without_ranges(revision_list):
    '''Convert a revision list with ranges (e.g. '1,3,5-7') to a
simple list without ranges (1,3,5,6,7)'''
    for revision in revision_list:
        if '-' in revision:
            from_revision, _, to_revision = revision.partition('-')
            for revision_in_range in range(int(from_revision),
int(to_revision)+1):
                yield revision_in_range
        else:
            yield int(revision)

-- 
Cheers,
Simon B.



More information about the Python-list mailing list