[Python-ideas] Re: String method to check for a float

2021-10-07 Thread Jeremiah Paige
If the end goal is to just get either a float or an int from a string, and you want to only accept floats the way Python spells them, what about using ast.literal_eval? >>> type(ast.literal_eval('1')) >>> type(ast.literal_eval('1.0')) >>> type(ast.literal_eval('01_105e-3')) On Sun, Oct 3,

[Python-ideas] Re: String method to check for a float

2021-10-03 Thread Debashish Palit
There are plenty of use cases if you pause to think. The other objections are trivial. Even the simplest usage with the input() function is enough to warrant its inclusion, considering that there are other useless string methods. As I have had no support for the past few days, I quit the

[Python-ideas] Re: String method to check for a float

2021-10-02 Thread Steven D'Aprano
On Sun, Oct 03, 2021 at 01:26:06AM -, Debashish Palit wrote: > The floatify solution is a bit involved - using map inside a list > comprehension. Usually list comprehensions replace the usage of map. > Also floatify requires 5 extra lines of code (putting `return` on the > same line is not

[Python-ideas] Re: String method to check for a float

2021-10-02 Thread Steven D'Aprano
On Sat, Oct 02, 2021 at 01:06:46PM -, Debashish Palit wrote: [...] > Consider a number radix conversion software. It needs to accept the > input number as a string (say '15A' in base 12). When we provide it a > base 10 number, it would also be a string. Why a string? That's an unusual API.

[Python-ideas] Re: String method to check for a float

2021-10-02 Thread Debashish Palit
The floatify solution is a bit involved - using map inside a list comprehension. Usually list comprehensions replace the usage of map. Also floatify requires 5 extra lines of code (putting `return` on the same line is not a best practice). Why not do it with one simple line of code ? Debashish

[Python-ideas] Re: String method to check for a float

2021-10-02 Thread Chris Angelico
On Sun, Oct 3, 2021 at 1:55 AM Debashish Palit wrote: > > Ok it is a 3 way flag, but the code is not awkward. 'c' is just a name I > chose - it does not mean anything. > > The check for a valid number can be used on a list of strings - > > [float(s) for s in lst if s.isfloat() is not None] > >

[Python-ideas] Re: String method to check for a float

2021-10-02 Thread Debashish Palit
Ok it is a 3 way flag, but the code is not awkward. 'c' is just a name I chose - it does not mean anything. The check for a valid number can be used on a list of strings - [float(s) for s in lst if s.isfloat() is not None] No 3 way flag here. Difficult to do with EAFP.

[Python-ideas] Re: String method to check for a float

2021-10-02 Thread Steven D'Aprano
On Sat, Oct 02, 2021 at 01:21:46PM -, Debashish Palit wrote: > Steven D'Aprano wrote: > > On Sat, Oct 02, 2021 at 02:53:03AM -, Debashish Palit wrote: > > > There is no need of a three_way_flag - just use a conditional > > > expression instead of an if-elif-else block, > Using a

[Python-ideas] Re: String method to check for a float

2021-10-02 Thread Debashish Palit
> As I have mentioned above, EAFP is fine but you have to apply it twice. > str.float accomplishes the same with 1 line of code. > num = float(s) if s.float() else int(s) There is a typo in my reply as quoted: it should be .isfloat() and not .float() .

[Python-ideas] Re: String method to check for a float

2021-10-02 Thread Debashish Palit
Steven D'Aprano wrote: > On Sat, Oct 02, 2021 at 02:53:03AM -, Debashish Palit wrote: > > There is no need of a three_way_flag - just use a conditional > > expression instead of an if-elif-else block, > > Of course you need a three way flag if your function returns a three way > flag. It

[Python-ideas] Re: String method to check for a float

2021-10-02 Thread Debashish Palit
Steven D'Aprano wrote: > On Sat, Oct 02, 2021 at 02:17:32AM -, Debashish Palit wrote: > > The method need not take care of every variation. Currently there is > > no method to check for a float in a string even without formatting. > > Right, because no method is needed. If you want to convert

[Python-ideas] Re: String method to check for a float

2021-10-02 Thread Steven D'Aprano
On Sat, Oct 02, 2021 at 02:17:32AM -, Debashish Palit wrote: > The method need not take care of every variation. Currently there is > no method to check for a float in a string even without formatting. Right, because no method is needed. If you want to convert something to a float (whether

[Python-ideas] Re: String method to check for a float

2021-10-02 Thread Steven D'Aprano
On Sat, Oct 02, 2021 at 02:53:03AM -, Debashish Palit wrote: > There is no need of a three_way_flag - just use a conditional > expression instead of an if-elif-else block, Of course you need a three way flag if your function returns a three way flag. It returns False for ints, True for

[Python-ideas] Re: String method to check for a float

2021-10-01 Thread Debashish Palit
A correction to my reply: int('1234567890' * 100) will not raise an overflow error, multiplying 1.0 with it raises the error. str.isfloat would still flag the string as an int and then it is upto the application code to deal with the overflow error.

[Python-ideas] Re: String method to check for a float

2021-10-01 Thread Debashish Palit
Having such a method in the core helps avoid writing the function repeatedly. (This would especially help beginners.) My version is suited to the core language as a string method. parse_value as given by Dennis Sweeney is suited to user code. ___

[Python-ideas] Re: String method to check for a float

2021-10-01 Thread Debashish Palit
There is no need of a three_way_flag - just use a conditional expression instead of an if-elif-else block, str.isfloat uses the int() and float() functions, so, in your example, if float returns inf we can still return True (or maybe return None in this case too). If int() raises overflow

[Python-ideas] Re: String method to check for a float

2021-10-01 Thread Chris Angelico
On Sat, Oct 2, 2021 at 12:19 PM Debashish Palit wrote: > > The method need not take care of every variation. Currently there is no > method to check for a float in a string even without formatting. str.numeric > or str.decimal don't work. str.isfloat would test unformatted strings for > floats

[Python-ideas] Re: String method to check for a float

2021-10-01 Thread Debashish Palit
The method need not take care of every variation. Currently there is no method to check for a float in a string even without formatting. str.numeric or str.decimal don't work. str.isfloat would test unformatted strings for floats or ints just like the functions int() and float() convert strings

[Python-ideas] Re: String method to check for a float

2021-10-01 Thread Steven D'Aprano
On Fri, Oct 01, 2021 at 08:56:39AM -, dpali...@outlook.com wrote: > It would be nice to have a string method that checks for a float. > Currently there is no support for this, either built-in or in the > standard library. There is a thread, dating back to Dec 2020, that > proposes a

[Python-ideas] Re: String method to check for a float

2021-10-01 Thread Dennis Sweeney
I don't know how common it is to want to distinguish between string representations of integers and those of floats. It seems like a function that could have a million little variations: What if my boss says that now my input integers can have commas? Or have extra space added? Or if