Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > [DICTIONARY] - Copy dictionary entries to attributes

Reply
Thread Tools

[DICTIONARY] - Copy dictionary entries to attributes

 
 
Ilias Lazaridis
Guest
Posts: n/a
 
      02-18-2006
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

?

..

--
http://lazaridis.com
 
Reply With Quote
 
 
 
 
Diez B. Roggisch
Guest
Posts: n/a
 
      02-18-2006
Ilias Lazaridis schrieb:
> 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/Coo...n/Recipe/52308

Diez

 
Reply With Quote
 
 
 
 
Ben Wilson
Guest
Posts: n/a
 
      02-19-2006
Perhaps:

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

http://ftp.python.org/doc/lib/built-...cs.html#l2h-64

 
Reply With Quote
 
Alex Martelli
Guest
Posts: n/a
 
      02-19-2006
Ben Wilson <(E-Mail Removed)> wrote:

> 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
 
Reply With Quote
 
Ilias Lazaridis
Guest
Posts: n/a
 
      02-21-2006
Diez B. Roggisch wrote:
> Ilias Lazaridis schrieb:
>> 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/Coo...n/Recipe/52308
>
> Diez


Looks intresting, but I am a newcomer to python.

How would the __getitem__ implementation look like?

..

--
http://lazaridis.com
 
Reply With Quote
 
Ilias Lazaridis
Guest
Posts: n/a
 
      02-21-2006
Ben Wilson wrote:
> 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()

> http://ftp.python.org/doc/lib/built-...cs.html#l2h-64




--
http://lazaridis.com
 
Reply With Quote
 
Ilias Lazaridis
Guest
Posts: n/a
 
      02-21-2006
Alex Martelli wrote:
> Ben Wilson <(E-Mail Removed)> wrote:
>
>> Perhaps:
>>
>> def dictionary_make_attributes(self, settings):
>> for k,v in settings:
>> setattr(self, k, v)


for k,v in settings.items()

> 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


this is the construct I was looking for.

see it active:

http://pudge.lesscode.org/trac/changeset/118

http://pudge.lesscode.org/trac/ticket/16

..

--
http://lazaridis.com
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Picking X random entries from linked list of Y entries Don Bruder C Programming 3 08-03-2010 09:10 AM
Oddity with large dictionary (several million entries) Lothar Werzinger Python 4 04-28-2010 01:43 AM
can the sequence of entries in a dictionary be depended on? Priya Python 9 11-24-2008 01:23 PM
is dict.copy() a deep copy or a shallow copy Alex Python 2 09-05-2005 07:01 AM
Tying up Port Login table entries with Port Table Entries in CISCO SNMP John Ramsden Cisco 0 07-24-2004 04:03 PM



Advertisments