[DICTIONARY] - Copy dictionary entries to attributes

Discussion in 'Python' started by Ilias Lazaridis, Feb 18, 2006.

  1. 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
    Ilias Lazaridis, Feb 18, 2006
    #1
    1. Advertising

  2. 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/Cookbook/Python/Recipe/52308

    Diez
    Diez B. Roggisch, Feb 18, 2006
    #2
    1. Advertising

  3. Ilias Lazaridis

    Ben Wilson Guest

    Ben Wilson, Feb 19, 2006
    #3
  4. Re: - Copy dictionary entries to attributes

    Ben Wilson <> 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
    Alex Martelli, Feb 19, 2006
    #4
  5. 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/Cookbook/Python/Recipe/52308
    >
    > Diez


    Looks intresting, but I am a newcomer to python.

    How would the __getitem__ implementation look like?

    ..

    --
    http://lazaridis.com
    Ilias Lazaridis, Feb 21, 2006
    #5
  6. Re: - Copy dictionary entries to attributes

    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-in-funcs.html#l2h-64




    --
    http://lazaridis.com
    Ilias Lazaridis, Feb 21, 2006
    #6
  7. Re: - Copy dictionary entries to attributes

    Alex Martelli wrote:
    > Ben Wilson <> 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
    Ilias Lazaridis, Feb 21, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alex
    Replies:
    2
    Views:
    1,206
  2. Replies:
    26
    Views:
    2,095
    Roland Pibinger
    Sep 1, 2006
  3. Priya
    Replies:
    9
    Views:
    261
  4. Lothar Werzinger
    Replies:
    4
    Views:
    533
    Benjamin Peterson
    Apr 28, 2010
  5. Don Bruder
    Replies:
    3
    Views:
    954
    spikeysnack
    Aug 3, 2010
Loading...

Share This Page