ASKSAGE: Sage Q&A Forum - Individual question feedhttps://ask.sagemath.org/questions/Q&A Forum for SageenCopyright Sage, 2010. Some rights reserved under creative commons license.Fri, 01 Nov 2019 19:45:18 -0500Sage is not preparsing Python 3 underscored integershttps://ask.sagemath.org/question/47908/sage-is-not-preparsing-python-3-underscored-integers/Hello, Sage community!
With the upcoming migration of Sage from Python 2 to Python 3 (really soon, let us hope!), there are some testings I have been trying, and I have noticed that the "underscored integer literals", like "1_000_000" are not preparsed by Sage. For example,
preparse('1_000_000 + 3')
results in
'1_000_000 + Integer(3)'
However,
preparse('1000000 + 3')
returns
'Integer(1000000) + Integer(3)'
On the other hand, if I write
type(1_000_000 + 3)
the result is
<class 'sage.rings.integer.Integer'>
which is great, but
type(1_000_000 + 3_000)
instead gives me
<class 'int'>
Finally,
type(1000000 + 3000)
gives us
<class 'sage.rings.integer.Integer'>
I know one of the reasons for the `Integer` type was to allow divisions like `3/2` to return the float `1.5` even with Python 2, which would return (very disturbingly) `1`, in other case. However, Python 3 integers already do this. Besides what I mentioned above and this, I haven't found any other difference in behavior.
So I was wondering: Is there any disadvantage in Sage not being preparsing this type of literals?Fri, 13 Sep 2019 15:41:47 -0500https://ask.sagemath.org/question/47908/sage-is-not-preparsing-python-3-underscored-integers/Comment by dsejas for <p>Hello, Sage community!</p>
<p>With the upcoming migration of Sage from Python 2 to Python 3 (really soon, let us hope!), there are some testings I have been trying, and I have noticed that the "underscored integer literals", like "1_000_000" are not preparsed by Sage. For example,</p>
<pre><code>preparse('1_000_000 + 3')
</code></pre>
<p>results in</p>
<pre><code>'1_000_000 + Integer(3)'
</code></pre>
<p>However,</p>
<pre><code>preparse('1000000 + 3')
</code></pre>
<p>returns</p>
<pre><code>'Integer(1000000) + Integer(3)'
</code></pre>
<p>On the other hand, if I write</p>
<pre><code>type(1_000_000 + 3)
</code></pre>
<p>the result is</p>
<pre><code><class 'sage.rings.integer.Integer'>
</code></pre>
<p>which is great, but</p>
<pre><code>type(1_000_000 + 3_000)
</code></pre>
<p>instead gives me</p>
<pre><code><class 'int'>
</code></pre>
<p>Finally,</p>
<pre><code>type(1000000 + 3000)
</code></pre>
<p>gives us</p>
<pre><code><class 'sage.rings.integer.Integer'>
</code></pre>
<p>I know one of the reasons for the <code>Integer</code> type was to allow divisions like <code>3/2</code> to return the float <code>1.5</code> even with Python 2, which would return (very disturbingly) <code>1</code>, in other case. However, Python 3 integers already do this. Besides what I mentioned above and this, I haven't found any other difference in behavior.</p>
<p>So I was wondering: Is there any disadvantage in Sage not being preparsing this type of literals?</p>
https://ask.sagemath.org/question/47908/sage-is-not-preparsing-python-3-underscored-integers/?comment=48108#post-id-48108It is quite useful for the clarity of the programs. I think "51_428_826" is more readable than "51428826".Mon, 30 Sep 2019 16:35:19 -0500https://ask.sagemath.org/question/47908/sage-is-not-preparsing-python-3-underscored-integers/?comment=48108#post-id-48108Comment by Iguananaut for <p>Hello, Sage community!</p>
<p>With the upcoming migration of Sage from Python 2 to Python 3 (really soon, let us hope!), there are some testings I have been trying, and I have noticed that the "underscored integer literals", like "1_000_000" are not preparsed by Sage. For example,</p>
<pre><code>preparse('1_000_000 + 3')
</code></pre>
<p>results in</p>
<pre><code>'1_000_000 + Integer(3)'
</code></pre>
<p>However,</p>
<pre><code>preparse('1000000 + 3')
</code></pre>
<p>returns</p>
<pre><code>'Integer(1000000) + Integer(3)'
</code></pre>
<p>On the other hand, if I write</p>
<pre><code>type(1_000_000 + 3)
</code></pre>
<p>the result is</p>
<pre><code><class 'sage.rings.integer.Integer'>
</code></pre>
<p>which is great, but</p>
<pre><code>type(1_000_000 + 3_000)
</code></pre>
<p>instead gives me</p>
<pre><code><class 'int'>
</code></pre>
<p>Finally,</p>
<pre><code>type(1000000 + 3000)
</code></pre>
<p>gives us</p>
<pre><code><class 'sage.rings.integer.Integer'>
</code></pre>
<p>I know one of the reasons for the <code>Integer</code> type was to allow divisions like <code>3/2</code> to return the float <code>1.5</code> even with Python 2, which would return (very disturbingly) <code>1</code>, in other case. However, Python 3 integers already do this. Besides what I mentioned above and this, I haven't found any other difference in behavior.</p>
<p>So I was wondering: Is there any disadvantage in Sage not being preparsing this type of literals?</p>
https://ask.sagemath.org/question/47908/sage-is-not-preparsing-python-3-underscored-integers/?comment=47956#post-id-47956Huh. I didn't even know this a feature of Python 3.Wed, 18 Sep 2019 04:05:24 -0500https://ask.sagemath.org/question/47908/sage-is-not-preparsing-python-3-underscored-integers/?comment=47956#post-id-47956Answer by tmonteil for <p>Hello, Sage community!</p>
<p>With the upcoming migration of Sage from Python 2 to Python 3 (really soon, let us hope!), there are some testings I have been trying, and I have noticed that the "underscored integer literals", like "1_000_000" are not preparsed by Sage. For example,</p>
<pre><code>preparse('1_000_000 + 3')
</code></pre>
<p>results in</p>
<pre><code>'1_000_000 + Integer(3)'
</code></pre>
<p>However,</p>
<pre><code>preparse('1000000 + 3')
</code></pre>
<p>returns</p>
<pre><code>'Integer(1000000) + Integer(3)'
</code></pre>
<p>On the other hand, if I write</p>
<pre><code>type(1_000_000 + 3)
</code></pre>
<p>the result is</p>
<pre><code><class 'sage.rings.integer.Integer'>
</code></pre>
<p>which is great, but</p>
<pre><code>type(1_000_000 + 3_000)
</code></pre>
<p>instead gives me</p>
<pre><code><class 'int'>
</code></pre>
<p>Finally,</p>
<pre><code>type(1000000 + 3000)
</code></pre>
<p>gives us</p>
<pre><code><class 'sage.rings.integer.Integer'>
</code></pre>
<p>I know one of the reasons for the <code>Integer</code> type was to allow divisions like <code>3/2</code> to return the float <code>1.5</code> even with Python 2, which would return (very disturbingly) <code>1</code>, in other case. However, Python 3 integers already do this. Besides what I mentioned above and this, I haven't found any other difference in behavior.</p>
<p>So I was wondering: Is there any disadvantage in Sage not being preparsing this type of literals?</p>
https://ask.sagemath.org/question/47908/sage-is-not-preparsing-python-3-underscored-integers/?answer=47911#post-id-47911Good point. Indeed, the preparser is not able to handle such literals yet. This is now [trac ticket 28490](https://trac.sagemath.org/ticket/28490).Fri, 13 Sep 2019 19:41:37 -0500https://ask.sagemath.org/question/47908/sage-is-not-preparsing-python-3-underscored-integers/?answer=47911#post-id-47911Comment by John Palmieri for <p>Good point. Indeed, the preparser is not able to handle such literals yet. This is now <a href="https://trac.sagemath.org/ticket/28490">trac ticket 28490</a>.</p>
https://ask.sagemath.org/question/47908/sage-is-not-preparsing-python-3-underscored-integers/?comment=48454#post-id-48454There is now a proposed fix at the ticket.Mon, 21 Oct 2019 19:39:54 -0500https://ask.sagemath.org/question/47908/sage-is-not-preparsing-python-3-underscored-integers/?comment=48454#post-id-48454Comment by dsejas for <p>Good point. Indeed, the preparser is not able to handle such literals yet. This is now <a href="https://trac.sagemath.org/ticket/28490">trac ticket 28490</a>.</p>
https://ask.sagemath.org/question/47908/sage-is-not-preparsing-python-3-underscored-integers/?comment=48472#post-id-48472Will it be available at Sage-9.0? Thank you very much!Tue, 22 Oct 2019 14:07:05 -0500https://ask.sagemath.org/question/47908/sage-is-not-preparsing-python-3-underscored-integers/?comment=48472#post-id-48472Comment by dsejas for <p>Good point. Indeed, the preparser is not able to handle such literals yet. This is now <a href="https://trac.sagemath.org/ticket/28490">trac ticket 28490</a>.</p>
https://ask.sagemath.org/question/47908/sage-is-not-preparsing-python-3-underscored-integers/?comment=48559#post-id-48559Hello, @tmontiel and @john-palmieri. I don't know which one of you should I refer this observation to, so I am mentioning both of you. I just compiled sage-9.0-beta3 with Python 3. The proposed fix works beautifully, except for the fact that numbers like `1_000.3` are preparsed like `1_000.gen(3)`, which produces an error.
I don't know this`gen()` method, so I don't know how to fix this. Have you considered extending the fix for real numbers? Thank you for your efforts!Wed, 30 Oct 2019 11:31:45 -0500https://ask.sagemath.org/question/47908/sage-is-not-preparsing-python-3-underscored-integers/?comment=48559#post-id-48559Comment by John Palmieri for <p>Good point. Indeed, the preparser is not able to handle such literals yet. This is now <a href="https://trac.sagemath.org/ticket/28490">trac ticket 28490</a>.</p>
https://ask.sagemath.org/question/47908/sage-is-not-preparsing-python-3-underscored-integers/?comment=48560#post-id-48560I don't see this behavior: real numbers are preparsed for me, with 9.0.beta3, both Python 2 and 3. What platform is this?Wed, 30 Oct 2019 18:17:48 -0500https://ask.sagemath.org/question/47908/sage-is-not-preparsing-python-3-underscored-integers/?comment=48560#post-id-48560Comment by John Palmieri for <p>Good point. Indeed, the preparser is not able to handle such literals yet. This is now <a href="https://trac.sagemath.org/ticket/28490">trac ticket 28490</a>.</p>
https://ask.sagemath.org/question/47908/sage-is-not-preparsing-python-3-underscored-integers/?comment=48561#post-id-48561@dsejas: you have to run `./sage -b` to rebuild the Sage library, incorporating the changes. Then try `1_000.3` again.Wed, 30 Oct 2019 18:22:59 -0500https://ask.sagemath.org/question/47908/sage-is-not-preparsing-python-3-underscored-integers/?comment=48561#post-id-48561Comment by dsejas for <p>Good point. Indeed, the preparser is not able to handle such literals yet. This is now <a href="https://trac.sagemath.org/ticket/28490">trac ticket 28490</a>.</p>
https://ask.sagemath.org/question/47908/sage-is-not-preparsing-python-3-underscored-integers/?comment=48562#post-id-48562Hello, @john-palmieri. I am working with Ubuntu 18.04.3 LTS. I compiled SageMath by running:
git clone git://git.sagemath.org/sage.git/ SageMath
cd SageMath
git checkout develop
make configure
./configure --with-python=3
make
Here is a [screenshot](https://drive.google.com/open?id=1Esm8AqXGiYPovZVe4V-giBCiORDwqFu-).
By the way, I did run `./sage -b` just in case, but with no effect.Wed, 30 Oct 2019 19:30:06 -0500https://ask.sagemath.org/question/47908/sage-is-not-preparsing-python-3-underscored-integers/?comment=48562#post-id-48562Comment by dsejas for <p>Good point. Indeed, the preparser is not able to handle such literals yet. This is now <a href="https://trac.sagemath.org/ticket/28490">trac ticket 28490</a>.</p>
https://ask.sagemath.org/question/47908/sage-is-not-preparsing-python-3-underscored-integers/?comment=48590#post-id-48590Hello, @john-palmieri. This is embarrassing, but I wasn't able to use `git` in order to apply the changes. After hours reading about `git`, I gave up for now, and applied the changes manually, recompiled `sage`, and now the patch is working. Thank you very much. As of the moment of writing this, the new code has passed all my tests. However, should I find an error, I will report it, of course. Thanks again!
P.S.: Tomorrow I am starting to read the "[Pro git](https://git-scm.com/book/en/v2)" book, along with the tutorial you pointed me to. I must learn `git` if I want to contribute to the SageMath Project.Fri, 01 Nov 2019 19:45:18 -0500https://ask.sagemath.org/question/47908/sage-is-not-preparsing-python-3-underscored-integers/?comment=48590#post-id-48590Comment by John Palmieri for <p>Good point. Indeed, the preparser is not able to handle such literals yet. This is now <a href="https://trac.sagemath.org/ticket/28490">trac ticket 28490</a>.</p>
https://ask.sagemath.org/question/47908/sage-is-not-preparsing-python-3-underscored-integers/?comment=48571#post-id-48571You have checked out the `develop` branch but not applied the changes from trac ticket 28490, so you are just seeing the old Sage behavior.Thu, 31 Oct 2019 10:11:27 -0500https://ask.sagemath.org/question/47908/sage-is-not-preparsing-python-3-underscored-integers/?comment=48571#post-id-48571Comment by dsejas for <p>Good point. Indeed, the preparser is not able to handle such literals yet. This is now <a href="https://trac.sagemath.org/ticket/28490">trac ticket 28490</a>.</p>
https://ask.sagemath.org/question/47908/sage-is-not-preparsing-python-3-underscored-integers/?comment=48572#post-id-48572Oh, man! I really need to learn `git`. What can I do now? Should I rebuild Sage again? Could you give me a tiny guide here? I appreciate your taking the time to answer.Thu, 31 Oct 2019 10:23:02 -0500https://ask.sagemath.org/question/47908/sage-is-not-preparsing-python-3-underscored-integers/?comment=48572#post-id-48572Comment by John Palmieri for <p>Good point. Indeed, the preparser is not able to handle such literals yet. This is now <a href="https://trac.sagemath.org/ticket/28490">trac ticket 28490</a>.</p>
https://ask.sagemath.org/question/47908/sage-is-not-preparsing-python-3-underscored-integers/?comment=48573#post-id-48573Yes, you need to know a little `git`. Start with http://doc.sagemath.org/html/en/developer/manual_git.html. You want to be able to run (from the section Checking Out Tickets) `git fetch trac u/jhpalmieri/preparse_underscore`, after which run `git rebase develop`, and then finally `./sage -b`.Thu, 31 Oct 2019 12:52:39 -0500https://ask.sagemath.org/question/47908/sage-is-not-preparsing-python-3-underscored-integers/?comment=48573#post-id-48573Comment by dsejas for <p>Good point. Indeed, the preparser is not able to handle such literals yet. This is now <a href="https://trac.sagemath.org/ticket/28490">trac ticket 28490</a>.</p>
https://ask.sagemath.org/question/47908/sage-is-not-preparsing-python-3-underscored-integers/?comment=47917#post-id-47917Thank you, @tmontiel. I suppose I should have reported this directly as a bug. I will open an account in trac.sagemath.org for future reports. Thank you again!Sat, 14 Sep 2019 19:50:11 -0500https://ask.sagemath.org/question/47908/sage-is-not-preparsing-python-3-underscored-integers/?comment=47917#post-id-47917Comment by tmonteil for <p>Good point. Indeed, the preparser is not able to handle such literals yet. This is now <a href="https://trac.sagemath.org/ticket/28490">trac ticket 28490</a>.</p>
https://ask.sagemath.org/question/47908/sage-is-not-preparsing-python-3-underscored-integers/?comment=47919#post-id-47919This is not necessary, ask.sagemath.org is a good place to report bugs as well.Sun, 15 Sep 2019 06:09:37 -0500https://ask.sagemath.org/question/47908/sage-is-not-preparsing-python-3-underscored-integers/?comment=47919#post-id-47919