News for hackers. Stuff that matters.
Re:ISO name change for Türkiye by nemoniac on Tuesday July 12, 2022 @07:49AM (#32068498)
In Python:

    >>> "Türkiye".upper()
    
    'TÜRKIYE'
    
    >>> import locale
    
    >>> locale.setlocale(locale.LC_ALL, "tr_TR.UTF-8")
    
    'tr_TR.UTF-8'
    
    >>> "Türkiye".upper()
    
    'TÜRKIYE' # Expect "TÜRKİYE"
    
    >>> locale.resetlocale()
Re:ISO name change for Türkiye by gumby on Tuesday July 12, 2022 @08:07AM (#32068666)
Thank you for calling locale.resetlocale() before exiting.
Re:ISO name change for Türkiye by wyldfire on Tuesday July 12, 2022 @09:06AM (#32069291)
Maybe there should be a context manager?
Re:ISO name change for Türkiye by nerdponx on Tuesday July 12, 2022 @11:36AM (#32071471)
Absolutely. It's not that hard to write your own though:

    import locale
    from contextlib import contextmanager

    @contextmanager
    def use_locale(*args, **kwargs):
        locale.setlocale(*args, **kwargs)
        yield
        locale.resetlocale()
Contextlib is one of the under-appreciated gems in Python: https://docs.python.org/3/library/contextlib.html#contextlib...
Re:ISO name change for Türkiye by catskul2 on Tuesday July 12, 2022 @03:01PM (#32074309)
> Maybe there should be a context manager?

Even despite the:

> Absolutely.

My absolute least favorite response to this is:

> It's not that hard to write your own though:

Re:ISO name change for Türkiye by nerdponx on Tuesday July 12, 2022 @03:23PM (#32074526)
The alternative is trying to get one merged into the standard library. At least writing your own is better than not having one!
Re:ISO name change for Türkiye by catskul2 on Wednesday July 13, 2022 @10:03AM (#32082867)
I often see this answer as a reason it's not particularly important to have on in a standard library.

I strongly disagree with that sentiment as often these simple things have subtle gotchas, and/or subtle differences in possible net effects creating much bigger problems than their diminutive implementations would suggest.

Re:ISO name change for Türkiye by nerdponx on Wednesday July 13, 2022 @11:59AM (#32084518)
I generally feel the same way as you. My comment about getting it merged was not meant to imply that it shouldn't get merged, but that it might be difficult to. I find it very hard to predict which of these small QoL features will be accepted by the core devs and which features will be rejected for some reason or other.
Re:ISO name change for Türkiye by kzrdude on Tuesday July 12, 2022 @11:41AM (#32071549)
Does not really help parallel code
Re:ISO name change for Türkiye by dolmen on Tuesday July 12, 2022 @12:36PM (#32072411)
Use a global lock in the context manager.

Or use a programming language that doesn't rely on libc for locales.

Re:ISO name change for Türkiye by neuronexmachina on Tuesday July 12, 2022 @02:50PM (#32074158)
Well dang: https://docs.python.org/3.12/library/locale.html

> There is no way to perform case conversions and character classifications according to the locale. For (Unicode) text strings these are done according to the character value only, while for byte strings, the conversions and classifications are done according to the ASCII value of the byte, and bytes whose high bit is set (i.e., non-ASCII bytes) are never converted or considered part of a character class such as letter or whitespace.

Re:ISO name change for Türkiye by mzs on Tuesday July 12, 2022 @05:53PM (#32076177)
I actually like that python doesn't do locale aware case conversions. You can use ICU* for that, though it shows more warts (like the i in republic and that you have to handle Chinese banknotes differently as well):

  % env - LC_ALL=en_US.UTF-8 PATH="$PATH" python3
  Python 3.8.12 (default, Nov 13 2021, 10:49:08) 
  [Clang 11.0.3 (clang-1103.0.32.62)] on darwin
  Type "help", "copyright", "credits" or "license" for more information.
  >>> from icu import UnicodeString, Locale
  >>> s = b'the Republic of T\xc3\xbcrkiye'
  >>> s = s.decode()
  >>> s
  'the Republic of Türkiye'
  >>> lc = Locale("TR")
  >>> s = UnicodeString(s)
  >>> s
  <UnicodeString: 'the Republic of Türkiye'>
  >>> s = s.toUpper(lc)
  >>> s
  <UnicodeString: 'THE REPUBLİC OF TÜRKİYE'>
  >>> s = str(s)
  >>> s
  'THE REPUBLİC OF TÜRKİYE'
  >>> s.encode()
  b'THE REPUBL\xc4\xb0C OF T\xc3\x9cRK\xc4\xb0YE'
  >>> s = UnicodeString(s)
  >>> lc = Locale("CN")
  >>> s = s.toLower(lc)
  >>> s
  <UnicodeString: 'the republi̇c of türki̇ye'>
  >>> s = '壹,貳,參,肆,伍,陸,柒,捌,玖,拾,佰,仟,萬'
  >>> s.encode()
  b'\xe5\xa3\xb9,\xe8\xb2\xb3,\xe5\x8f\x83,\xe8\x82\x86,\xe4\xbc\x8d,\xe9\x99\xb8,\xe6\x9f\x92,\xe6\x8d\x8c,\xe7\x8e\x96,\xe6\x8b\xbe,\xe4\xbd\xb0,\xe4\xbb\x9f,\xe8\x90\xac'
  >>> s = UnicodeString(s)
  >>> s
  <UnicodeString: '壹,貳,參,肆,伍,陸,柒,捌,玖,拾,佰,仟,萬'>
  >>> s = s.toLower(lc)
  >>> s
  <UnicodeString: '壹,貳,參,肆,伍,陸,柒,捌,玖,拾,佰,仟,萬'>

* https://pypi.org/project/PyICU/

edit: Oh neat there's a hansfin keyword that I had not known about.

https://unicode-org.github.io/icu/userguide/locale/#keywords

https://stackoverflow.com/questions/6224177/how-to-convert-e...

Re:ISO name change for Türkiye by Bedon292 on Tuesday July 12, 2022 @10:42AM (#32070696)
Definitely odd, I did a bit more checking in another comment: https://news.ycombinator.com/item?id=32070549