[DICTIONARY] - Copy dictionary entries to attributes

I

Ilias Lazaridis

remark: not sure if the term "dictionary" is correct here.

I have the following situation:

within a setup.cfg, settings are passed this way:

settings=project_page=theProjectPage.com
myVar=myValue

those are accessible later like this:

settings['project_page'] / settings['myValue']

-

Now my question: is there any standard function to map the settings
directly to attributes?

something like:

dictionary_make_attributes(settings)

thus they can be accessed via:

settings.project_page / settings.myVar

or

copy_dictionary_entries_to_attributes(vars, settings)

vars.project_page / vars.myVar

?

..
 
D

Diez B. Roggisch

Ilias said:
remark: not sure if the term "dictionary" is correct here.

I have the following situation:

within a setup.cfg, settings are passed this way:

settings=project_page=theProjectPage.com
myVar=myValue

those are accessible later like this:

settings['project_page'] / settings['myValue']

-

Now my question: is there any standard function to map the settings
directly to attributes?

something like:

dictionary_make_attributes(settings)

thus they can be accessed via:

settings.project_page / settings.myVar

or

copy_dictionary_entries_to_attributes(vars, settings)

vars.project_page / vars.myVar


Either you use __getitem__, or the "bunch"-recipe:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52308

Diez
 
A

Alex Martelli

Ben Wilson said:
Perhaps:

def dictionary_make_attributes(self, settings):
for k,v in settings:
setattr(self, k, v)

This is a very general solution and will work for all kinds of objects
with settable attributes, even if some of the attributes are properties,
slots or weirder descriptors yet.

For plain vanilla class instances, though,
self.__dict__.update(settings)
may be sufficient.


Alex
 
I

Ilias Lazaridis

Diez said:
Ilias said:
remark: not sure if the term "dictionary" is correct here.

I have the following situation:

within a setup.cfg, settings are passed this way:

settings=project_page=theProjectPage.com
myVar=myValue

those are accessible later like this:

settings['project_page'] / settings['myValue']

-

Now my question: is there any standard function to map the settings
directly to attributes?

something like:

dictionary_make_attributes(settings)

thus they can be accessed via:

settings.project_page / settings.myVar

or

copy_dictionary_entries_to_attributes(vars, settings)

vars.project_page / vars.myVar

Either you use __getitem__, or the "bunch"-recipe:

http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52308

Diez

Looks intresting, but I am a newcomer to python.

How would the __getitem__ implementation look like?

..
 
I

Ilias Lazaridis

Ben said:
Perhaps:

def dictionary_make_attributes(self, settings):
for k,v in settings:
setattr(self, k, v)

this one resulted in an error:

"ValueError: too many values to unpack"

it works with this correction:

for k,v in settings.items()
 
I

Ilias Lazaridis

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top