Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #50937

Re: Share Code Tips

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <rosuav@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.052
X-Spam-Evidence '*H*': 0.90; '*S*': 0.00; 'algorithm': 0.04; 'string.': 0.05; '"""': 0.07; 'collier': 0.09; 'converts': 0.09; 'sentence': 0.09; 'tackle': 0.09; 'comparison"': 0.16; 'earlier.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'letters.': 0.16; 'lowercase': 0.16; 'perfect.': 0.16; 'those,': 0.16; 'varies': 0.16; 'sat,': 0.16; 'wrote:': 0.18; '>>>': 0.22; 'subject:Code': 0.24; 'unicode': 0.24; 'options': 0.25; 'compare': 0.26; 'equivalent': 0.26; 'first,': 0.26; 'second': 0.26; 'header :In-Reply-To:1': 0.27; 'correct': 0.29; 'chris': 0.29; 'am,': 0.29; 'characters': 0.30; 'message-id:@mail.gmail.com': 0.30; 'code': 0.31; 'context.': 0.31; "d'aprano": 0.31; 'option.': 0.31; 'steven': 0.31; 'figure': 0.32; 'skip:c 30': 0.32; 'url:python': 0.33; 'are:': 0.33; '"the': 0.34; "can't": 0.35; 'something': 0.35; 'german': 0.35; 'johnson': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'there': 0.35; 'version': 0.36; 'really': 0.36; 'described': 0.36; 'similar': 0.36; 'url:org': 0.36; 'example,': 0.37; 'two': 0.37; 'url:library': 0.38; 'to:addr :python-list': 0.38; 'pm,': 0.38; 'anything': 0.39; 'to:addr:python.org': 0.39; 'either': 0.39; 'according': 0.40; 'even': 0.60; 'remove': 0.60; 'dave': 0.60; 'worry': 0.60; 'most': 0.60; 'hope': 0.61; 'new': 0.61; 'url:3': 0.61; 'range': 0.61; 'simply': 0.61; 'simple': 0.61; 'making': 0.63; 'more': 0.64; '20,': 0.68; 'combining': 0.68; 'hints': 0.68; 'containing': 0.69; 'jul': 0.74; 'future,': 0.83; '3.13': 0.84; 'characters,': 0.84; 'angel': 0.91; 'treatment': 0.95; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; bh=B2M0Sd3Go+jyyqcTd2XLMew0xwF/atQMblCtpxCtq5A=; b=dZ28uHR8gFNW2dV3rq4dLfcA/j4wWoMcJTEvfe7Huu0IjWuuwAUjTaVlYWEi/sYCZh PeGoN0MWLrjAXszqIF0Z6PIvTlli+toTfcsumvT4kVj0P3WRWJSOuB+Liwst0ymm3vCD F04q11iXL0/e4wBeTI7nklqNzOLFhsyAIV2/hS0eCJxtvtPsrw7d2A8ZcUUL4r3ehkKi 2XW//HsFN6+hny0M/mW5/NkeGlX5fs18d1oWT/dVBawh/1cxnsW/IwTXPQySGdktI7lj Jcrsm060KWIenNN3ZUvW3oFKky+r8tsJKDLZ84UKXalRf9QPgY7pbHMKNipxyjjdAnD7 tPxA==
MIME-Version 1.0
X-Received by 10.58.187.4 with SMTP id fo4mr6561814vec.55.1374282798047; Fri, 19 Jul 2013 18:13:18 -0700 (PDT)
In-Reply-To <51E9E237.2040903@Gmail.com>
References <mailman.4868.1374241904.3114.python-list@python.org> <51e97e6e$0$29971$c3e8da3$5496439d@news.astraweb.com> <51E9B8EB.5060007@Gmail.com> <kscgv5$gv9$1@ger.gmane.org> <51E9E237.2040903@Gmail.com>
Date Sat, 20 Jul 2013 11:13:17 +1000
Subject Re: Share Code Tips
From Chris Angelico <rosuav@gmail.com>
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
Content-Transfer-Encoding quoted-printable
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.4896.1374282806.3114.python-list@python.org> (permalink)
Lines 69
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1374282806 news.xs4all.nl 15944 [2001:888:2000:d::a6]:43411
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:50937

Show key headers only | View raw


On Sat, Jul 20, 2013 at 11:04 AM, Devyn Collier Johnson
<devyncjohnson@gmail.com> wrote:
>
> On 07/19/2013 07:09 PM, Dave Angel wrote:
>>
>> On 07/19/2013 06:08 PM, Devyn Collier Johnson wrote:
>>>
>>>
>>> On 07/19/2013 01:59 PM, Steven D'Aprano wrote:
>>
>>
>>      <snip>
>>>
>>>
>>> As for the case-insensitive if-statements, most code uses Latin letters.
>>> Making a case-insensitive-international if-statement would be
>>> interesting. I can tackle that later. For now, I only wanted to take
>>> care of Latin letters. I hope to figure something out for all characters.
>>>
>>
>> Once Steven gave you the answer, what's to figure out?  You simply use
>> casefold() instead of lower().  The only constraint is it's 3.3 and later,
>> so you can't use it for anything earlier.
>>
>> http://docs.python.org/3.3/library/stdtypes.html#str.casefold
>>
>> """
>> str.casefold()
>> Return a casefolded copy of the string. Casefolded strings may be used for
>> caseless matching.
>>
>> Casefolding is similar to lowercasing but more aggressive because it is
>> intended to remove all case distinctions in a string. For example, the
>> German lowercase letter 'ß' is equivalent to "ss". Since it is already
>> lowercase, lower() would do nothing to 'ß'; casefold() converts it to "ss".
>>
>> The casefolding algorithm is described in section 3.13 of the Unicode
>> Standard.
>>
>> New in version 3.3.
>> """
>>
> Chris Angelico said that casefold is not perfect. In the future, I want to
> make the perfect international-case-insensitive if-statement. For now, my
> code only supports a limited range of characters. Even with casefold, I will
> have some issues as Chris Angelico mentioned. Also, "ß" is not really the
> same as "ss".

Well, casefold is about as good as it's ever going to be, but that's
because "the perfect international-case-insensitive comparison" is a
fundamentally impossible goal. Your last sentence hints as to why;
there is no simple way to compare strings containing those characters,
because the correct treatment varies according to context.

Your two best options are: Be case sensitive (and then you need only
worry about composition and combining characters and all those
nightmares - the ones you have to worry about either way), or use
casefold(). Of those, I prefer the first, because it's safer; the
second is also a good option.

ChrisA

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Share Code Tips Devyn Collier Johnson <devyncjohnson@gmail.com> - 2013-07-19 09:51 -0400
  Re: Share Code Tips Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-19 17:59 +0000
    Re: Share Code Tips Devyn Collier Johnson <devyncjohnson@gmail.com> - 2013-07-19 18:08 -0400
      Re: Share Code Tips Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-20 03:18 +0000
        Re: Share Code Tips Devyn Collier Johnson <devyncjohnson@gmail.com> - 2013-07-20 06:07 -0400
    Re: Share Code Tips Chris Angelico <rosuav@gmail.com> - 2013-07-20 09:08 +1000
    Re: Share Code Tips Dave Angel <davea@davea.name> - 2013-07-19 19:09 -0400
    Re: Share Code Tips Devyn Collier Johnson <devyncjohnson@gmail.com> - 2013-07-19 21:04 -0400
      Re: Share Code Tips Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2013-07-20 03:44 +0000
        Re: Share Code Tips David Hutto <dwightdhutto@gmail.com> - 2013-07-20 00:15 -0400
        Re: Share Code Tips David Hutto <dwightdhutto@gmail.com> - 2013-07-20 00:22 -0400
        Re: Share Code Tips David Hutto <dwightdhutto@gmail.com> - 2013-07-20 00:26 -0400
        Re: Share Code Tips David Hutto <dwightdhutto@gmail.com> - 2013-07-20 00:27 -0400
        Re: Share Code Tips Devyn Collier Johnson <devyncjohnson@gmail.com> - 2013-07-20 06:10 -0400
        Re: Share Code Tips Devyn Collier Johnson <devyncjohnson@gmail.com> - 2013-07-20 08:36 -0400
    Re: Share Code Tips Chris Angelico <rosuav@gmail.com> - 2013-07-20 11:13 +1000
    Re: Share Code Tips Dave Angel <davea@davea.name> - 2013-07-19 21:51 -0400
    Re: Share Code Tips David Hutto <dwightdhutto@gmail.com> - 2013-07-19 23:42 -0400
    Re: Share Code Tips Devyn Collier Johnson <devyncjohnson@gmail.com> - 2013-07-20 06:06 -0400
    Re: Share Code Tips Devyn Collier Johnson <devyncjohnson@gmail.com> - 2013-07-20 08:20 -0400

csiph-web