
Dear all, what do you think about allowing the use of operators on ranges? I thought of things like: a = range(1,10) b = range(5,12) intersect = a & b # equivalent to intersect = range(5,10) merge = a | b # equivalent to merge = range(1,12) to make this work in more complex cases, like: a = range(1,10) b = range(12,15) merge = a | b # equivalent to sth. like range(range(1,10),range(12,15)) maybe range nesting would be desirable, but the full sequence of values of such a nested range could still be calculated on demand. Such syntax would allow for the generation of more complex sequences, and, as a side-effect, ranges could also be checked for overlap easily: if a & b: do_something() this whole idea is reminiscent, of course, of what's implemented for sets already, so like there you could think of intersect = a & b as shorthand for intersect = a.intersection(b) Opinions?

On Thu, Feb 21, 2013 at 3:35 PM, Wolfgang Maier < wolfgang.maier@biologie.uni-freiburg.de> wrote:
This has been suggested in the past. http://mail.python.org/pipermail/python-bugs-list/2012-June/173343.html I think each operation you implement can have a lot of different bikeshed details. Since these are all very simple to begin with, it's better to not burden the language and just let users implement whatever it is they need. Though, maybe you can show some popular and compelling use cases in the wild? Yuval Greenfield

On Thu, Feb 21, 2013 at 5:50 AM, Yuval Greenfield <ubershmekel@gmail.com>wrote:
Ignoring performance issues, imagine instead a sorted set class that you could add ranges to (or remove them). Well, actually it's trivial to convert a range to a set; the interesting problem is performance. You could save the range parts unexpanded so you could add huge ranges and you could also support infinite ranges (itertools.count). I suggest this because once you consider doing the first set of operations, like r1 & r2, it's quite natural for someone to want some_non_unlucky_integers = range(100) - [13] The idea of a sorted set class is, of course, not original. See for example http://stutzbachenterprises.com/blist/sortedset.html. If SortedSet isn't in the stdlib, I can't imagine this enhancement being either. --- Bruce Latest blog post: Alice's Puzzle Page http://www.vroospeak.com

On Feb 21, 2013, at 5:35, Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> wrote:
That means ranges are no longer constant size, no longer have constant- time contains-testing, etc.; it's linear in the number of sub ranges. (And so would intersect and union, for that matter.) That's more like a sparse set or an RLE-encoded set or something--which is a useful thing for numerical work (does pandas have an equivalent?), but it's not really a range anymore. Maybe build a new IntervalSet or something, which can interact with ranges (and itertools.count?) transparently (by treating them as a set of 1), put it on PyPI, and see how many users find themselves wishing that range(1, 10) | range(12, 15) would return an IntervalSet so they wouldn't have to do IntervalSet(range(1, 10)) | range(12, 15). That seems like the best way to find out whether this would be useful in practice. Also, I think I'd expect difference and symmetric difference if I wanted union, and if you're handling infinite ranges I'd expect complement as well. Plus interaction with single ints as well as ranges and other sets. But that may be excessive feature creep--since I don't have an obvious use for union, I may be imagining things wrong.
This side effect seems like it would be useful more often than the main effect. I've written a ranges_intersect function in at least two projects, but I've never written an intersect_ranges. (That is, I only need a boolean value.)

On Sat, Feb 23, 2013 at 12:05 AM, Antoine Pitrou <solipsis@pitrou.net> wrote:
-1 from me as well. We don't even implement concatenation and repetition for ranges in order to keep them simple - we have to caveat the range docs to point out they don't *quite* implement the full sequence API due to this restriction. However, I will note that starting in Python 3.3, range objects expose their "start", "stop" and "step" attributes. That makes it much easier for third party software to manipulate them arithmetically. Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia

On Fri, 22 Feb 2013 17:40:29 +0000 (UTC) Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> wrote:
I suppose it wouldn't very difficult to make range subclassable. You can try writing a patch if you want: http://docs.python.org/devguide/ Regards Antoine.

On Fri, Feb 22, 2013 at 1:43 PM, Antoine Pitrou <solipsis@pitrou.net> wrote:
I would be very happy to see a similar data structure available (on pypi). I was writing an HTTP-backed file object that used partial GET requests to just fetch the parts of the file that were actually read; it would have benefited from an efficient set-of-ranges (start+stop but not step) implementation.

On Sat, Feb 23, 2013 at 7:21 PM, Terry Reedy <tjreedy@udel.edu> wrote:
I wasn't even aware you *couldn't* subclass it (I'd never tried). As far as I am aware, that's just a quirk inherited from the old xrange implementation rather than a deliberate design decision. On the other hand, I suspect for many cases involving more advanced range variants, containment would be a better option than inheritance, particular if you want to implement a type that can't be reliably described through a (start, stop, step) triple. Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia

On 2/26/2013 11:46 PM, Raymond Hettinger wrote:
The four builtins that cannot be subclassed are bool, memoryview, range, slice. This is documented for bool; I propose to do so for the others in http://bugs.python.org/issue17279. Would it be correct to say (now) that all 4 are intentional omissions? and not merely oversights? -- Terry Jan Reedy

On Thu, Feb 21, 2013 at 3:35 PM, Wolfgang Maier < wolfgang.maier@biologie.uni-freiburg.de> wrote:
This has been suggested in the past. http://mail.python.org/pipermail/python-bugs-list/2012-June/173343.html I think each operation you implement can have a lot of different bikeshed details. Since these are all very simple to begin with, it's better to not burden the language and just let users implement whatever it is they need. Though, maybe you can show some popular and compelling use cases in the wild? Yuval Greenfield

On Thu, Feb 21, 2013 at 5:50 AM, Yuval Greenfield <ubershmekel@gmail.com>wrote:
Ignoring performance issues, imagine instead a sorted set class that you could add ranges to (or remove them). Well, actually it's trivial to convert a range to a set; the interesting problem is performance. You could save the range parts unexpanded so you could add huge ranges and you could also support infinite ranges (itertools.count). I suggest this because once you consider doing the first set of operations, like r1 & r2, it's quite natural for someone to want some_non_unlucky_integers = range(100) - [13] The idea of a sorted set class is, of course, not original. See for example http://stutzbachenterprises.com/blist/sortedset.html. If SortedSet isn't in the stdlib, I can't imagine this enhancement being either. --- Bruce Latest blog post: Alice's Puzzle Page http://www.vroospeak.com

On Feb 21, 2013, at 5:35, Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> wrote:
That means ranges are no longer constant size, no longer have constant- time contains-testing, etc.; it's linear in the number of sub ranges. (And so would intersect and union, for that matter.) That's more like a sparse set or an RLE-encoded set or something--which is a useful thing for numerical work (does pandas have an equivalent?), but it's not really a range anymore. Maybe build a new IntervalSet or something, which can interact with ranges (and itertools.count?) transparently (by treating them as a set of 1), put it on PyPI, and see how many users find themselves wishing that range(1, 10) | range(12, 15) would return an IntervalSet so they wouldn't have to do IntervalSet(range(1, 10)) | range(12, 15). That seems like the best way to find out whether this would be useful in practice. Also, I think I'd expect difference and symmetric difference if I wanted union, and if you're handling infinite ranges I'd expect complement as well. Plus interaction with single ints as well as ranges and other sets. But that may be excessive feature creep--since I don't have an obvious use for union, I may be imagining things wrong.
This side effect seems like it would be useful more often than the main effect. I've written a ranges_intersect function in at least two projects, but I've never written an intersect_ranges. (That is, I only need a boolean value.)

On Sat, Feb 23, 2013 at 12:05 AM, Antoine Pitrou <solipsis@pitrou.net> wrote:
-1 from me as well. We don't even implement concatenation and repetition for ranges in order to keep them simple - we have to caveat the range docs to point out they don't *quite* implement the full sequence API due to this restriction. However, I will note that starting in Python 3.3, range objects expose their "start", "stop" and "step" attributes. That makes it much easier for third party software to manipulate them arithmetically. Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia

On Fri, 22 Feb 2013 17:40:29 +0000 (UTC) Wolfgang Maier <wolfgang.maier@biologie.uni-freiburg.de> wrote:
I suppose it wouldn't very difficult to make range subclassable. You can try writing a patch if you want: http://docs.python.org/devguide/ Regards Antoine.

On Fri, Feb 22, 2013 at 1:43 PM, Antoine Pitrou <solipsis@pitrou.net> wrote:
I would be very happy to see a similar data structure available (on pypi). I was writing an HTTP-backed file object that used partial GET requests to just fetch the parts of the file that were actually read; it would have benefited from an efficient set-of-ranges (start+stop but not step) implementation.

On Sat, Feb 23, 2013 at 7:21 PM, Terry Reedy <tjreedy@udel.edu> wrote:
I wasn't even aware you *couldn't* subclass it (I'd never tried). As far as I am aware, that's just a quirk inherited from the old xrange implementation rather than a deliberate design decision. On the other hand, I suspect for many cases involving more advanced range variants, containment would be a better option than inheritance, particular if you want to implement a type that can't be reliably described through a (start, stop, step) triple. Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia

On 2/26/2013 11:46 PM, Raymond Hettinger wrote:
The four builtins that cannot be subclassed are bool, memoryview, range, slice. This is documented for bool; I propose to do so for the others in http://bugs.python.org/issue17279. Would it be correct to say (now) that all 4 are intentional omissions? and not merely oversights? -- Terry Jan Reedy

On Thu, Feb 28, 2013 at 6:43 AM, Terry Reedy <tjreedy@udel.edu> wrote:
Yes, I think so. People will have to be *real* convincing to explain a case where composition isn't a more appropriate solution Cheers, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia
participants (9)
-
Andrew Barnert
-
Antoine Pitrou
-
Bruce Leban
-
Daniel Holth
-
Nick Coghlan
-
Raymond Hettinger
-
Terry Reedy
-
Wolfgang Maier
-
Yuval Greenfield