SethMMorton / fastnumbers

Super-fast and clean conversions to numbers for Python.
https://pypi.org/project/fastnumbers/
MIT License
105 stars 13 forks source link

Request for an option to not ignore underscores in numeric literals #23

Closed wavefancy closed 5 years ago

wavefancy commented 5 years ago

Describe the bug '10117_1109' was converted to '101171109'

Expected behavior Should not convert to a number.

SethMMorton commented 5 years ago

What python version is this?

wavefancy commented 5 years ago

Both: Python 3.6.5 :: Anaconda, Inc. and Python 3.6.8 :: Anaconda, Inc.

Best regards Wallace

On Tue, Mar 12, 2019 at 2:28 PM Seth Morton notifications@github.com wrote:

What python version is this?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/SethMMorton/fastnumbers/issues/23#issuecomment-472126278, or mute the thread https://github.com/notifications/unsubscribe-auth/ABZncrCiPykyOedYGgUvGRluN4awbCoEks5vV_G7gaJpZM4brjeK .

SethMMorton commented 5 years ago

This is expected behavior. Python >= 3.6 itself accepts this as valid numeric input as well. Behold:

Python 3.6.3 (default, Jan 24 2018, 17:56:11) 
[GCC 4.7.2] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 10117_1109
101171109
>>> int('10117_1109')
101171109

If you ran this on Python 3.5 this would not be treated as a number.

PEP 515 provides the full specification for what is and is not a valid number when underscores are added. You can also look at Python's test suit for what is considered valid or invalid.

wavefancy commented 5 years ago

Can you please add a option to treat this as not in your fastnumber package. Let people change the behavior they want.

Best regards Wallace

On Wed, Mar 13, 2019 at 12:49 PM Seth Morton notifications@github.com wrote:

This is expected behavior. Python >= 3.6 accepts this as valid numeric input as well. Behold:

Python 3.6.3 (default, Jan 24 2018, 17:56:11) [GCC 4.7.2] on linux Type "help", "copyright", "credits" or "license" for more information.>>> 10117_1109101171109>>> int('10117_1109')101171109

If you ran this on Python 3.5 this would not be treated as a number.

PEP 515 https://www.python.org/dev/peps/pep-0515/ provides the full specification for what is and is not a valid number when underscores are added. You can also look at Python's test suit for what is considered valid or invalid https://github.com/python/cpython/blob/v3.6.8/Lib/test/test_grammar.py#L23 .

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/SethMMorton/fastnumbers/issues/23#issuecomment-472508578, or mute the thread https://github.com/notifications/unsubscribe-auth/ABZncrPbB4RWdtPdLKOk-Hr2zuvnO5J1ks5vWSwfgaJpZM4brjeK .

-- Best regards Minxian Wang

Research Fellow Beth Israel Deaconess Medical Center Harvard Medical School 99 Brookline Ave Boston, MA 02215

SethMMorton commented 5 years ago

Can you please be more specific about what you want? For example, I am unwilling to make this change for fastnumbers.int or fastnumbers.float because these are intended to be drop-in replacements for the python built-ins. However, I would be willing to make such a change for the fastnumbers.fast_* or fastnumbers.is* functions.

wavefancy commented 5 years ago

I like the function of fast_real() do not treat this type of number (10117_1109) as int. If you have some other function can do this, I would very happy about that also.

I don't know why people like auto remove the underscore. Make things anti-intuitive.

Best regards Wallace

SethMMorton commented 5 years ago

I think that https://www.python.org/dev/peps/pep-0515/ does a good job of explaining the rationale behind this. I'm sorry that you don't like the behavior, but if another person was expecting underscores to be ignored by fastnumbers but it did not they would find that behavior un-intuitive.

I will add this as an option.

wavefancy commented 5 years ago

Thank you very much for helping with this.

Best regards Wallace

SethMMorton commented 5 years ago

This has been released in fastnumbers version 2.2.0.

wavefancy commented 5 years ago

Thank you very much, works perfectly.

However, the docstring function signature seems no this option fast_real(x, default=None, raise_on_invalid=False, key=None, nan=None, inf=None, coerce=True). But it really works, as I can find the option in Parameters: allow_underscores

Best regards Wallace