[Python-ideas] Adding str.isascii() ?
M.-A. Lemburg
mal at egenix.com
Fri Jan 26 04:16:41 EST 2018
On 26.01.2018 09:53, Chris Angelico wrote:
> On Fri, Jan 26, 2018 at 7:42 PM, INADA Naoki <songofacandy at gmail.com> wrote:
>> Hi.
>>
>> Currently, int(), str.isdigit(), str.isalnum(), etc... accepts
>> non-ASCII strings.
>>
>>>>> s = 123"
>>>>> s
>> '123'
>>>>> s.isdigit()
>> True
>>>>> print(ascii(s))
>> '\uff11\uff12\uff13'
>>>>> int(s)
>> 123
>>
>> But sometimes, we want to accept only ascii string. For example,
>> ipaddress module uses:
>>
>> _DECIMAL_DIGITS = frozenset('0123456789')
>> ...
>> if _DECIMAL_DIGITS.issuperset(str):
>>
>> ref: https://github.com/python/cpython/blob/e76daebc0c8afa3981a4c5a8b54537f756e805de/Lib/ipaddress.py#L491-L494
>>
>> If str has str.isascii() method, it can be simpler:
>>
>> `if s.isascii() and s.isdigit():`
>>
>> I want to add it in Python 3.7 if there are no opposite opinions.
>>
>
> I'm not sure that the decimal-digit check is actually improved by
> this, but nonetheless, I am in favour of this feature. In CPython,
> this method can simply look at the object headers to see if it has the
> 'ascii' flag set; otherwise, it'd be effectively equivalent to:
>
> def isascii(self):
> return ord(max(self)) < 128
>
> Would be handy when working with semi-textual protocols, where ASCII
> text is trivially encoded, but non-ASCII text may require negotiation
> or a protocol header.
+1
Just a note: checking the header in CPython will only give a hint,
since strings created using higher order kinds can still be 100%
ASCII.
--
Marc-Andre Lemburg
eGenix.com
Professional Python Services directly from the Experts (#1, Jan 26 2018)
>>> Python Projects, Coaching and Consulting ... http://www.egenix.com/
>>> Python Database Interfaces ... http://products.egenix.com/
>>> Plone/Zope Database Interfaces ... http://zope.egenix.com/
________________________________________________________________________
::: We implement business ideas - efficiently in both time and costs :::
eGenix.com Software, Skills and Services GmbH Pastor-Loeh-Str.48
D-40764 Langenfeld, Germany. CEO Dipl.-Math. Marc-Andre Lemburg
Registered at Amtsgericht Duesseldorf: HRB 46611
http://www.egenix.com/company/contact/
http://www.malemburg.com/
More information about the Python-ideas
mailing list