sagemath / sage

Main repository of SageMath
https://www.sagemath.org
Other
1.34k stars 452 forks source link

py3: Adapt Sage's range-like functions #30121

Open slel opened 4 years ago

slel commented 4 years ago

Python 2 had range (returning lists) and xrange (returning iterators).

Python 3 only has range which behaves like the old xrange.

This ticket is to rename or change our range-like functions accordingly.

To go further we could make sure they all follow similar semantics, i.e.:

Functions in the global name space:

See also:

CC: @slel @tscrim @fchapoton

Component: python3

Keywords: range

Branch/Commit: public/srange_as_iterator @ 3cee22e

Issue created by migration from https://trac.sagemath.org/ticket/30121

slel commented 4 years ago
comment:1

Initial list from a look at:

sage: [x for x in dir() if 'range' in x]
slel commented 4 years ago

Description changed:

--- 
+++ 
@@ -3,6 +3,12 @@
 Python 3 only has `range` which behaves like the old `xrange`.

 This ticket is to rename or change our range-like functions accordingly.
+
+To go further we could make sure they all follow similar semantics, i.e.:
+- `f(a)` for the range from `0` included to `a` excluded
+- `f(a, b)` for the range from `a` included to `b` excluded
+- `f(a, b, c)` for the range from `a` included to `b` excluded, by steps of `c`
+

 Functions in the global name space:
 - `ellipsis_range`
slel commented 4 years ago

Description changed:

--- 
+++ 
@@ -26,5 +26,7 @@

 See also:

+- #29760: `prime_range` should allow a step
+- #29728: Meta-ticket: improve compatibility with the Python library
 - #18298: `prime_range` vs `primes`
-- #29728: Meta-ticket: improve compatibility with the Python library
+
slel commented 4 years ago

Description changed:

--- 
+++ 
@@ -28,5 +28,7 @@

 - #29760: `prime_range` should allow a step
 - #29728: Meta-ticket: improve compatibility with the Python library
+- #24543: py3: need to fix sequence of ranges
 - #18298: `prime_range` vs `primes`
+- #17373: Add more examples for when `srange` is better than `range`
mkoeppe commented 3 years ago
comment:6

Setting new milestone based on a cursory review of ticket status, priority, and last modification date.

fchapoton commented 3 years ago

Branch: public/srange_as_iterator

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

efc3d67make srange an iterator
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Commit: efc3d67

fchapoton commented 3 years ago
comment:10

Making srange an iterator almost break every single file. Any volunteer to handle the fixes ?

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from efc3d67 to 3653098

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

3653098one fix in plot
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

02df950one more fix
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from 3653098 to 02df950

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

9eb53f6more fixes for srange
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from 02df950 to 9eb53f6

fchapoton commented 3 years ago
comment:14

We meet the following issue:

sage: A = srange(1,4)                                                            
sage: list(A)                                                                   
[1, 2, 3]
sage: list(A)                                                                   
[]
sage: B = range(1,4)                                                              
sage: list(B)                                                                   
[1, 2, 3]
sage: list(B)                                                                   
[1, 2, 3]
slel commented 3 years ago
comment:15

According to the Python 3 documentation for range:

Rather than being a function, range is actually an immutable sequence type, as documented in Ranges and Sequence Types — list, tuple, range.

Could we make srange an immutable sequence type too?

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

9e33b25make srange an iterator
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from 9eb53f6 to 9e33b25

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

b8740a6using Iterable from collections.abc
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from 9e33b25 to b8740a6

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

6025e4done more fix for srange
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from b8740a6 to 6025e4d

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from 6025e4d to acd32da

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Branch pushed to git repo; I updated commit sha1. New commits:

acd32daone more fix
mkoeppe commented 3 years ago
comment:20

I agree with comment:14 - this is problematic. srange should really construct an object similar to range objects in python3

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from acd32da to 4910cc6

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

4910cc6make srange an iterator
7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Changed commit from 4910cc6 to 3cee22e

7ed8c4ca-6d56-4ae9-953a-41e42b4ed313 commented 3 years ago

Branch pushed to git repo; I updated commit sha1. This was a forced push. New commits:

3cee22emake srange an iterator
mkoeppe commented 2 years ago
comment:26

Can we just get rid of all *mrange* and replace its uses by itertools.product? (I've opened #34337 for this.)

mkoeppe commented 2 years ago

Description changed:

--- 
+++ 
@@ -31,4 +31,4 @@
 - #24543: py3: need to fix sequence of ranges
 - #18298: `prime_range` vs `primes`
 - #17373: Add more examples for when `srange` is better than `range`
-
+- #34337: Deprecate `sage.misc.mrange.*mrange*` and `cartesian_product_iterator`
tscrim commented 2 years ago
comment:28

Replying to @mkoeppe:

Can we just get rid of all *mrange* and replace its uses by itertools.product? (I've opened #34337 for this.)

I posted something on #34337 about somet hings to be careful about with doing the replacement.

kedlaya commented 7 months ago

I would suggest to add #31544 to the list of related issues.