Re: Configuring an object via a dictionary

2024-03-18 Thread Anders Munch via Python-list
dn wrote:
>Loris Bennett wrote:
>> However, with a view to asking forgiveness rather than
>> permission, is there some simple way just to assign the dictionary
>> elements which do in fact exist to self-variables?
>
>Assuming config is a dict:
>
>   self.__dict__.update( config )

Here's another approach:

config_defaults = dict(
 server_host='localhost',
 server_port=443,
# etc.
)
...
def __init__(self, config):
self.conf = types.SimpleNamespace(**{**config_defaults, **config})

This gives you defaults, simple attribute access, and avoids the risk of name 
collisions that you get when updating __dict__.

Using a dataclass may be better:

@dataclasses.dataclass
class Settings:
 group_base : str
 server_host : str = 'localhost'
 server_port : int = 443
...
def __init__(self, config):
self.conf = Settings(**config)

regards, Anders

-- 
https://mail.python.org/mailman/listinfo/python-list


[issue44283] Add jump table for certain safe match-case statements

2021-06-07 Thread Anders Munch


Anders Munch  added the comment:

Are you sure you want to do this?

This optimisation is not applicable if the matched values are given symbolic 
names.  You would be encouraging people to write bad code with lots of 
literals, for speed.

--
nosy: +AndersMunch

___
Python tracker 
<https://bugs.python.org/issue44283>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43115] locale.getlocale fails if locale is set

2021-02-23 Thread Anders Munch


Anders Munch  added the comment:

>> What does use getlocale is time.strptime and datetime.datetime.strptime, so 
>> when getlocale fails, strptime fails.

> Would they work with getlocale() returning None for the encoding ?

Yes. All getlocale is used for in _strptime.py is comparing the value returned 
to the previous value returned.

I expect changing _strptime._getlang to return the unnormalized value 
locale._setlocale(locale.LC_TIME, None) would work as well and more robustly.

--

___
Python tracker 
<https://bugs.python.org/issue43115>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43115] locale.getlocale fails if locale is set

2021-02-17 Thread Anders Munch


Anders Munch  added the comment:

> BTW: What is wxWidgets doing with the returned values ?

wxWidgets doesn't call getlocale, it's a C++ library (wrapped by wxPython) that 
uses C setlocale.

What does use getlocale is time.strptime and datetime.datetime.strptime, so 
when getlocale fails, strptime fails.

> We could enhance this to return None for the encoding instead
> of raising an exception, but would this really help ?

Very much so.

Frankly, I don't get the impression that the current locale preferred encoding 
is used for *anything*.  Other than possibly having a role in implementing 
getpreferredencoding.

> Alternatively, we could add "en_DE" to the alias table and set
> a default encoding to use. 

Where would you get a complete list of all the new aliases that would need be 
to be added?

As the "en_DE" example shows, you'd need all combinations of languages and 
regions.  That's going to be a very long list.

--

___
Python tracker 
<https://bugs.python.org/issue43115>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43115] locale.getlocale fails if locale is set

2021-02-17 Thread Anders Munch


Anders Munch  added the comment:

getlocale is documented to return None for the encoding if no encoding can be 
determined.  There's no need to guess.

I can't change the locale.setlocale call, because where I'm actually having the 
problem, I'm not even calling locale.setlocale: wxWidgets is calling C 
setlocale, that's where it comes from.

wxWidgets aside, it doesn't seem right that getlocale fails when setlocale 
succeeded.

--

___
Python tracker 
<https://bugs.python.org/issue43115>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43115] locale.getlocale fails if locale is set

2021-02-17 Thread Anders Munch


Anders Munch  added the comment:

I discovered that this can happen with underscores as well:

Python 3.8.7 (tags/v3.8.7:6503f05, Dec 21 2020, 17:59:51) [MSC v.1928 64 bit 
(AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'en_DE')
'en_DE'
>>> locale.getlocale()
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\flonidan\env\Python38-64\lib\locale.py", line 591, in getlocale
return _parse_localename(localename)
  File "C:\flonidan\env\Python38-64\lib\locale.py", line 499, in 
_parse_localename
raise ValueError('unknown locale: %s' % localename)
ValueError: unknown locale: en_DE

locale.setlocale does validate input - if you write nonsense in the second 
argument then you get an exception, "locale.Error: unsupported locale setting". 
 So I'm guessing the en_DE locale is actually being set here, and the problem 
is solely about getlocale making unfounded assumptions about the format.

Same thing happens in 3.10.0a4.

--

___
Python tracker 
<https://bugs.python.org/issue43115>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue43115] locale.getlocale fails if locale is set

2021-02-03 Thread Anders Munch


New submission from Anders Munch :

getlocale fails with an exception when the string returned by _setlocale is 
already an RFC 1766 language tag.

Example:

Python 3.10.0a4 (tags/v3.10.0a4:445f7f5, Jan  4 2021, 19:55:53) [MSC v.1928 64 
bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import locale
>>> locale.setlocale(locale.LC_ALL, 'en-US')
'en-US'
>>> locale.getlocale()
Traceback (most recent call last):
  File "", line 1, in 
  File "C:\flonidan\env\Python310\lib\locale.py", line 593, in getlocale
return _parse_localename(localename)
  File "C:\flonidan\env\Python310\lib\locale.py", line 501, in _parse_localename
raise ValueError('unknown locale: %s' % localename)
ValueError: unknown locale: en-US

Expected result:
  ('en-US', None)

See https://github.com/wxWidgets/Phoenix/issues/1637 for an example of the 
ensuing problems.  wx.Locale calls C setlocale directly, but, as far as I can 
see, correctly, using dashes in the language code which is consistent with not 
only RFC 1766 but also the examples in Microsoft's documentation 
(https://docs.microsoft.com/en-us/cpp/c-runtime-library/reference/setlocale-wsetlocale?view=msvc-160).
  CPython seems to assume underscored names such as 'en_US' instead, as shown 
by getdefaultlocale inserting an underscore.

--
components: Library (Lib), Windows
messages: 386203
nosy: AndersMunch, paul.moore, steve.dower, tim.golden, zach.ware
priority: normal
severity: normal
status: open
title: locale.getlocale fails if locale is set
versions: Python 3.10, Python 3.8, Python 3.9

___
Python tracker 
<https://bugs.python.org/issue43115>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



Re: Sorting NaNs

2018-06-11 Thread Anders Munch

Steven D'Aprano:


It is not a guess if the user explicitly specifies that as the behaviour.


If that was the context, sure, no problem.

- Anders

--
https://mail.python.org/mailman/listinfo/python-list


Re: Sorting NaNs

2018-06-10 Thread Anders Munch

Richard Damon wrote:


The two behaviors that I have heard suggested are:

1) If any of the inputs are a NaN, the median should be a NaN.
(Propagating the NaN as indicator of a numeric error)

2) Remove the NaNs from the input set and process what is left. If
nothing, then return a NaN (treating NaN as a 'No Data' placeholder).


3) Raise an exception.

I can't believe anyone even suggested 2).  "In the face of ambiguity, 
refuse the temptation to guess."


regards, Anders

--
https://mail.python.org/mailman/listinfo/python-list