Parsing numeric ranges

Suneel Kingrani suneel.kingrani at yahoo.com
Fri Feb 25 15:14:53 EST 2011



very well done..
But I thought it may require a little bit of change in the proposed code.. :)

def _revision_list_with_ranges_to_list_without_ranges(revision_list):
        for revision in revision_list.split(','):
            if '-' in str(revision):
                from_revision, _, to_revision = str(revision).partition('-')
                for revision_in_range in range(int(from_revision),
    int(to_revision)+1):
                    yield revision_in_range
            else:
                yield int(revision)



cheers:
suneel kingrani




________________________________
From: Simon Brunning <simon at brunningonline.net>
To: Seldon <seldon at katamail.it>
Cc: python-list at python.org
Sent: Fri, 25 February, 2011 10:36:10 PM
Subject: Re: Parsing numeric ranges

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.
-- 
http://mail.python.org/mailman/listinfo/python-list

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20110225/74a4d6bd/attachment.html>


More information about the Python-list mailing list