Re: Configuring an object via a dictionary

2024-03-20 Thread Dan Sommers via Python-list
On 2024-03-20 at 09:49:54 +0100, Roel Schroeven via Python-list wrote: > You haven't only checked for None! You have rejected *every* falsish value, > even though they may very well be acceptable values. OTOH, only you can answer these questions about your situations. Every application, every

Re: Configuring an object via a dictionary

2024-03-20 Thread Roel Schroeven via Python-list
Op 19/03/2024 om 0:44 schreef Gilmeh Serda via Python-list: On Mon, 18 Mar 2024 10:09:27 +1300, dn wrote: > YMMV! > NB your corporate Style Guide may prefer 'the happy path'... If you only want to check for None, this works too: >>> name = None >>> dafault_value = "default" >>> name or

Re: Configuring an object via a dictionary

2024-03-19 Thread Pokemon Chw via Python-list
It's too complicated, there's no need for this def __init__(self, config): self.__dict__ = config self.connection = None """ other code . """ Note that you need to keep the fields in the config dict named the same as the fields you want to be

Re: Configuring an object via a dictionary

2024-03-18 Thread Loris Bennett via Python-list
Tobiah writes: > I should mention that I wanted to answer your question, > but I wouldn't actually do this. I'd rather opt for > your self.config = config solution. The config options > should have their own namespace. > > I don't mind at all referencing foo.config['option'], > or you could

Re: Configuring an object via a dictionary

2024-03-18 Thread Tobiah via Python-list
I should mention that I wanted to answer your question, but I wouldn't actually do this. I'd rather opt for your self.config = config solution. The config options should have their own namespace. I don't mind at all referencing foo.config['option'], or you could make foo.config an object by

Re: Configuring an object via a dictionary

2024-03-18 Thread Tobiah via Python-list
On 3/15/24 02:30, Loris Bennett wrote: Hi, I am initialising an object via the following: def __init__(self, config): self.connection = None self.source_name = config['source_name'] self.server_host = config['server_host'] However, with a view to asking

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

RE: Configuring an object via a dictionary

2024-03-17 Thread AVI GROSS via Python-list
ars(**mydict) The above is really just keeping alpha. Of course if the possible keys are not known in advance, this does not work but other languages that allow this may be better for your purpose. -Original Message- From: Python-list On Behalf Of Peter J. Holzer via Python-list Sent: Sunday, M

Re: Configuring an object via a dictionary

2024-03-17 Thread dn via Python-list
On 18/03/24 04:11, Peter J. Holzer via Python-list wrote: On 2024-03-17 17:15:32 +1300, dn via Python-list wrote: On 17/03/24 12:06, Peter J. Holzer via Python-list wrote: On 2024-03-16 08:15:19 +, Barry via Python-list wrote: On 15 Mar 2024, at 19:51, Thomas Passin via Python-list

Re: Configuring an object via a dictionary

2024-03-17 Thread Peter J. Holzer via Python-list
On 2024-03-17 17:15:32 +1300, dn via Python-list wrote: > On 17/03/24 12:06, Peter J. Holzer via Python-list wrote: > > On 2024-03-16 08:15:19 +, Barry via Python-list wrote: > > > > On 15 Mar 2024, at 19:51, Thomas Passin via Python-list > > > > wrote: > > > > I've always like writing using

Re: Configuring an object via a dictionary

2024-03-16 Thread dn via Python-list
On 17/03/24 12:06, Peter J. Holzer via Python-list wrote: On 2024-03-16 08:15:19 +, Barry via Python-list wrote: On 15 Mar 2024, at 19:51, Thomas Passin via Python-list wrote: I've always like writing using the "or" form and have never gotten bit I, on the other hand, had to fix a

Re: Configuring an object via a dictionary

2024-03-16 Thread Peter J. Holzer via Python-list
On 2024-03-16 08:15:19 +, Barry via Python-list wrote: > > On 15 Mar 2024, at 19:51, Thomas Passin via Python-list > > wrote: > > I've always like writing using the "or" form and have never gotten bit > > I, on the other hand, had to fix a production problem that using “or” > introducted.

Re: Configuring an object via a dictionary

2024-03-16 Thread dn via Python-list
On 16/03/24 21:15, Barry via Python-list wrote: On 15 Mar 2024, at 19:51, Thomas Passin via Python-list wrote: I've always like writing using the "or" form and have never gotten bit I, on the other hand, had to fix a production problem that using “or” introducted. I avoid this idiom

Re: Configuring an object via a dictionary

2024-03-16 Thread Thomas Passin via Python-list
On 3/16/2024 8:12 AM, Roel Schroeven via Python-list wrote: Barry via Python-list schreef op 16/03/2024 om 9:15: > On 15 Mar 2024, at 19:51, Thomas Passin via Python-list   wrote: > > I've always like writing using the "or" form and have never gotten bit I, on the other hand, had to fix a

Re: Configuring an object via a dictionary

2024-03-16 Thread Roel Schroeven via Python-list
Barry via Python-list schreef op 16/03/2024 om 9:15: > On 15 Mar 2024, at 19:51, Thomas Passin via Python-list wrote: > > I've always like writing using the "or" form and have never gotten bit I, on the other hand, had to fix a production problem that using “or” introducted. I avoid this

Re: Configuring an object via a dictionary

2024-03-16 Thread Barry via Python-list
> On 15 Mar 2024, at 19:51, Thomas Passin via Python-list > wrote: > > I've always like writing using the "or" form and have never gotten bit I, on the other hand, had to fix a production problem that using “or” introducted. I avoid this idiom because it fails on falsy values. Barry --

RE: Configuring an object via a dictionary

2024-03-15 Thread AVI GROSS via Python-list
of control. -Original Message- From: Python-list On Behalf Of Dan Sommers via Python-list Sent: Friday, March 15, 2024 5:33 PM To: python-list@python.org Subject: Re: Configuring an object via a dictionary On 2024-03-15 at 15:48:17 -0400, Thomas Passin via Python-list wrote: > [...]

Re: Configuring an object via a dictionary

2024-03-15 Thread Thomas Passin via Python-list
On 3/15/2024 5:33 PM, Dan Sommers via Python-list wrote: On 2024-03-15 at 15:48:17 -0400, Thomas Passin via Python-list wrote: [...] And I suppose there is always the possibility that sometime in the future an "or" clause like that will be changed to return a Boolean, which one would expect

Re: Configuring an object via a dictionary

2024-03-15 Thread dn via Python-list
On 15/03/24 22:30, Loris Bennett via Python-list wrote: Hi, I am initialising an object via the following: def __init__(self, config): self.connection = None self.source_name = config['source_name'] self.server_host = config['server_host']

Re: Configuring an object via a dictionary

2024-03-15 Thread Dan Sommers via Python-list
On 2024-03-15 at 15:48:17 -0400, Thomas Passin via Python-list wrote: > [...] And I suppose there is always the possibility that sometime in > the future an "or" clause like that will be changed to return a > Boolean, which one would expect anyway. Not only is the current value is way more

Re: Configuring an object via a dictionary

2024-03-15 Thread Thomas Passin via Python-list
On 3/15/2024 3:09 PM, Grant Edwards via Python-list wrote: On 2024-03-15, Thomas Passin via Python-list wrote: On 3/15/2024 5:30 AM, Loris Bennett via Python-list wrote: Hi, I am initialising an object via the following: def __init__(self, config): self.connection = None

Re: Configuring an object via a dictionary

2024-03-15 Thread Grant Edwards via Python-list
On 2024-03-15, Thomas Passin via Python-list wrote: > On 3/15/2024 5:30 AM, Loris Bennett via Python-list wrote: >> Hi, >> >> I am initialising an object via the following: >> >> def __init__(self, config): >> >> self.connection = None >> >> self.source_name =

Re: Configuring an object via a dictionary

2024-03-15 Thread Thomas Passin via Python-list
On 3/15/2024 5:30 AM, Loris Bennett via Python-list wrote: Hi, I am initialising an object via the following: def __init__(self, config): self.connection = None self.source_name = config['source_name'] self.server_host = config['server_host']

Re: Configuring an object via a dictionary

2024-03-15 Thread Mats Wichmann via Python-list
On 3/15/24 03:30, Loris Bennett via Python-list wrote: Hi, I am initialising an object via the following: self.source_name = config['source_name'] config.get('source_name', default_if_not_defined) is a common technique... --