Sujet : Re: Configuring an object via a dictionary (Posting On Python-List Prohibited)
De : ldo (at) *nospam* nz.invalid (Lawrence D'Oliveiro)
Groupes : comp.lang.pythonDate : 15. Mar 2024, 22:09:25
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <ut2de5$2fd1e$5@dont-email.me>
References : 1
User-Agent : Pan/0.155 (Kherson; fc5a80b8)
On Fri, 15 Mar 2024 10:30:03 +0100, Loris Bennett wrote:
self.source_name = config['source_name']
self.server_host = config['server_host']
self.server_port = config['server_port']
self.user_base = config['user_base']
self.user_identifier = config['user_identifier']
self.group_base = config['group_base']
self.group_identifier = config['group_identifier']
self.owner_base = config['owner_base']
>
However, some entries in the configuration might be missing.
Since all the attribute names and dictionary keys are the same, the
solution is simple:
for k in \
(
'source_name',
'server_host',
'server_port',
'user_base',
'user_identifier',
'group_base',
'group_identifier',
'owner_base',
) \
:
if k in config :
setattr(self, k, config[k])
#end if
#end for