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


Groups > comp.lang.python > #50937

Re: Share Code Tips

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 2013-07-20 11:13 +1000
Subject Re: Share Code Tips
From Chris Angelico <rosuav@gmail.com>
Newsgroups comp.lang.python
Message-ID <mailman.4896.1374282806.3114.python-list@python.org> (permalink)

Show all headers | 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