Getting a dictionary from an object

Discussion in 'Python' started by Thanos Tsouanas, Jul 23, 2005.

  1. Hello.

    I would like to have a quick way to create dicts from object, so that a
    call to foo['bar'] would return obj.bar.

    The following works, but I would prefer to use a built-in way if one
    exists. Is there one?

    Thanks in advance.

    class dictobj(dict):
    """
    class dictobj(dict):
    A dictionary d with an object attached to it,
    which treats d['foo'] as d.obj.foo.
    """
    def __init__(self, obj):
    self.obj = obj
    def __getitem__(self, key):
    return self.obj.__getattribute__(key)

    --
    Thanos Tsouanas .: My Music: http://www.thanostsouanas.com/
    http://thanos.sians.org/ .: Sians Music: http://www.sians.org/
    Thanos Tsouanas, Jul 23, 2005
    #1
    1. Advertising

  2. Thanos Tsouanas a écrit :
    > Hello.
    >
    > I would like to have a quick way to create dicts from object, so that a
    > call to foo['bar'] would return obj.bar.
    >
    > The following works, but I would prefer to use a built-in way if one
    > exists. Is there one?
    >
    > Thanks in advance.
    >
    > class dictobj(dict):
    > """
    > class dictobj(dict):
    > A dictionary d with an object attached to it,
    > which treats d['foo'] as d.obj.foo.
    > """
    > def __init__(self, obj):
    > self.obj = obj
    > def __getitem__(self, key):
    > return self.obj.__getattribute__(key)


    I'd replace this last line with:
    return getattr(self.obj, key)


    Now given your specs, I don't see what's wrong with your solution.
    Bruno Desthuilliers, Jul 23, 2005
    #2
    1. Advertising

  3. On Sat, Jul 23, 2005 at 11:30:19AM +0200, Bruno Desthuilliers wrote:
    > Thanos Tsouanas a écrit :
    > > class dictobj(dict):
    > > """
    > > class dictobj(dict):
    > > A dictionary d with an object attached to it,
    > > which treats d['foo'] as d.obj.foo.
    > > """
    > > def __init__(self, obj):
    > > self.obj = obj
    > > def __getitem__(self, key):
    > > return self.obj.__getattribute__(key)

    >
    > I'd replace this last line with:
    > return getattr(self.obj, key)
    >
    > Now given your specs, I don't see what's wrong with your solution.


    I just dont want to use my class, if one already exists in the
    libraries (or any other way to achieve the same thing), that's all ;)

    Thanks for the tip.

    --
    Thanos Tsouanas .: My Music: http://www.thanostsouanas.com/
    http://thanos.sians.org/ .: Sians Music: http://www.sians.org/
    Thanos Tsouanas, Jul 23, 2005
    #3
  4. On Sat, 23 Jul 2005 11:48:27 +0300, Thanos Tsouanas wrote:

    > Hello.
    >
    > I would like to have a quick way to create dicts from object, so that a
    > call to foo['bar'] would return obj.bar.


    That looks rather confusing to me. Why not just call obj.bar, since it
    doesn't look like you are actually using the dictionary at all?

    > The following works, but I would prefer to use a built-in way if one
    > exists. Is there one?
    >
    > Thanks in advance.
    >
    > class dictobj(dict):
    > """
    > class dictobj(dict):
    > A dictionary d with an object attached to it,
    > which treats d['foo'] as d.obj.foo.
    > """
    > def __init__(self, obj):
    > self.obj = obj
    > def __getitem__(self, key):
    > return self.obj.__getattribute__(key)


    I don't think this is particularly useful behaviour. How do you use it?

    py> D = dictobj("hello world")
    py> D
    {}
    py> D.obj
    'hello world'
    py> D["food"] = "spam"
    py> D
    {'food': 'spam'}
    py> D["food"]
    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    File "<stdin>", line 5, in __getitem__
    AttributeError: 'str' object has no attribute 'food'


    --
    Steven.
    Steven D'Aprano, Jul 23, 2005
    #4
  5. Thanos Tsouanas

    Mike Meyer Guest

    Steven D'Aprano <> writes:
    > On Sat, 23 Jul 2005 11:48:27 +0300, Thanos Tsouanas wrote:
    >> Hello.
    >>
    >> I would like to have a quick way to create dicts from object, so that a
    >> call to foo['bar'] would return obj.bar.

    >
    > That looks rather confusing to me. Why not just call obj.bar, since it
    > doesn't look like you are actually using the dictionary at all?


    Well, I needed exactly this functionality last week. I have a
    collection of (rather messy) classes that have a slew of attributes as
    values. I would have used a dictionary for this, but I didn't write
    the code.

    I have to be able to display these objects (in HTML, if it matters),
    and have as a requirement that the format string live in a database.

    My solution didn't look to different from dictobj. There's some extra
    mechanism to fetch the format string from the database, and some
    formatting of the attribute based on meta-information in the object,
    but it's the same basic idea.

    >> class dictobj(dict):
    >> """
    >> class dictobj(dict):
    >> A dictionary d with an object attached to it,
    >> which treats d['foo'] as d.obj.foo.
    >> """
    >> def __init__(self, obj):
    >> self.obj = obj
    >> def __getitem__(self, key):
    >> return self.obj.__getattribute__(key)

    >
    > I don't think this is particularly useful behaviour. How do you use it?


    def __str__(self):
    return self._format % self


    <mike
    --
    Mike Meyer <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
    Mike Meyer, Jul 23, 2005
    #5
  6. On Sat, Jul 23, 2005 at 11:22:21PM +1000, Steven D'Aprano wrote:
    > On Sat, 23 Jul 2005 11:48:27 +0300, Thanos Tsouanas wrote:
    > > Hello.
    > >
    > > I would like to have a quick way to create dicts from object, so that a
    > > call to foo['bar'] would return obj.bar.

    >
    > That looks rather confusing to me. Why not just call obj.bar, since it
    > doesn't look like you are actually using the dictionary at all?
    >
    > > [...]

    >
    > I don't think this is particularly useful behaviour. How do you use it?


    print foo %do

    where do is a dictobj object...

    --
    Thanos Tsouanas .: My Music: http://www.thanostsouanas.com/
    http://thanos.sians.org/ .: Sians Music: http://www.sians.org/
    Thanos Tsouanas, Jul 24, 2005
    #6
  7. Thanos Tsouanas wrote:
    > I would like to have a quick way to create dicts from object, so that a
    > call to foo['bar'] would return obj.bar.
    >
    > The following works, but I would prefer to use a built-in way if one
    > exists. Is there one?


    Maybe I'm not understanding your problem, but have you looked at the
    builtin "vars()"?

    STeVe
    Steven Bethard, Jul 24, 2005
    #7
  8. On Sat, 23 Jul 2005 13:50:36 -0400, Mike Meyer wrote:

    >> I don't think this is particularly useful behaviour. How do you use it?

    >
    > def __str__(self):
    > return self._format % self


    That doesn't work. It calls self.__str__ recursively until Python halts
    the process.

    >>> class Thing(dict):

    .... _format = "Thing %s is good."
    .... def __str__(self):
    .... return self._format % self
    ....
    >>> X = Thing()
    >>> X # calls __repr__ so is safe

    {}
    >>> str(X) # not safe

    File "<stdin>", line 4, in __str__
    File "<stdin>", line 4, in __str__
    ...
    File "<stdin>", line 4, in __str__
    File "<stdin>", line 4, in __str__
    RuntimeError: maximum recursion depth exceeded


    --
    Steven.
    Steven D'Aprano, Jul 24, 2005
    #8
  9. On Sun, 24 Jul 2005 02:09:54 +0300, Thanos Tsouanas wrote:

    > On Sat, Jul 23, 2005 at 11:22:21PM +1000, Steven D'Aprano wrote:
    >> On Sat, 23 Jul 2005 11:48:27 +0300, Thanos Tsouanas wrote:
    >> > Hello.
    >> >
    >> > I would like to have a quick way to create dicts from object, so that a
    >> > call to foo['bar'] would return obj.bar.

    >>
    >> That looks rather confusing to me. Why not just call obj.bar, since it
    >> doesn't look like you are actually using the dictionary at all?
    >>
    >> > [...]

    >>
    >> I don't think this is particularly useful behaviour. How do you use it?

    >
    > print foo %do
    >
    > where do is a dictobj object...


    Are you telling me that the ONLY thing you use dictobj objects for is to
    print them?

    I don't think so. I do know how to print an object, amazingly.

    Perhaps you would like to explain how you use the rest of the
    functionality of the dictobj, instead of taking my words out of context
    and giving an inane answer.

    Why jump through all those hoops to get attributes when Python already
    provides indexing and attribute grabbing machinery that work well? Why do
    you bother to subclass dict, only to mangle the dict __getitem__ method so
    that you can no longer retrieve items from the dict?


    --
    Steven.
    Steven D'Aprano, Jul 24, 2005
    #9
  10. On Sun, Jul 24, 2005 at 01:43:43PM +1000, Steven D'Aprano wrote:
    > On Sun, 24 Jul 2005 02:09:54 +0300, Thanos Tsouanas wrote:
    > >
    > > print foo %do
    > >
    > > where do is a dictobj object...

    >
    > Are you telling me that the ONLY thing you use dictobj objects for is to
    > print them?


    I'm sorry to disappoint you, but yes. When you have a long text
    template to fill-out, with lots of %(foo)s, and all those foos are
    attributes of an object, it really helps to have dictobj.

    > I don't think so. I do know how to print an object, amazingly.


    Please, tell me, how would you print it in my case?

    > Perhaps you would like to explain how you use the rest of the
    > functionality of the dictobj, instead of taking my words out of context
    > and giving an inane answer.


    I dont see _ANY_ other functionality in the dictobj class. Do you?

    > Why jump through all those hoops to get attributes when Python already
    > provides indexing and attribute grabbing machinery that work well? Why do
    > you bother to subclass dict, only to mangle the dict __getitem__ method so
    > that you can no longer retrieve items from the dict?


    Because *obviously* I don't know of these indexing and attribute
    grabbing machineries you are talking about in my case. If you cared to
    read my first post, all I asked was for the "normal", "built-in" way to
    do it. Now, is there one, or not?

    --
    Thanos Tsouanas .: My Music: http://www.thanostsouanas.com/
    http://thanos.sians.org/ .: Sians Music: http://www.sians.org/
    Thanos Tsouanas, Jul 24, 2005
    #10
  11. On Sat, Jul 23, 2005 at 06:59:43PM -0600, Steven Bethard wrote:
    > Thanos Tsouanas wrote:
    > > I would like to have a quick way to create dicts from object, so that a
    > > call to foo['bar'] would return obj.bar.
    > >
    > > The following works, but I would prefer to use a built-in way if one
    > > exists. Is there one?

    >
    > Maybe I'm not understanding your problem, but have you looked at the
    > builtin "vars()"?


    I didn't know about it, but I knew about object.__dict__ which is, as I
    see equivalent with vars(object). But it doesn't do the job for me,
    since it fails to grab all obj.foo's, some of them being properties,
    etc.

    vars() is good to know though, Thanks!

    --
    Thanos Tsouanas .: My Music: http://www.thanostsouanas.com/
    http://thanos.sians.org/ .: Sians Music: http://www.sians.org/
    Thanos Tsouanas, Jul 24, 2005
    #11
  12. On Sun, Jul 24, 2005 at 02:01:30PM +0200, Bruno Desthuilliers wrote:
    > Thanos Tsouanas a écrit :
    > > On Sun, Jul 24, 2005 at 01:43:43PM +1000, Steven D'Aprano wrote:
    > >
    > > Because *obviously* I don't know of these indexing and attribute
    > > grabbing machineries you are talking about in my case. If you cared to
    > > read my first post, all I asked was for the "normal", "built-in" way to
    > > do it. Now, is there one, or not?

    >
    > If you re-read your first post, you'll notice that you didn't say
    > anything about the intention, only about implementation !-)


    """The following works, but I would prefer to use a built-in way if one
    exists. Is there one?"""

    > Now if your *only* need is to access object as a dict for formated
    > output, you don't need to subclass dict. This is (well, should be) enough:
    >
    > class Wrapper(object):
    > def __init__(self, obj):
    > self._obj = obj
    > def __getitem__(self, name):
    > return getattr(self._obj, name)
    >
    > This works with 'normal' attributes as well as with properties. Notice
    > that this wrapper is read-only, and don't pretend to be a real
    > dictionnary - but still it implements the minimum required interface for
    > "%(attname)s" like formatting.


    Thanks!! You made clear what 'the extra functionality' was. Indeed
    there is no need to subclass dict...

    > HTH


    it does!

    > Bruno
    > --
    > http://mail.python.org/mailman/listinfo/python-list


    --
    Thanos Tsouanas .: My Music: http://www.thanostsouanas.com/
    http://thanos.sians.org/ .: Sians Music: http://www.sians.org/
    Thanos Tsouanas, Jul 24, 2005
    #12
  13. Thanos Tsouanas a écrit :
    > On Sun, Jul 24, 2005 at 01:43:43PM +1000, Steven D'Aprano wrote:


    (snip)
    >
    >>Why jump through all those hoops to get attributes when Python already
    >>provides indexing and attribute grabbing machinery that work well? Why do
    >>you bother to subclass dict, only to mangle the dict __getitem__ method so
    >>that you can no longer retrieve items from the dict?


    >
    > Because *obviously* I don't know of these indexing and attribute
    > grabbing machineries you are talking about in my case. If you cared to
    > read my first post, all I asked was for the "normal", "built-in" way to
    > do it. Now, is there one, or not?


    If you re-read your first post, you'll notice that you didn't say
    anything about the intention, only about implementation !-)

    Now if your *only* need is to access object as a dict for formated
    output, you don't need to subclass dict. This is (well, should be) enough:

    class Wrapper(object):
    def __init__(self, obj):
    self._obj = obj
    def __getitem__(self, name):
    return getattr(self._obj, name)

    This works with 'normal' attributes as well as with properties. Notice
    that this wrapper is read-only, and don't pretend to be a real
    dictionnary - but still it implements the minimum required interface for
    "%(attname)s" like formatting.

    HTH
    Bruno
    Bruno Desthuilliers, Jul 24, 2005
    #13
  14. Bruno Desthuilliers a écrit :
    (snip)
    > class Wrapper(object):
    > def __init__(self, obj):
    > self._obj = obj
    > def __getitem__(self, name):
    > return getattr(self._obj, name)


    If you want the Wrapper to be more like a Decorator (ie still can use
    the Wrapper object as if it was the wrapped object), you can add this:

    def __getattr__(self, name):
    return getattr(self._obj, name)

    def __setattr__(self, name, val):
    if name == '_obj':
    super(Wrapper, self).__setattr__(name, val)
    else:
    setattr(self._obj, name, val)

    The Python cookbook may have some receipes too for this kind of funny
    things...
    Bruno Desthuilliers, Jul 24, 2005
    #14
  15. On Sun, Jul 24, 2005 at 03:01:40PM +0200, Bruno Desthuilliers wrote:
    > I gave you a solution based on the Decorator pattern in another post,
    > but there is also the possibility to add a __getitem__ method directly
    > to the to-be-formatted object's class:
    >
    > def mygetitem(obj, name):
    > return getattr(obj, name)
    >
    > setattr(obj.__class__, '__getitem__', mygetitem)
    > obj['bar']


    I used what you suggested earlier with the Wrapper, without subclassing
    dict anymore. Thanks!

    > <meta>
    > BTW, parts of this thread should remind us all that it's usually better
    > to clearly describe the *problem* before asking for comments on the
    > solution...
    > </meta>


    """I would like to have a quick way to create dicts from object, so
    that a call to foo['bar'] would return obj.bar."""

    Actually this is the problem, (I never said anything about _assigning_
    new keys in foo), and the line following it is my question ;)

    Thanks again!

    --
    Thanos Tsouanas .: My Music: http://www.thanostsouanas.com/
    http://thanos.sians.org/ .: Sians Music: http://www.sians.org/
    Thanos Tsouanas, Jul 24, 2005
    #15
  16. Steven D'Aprano a écrit :
    > On Sun, 24 Jul 2005 02:09:54 +0300, Thanos Tsouanas wrote:
    >
    >

    (snip)
    >
    > Are you telling me that the ONLY thing you use dictobj objects for is to
    > print them?
    >
    > I don't think so. I do know how to print an object, amazingly.
    >
    > Perhaps you would like to explain how you use the rest of the
    > functionality of the dictobj, instead of taking my words out of context
    > and giving an inane answer.
    >
    > Why jump through all those hoops to get attributes when Python already
    > provides indexing and attribute grabbing machinery that work well? Why do
    > you bother to subclass dict, only to mangle the dict __getitem__ method so
    > that you can no longer retrieve items from the dict?
    >


    The idea of the OP is not to use the dictobj as a full fledged dict,
    just to wrap the obj in something that is dict-like enough to be used
    for "%(attname)s" formatting. I also assume that he doesn't want to
    manually alter the code of each and every class to achieve this !-)

    So we can certainly agree that subclassing dict here is overkill and a
    bit misleading, but there are probably better ways to express this
    feeling. Of course, it would have been simpler if the OP had tell us
    from the start what was it's use case, but what...

    One could of course use metaclass tricks and the like to customize the
    objects __str__ or __repr__ (as in David Mertz's gnosis.magic package),
    but that would be overkill too IMHO.

    The plain old Decorator[1] pattern is probably enough in this case,
    since it's quite easy to implement a generic Decorator in Python.
    Another solution could be to dynamically modify the to-be-wrapped
    object's class to add a __getitem__ method.
    Bruno Desthuilliers, Jul 24, 2005
    #16
  17. Thanos Tsouanas a écrit :
    >>On Sat, 23 Jul 2005 11:48:27 +0300, Thanos Tsouanas wrote:
    >>
    >>>Hello.
    >>>
    >>>I would like to have a quick way to create dicts from object, so that a
    >>>call to foo['bar'] would return obj.bar.

    >>

    (snip)

    > print foo %do
    >
    > where do is a dictobj object...


    I gave you a solution based on the Decorator pattern in another post,
    but there is also the possibility to add a __getitem__ method directly
    to the to-be-formatted object's class:

    def mygetitem(obj, name):
    return getattr(obj, name)

    setattr(obj.__class__, '__getitem__', mygetitem)
    obj['bar']


    <meta>
    BTW, parts of this thread should remind us all that it's usually better
    to clearly describe the *problem* before asking for comments on the
    solution...
    </meta>

    My 2 cents...
    Bruno
    Bruno Desthuilliers, Jul 24, 2005
    #17
  18. Re: [meta] Getting a dictionary from an object

    Thanos Tsouanas a écrit :
    > On Sun, Jul 24, 2005 at 02:01:30PM +0200, Bruno Desthuilliers wrote:
    >

    (snip)
    >>If you re-read your first post, you'll notice that you didn't say
    >>anything about the intention, only about implementation !-)

    >
    > """The following works, but I would prefer to use a built-in way if one
    > exists. Is there one?"""


    This is about the " how", not about the "why". The "why" is:
    fmt_string % dictobj

    BTW, there are many templating solutions in Python, that are heavier but
    may (or may not, depending on the context) scale better or be more
    usable than string formatting...

    Bruno
    Bruno Desthuilliers, Jul 24, 2005
    #18
  19. On Sun, 24 Jul 2005 12:03:47 +0300, Thanos Tsouanas wrote:

    > On Sun, Jul 24, 2005 at 01:43:43PM +1000, Steven D'Aprano wrote:
    >> On Sun, 24 Jul 2005 02:09:54 +0300, Thanos Tsouanas wrote:
    >> >
    >> > print foo %do
    >> >
    >> > where do is a dictobj object...

    >>
    >> Are you telling me that the ONLY thing you use dictobj objects for is to
    >> print them?

    >
    > I'm sorry to disappoint you, but yes. When you have a long text
    > template to fill-out, with lots of %(foo)s, and all those foos are
    > attributes of an object, it really helps to have dictobj.


    Ah, now we're making progress in finding out what the purpose of the
    dictobj is! Thank you, this is starting to become clearer now.

    >> I don't think so. I do know how to print an object, amazingly.

    >
    > Please, tell me, how would you print it in my case?


    If I have understood you, you have some object like such:

    obj.foo = 1
    obj.bar = 2
    obj.spam = 'a'
    obj.eggs = 'b'

    say.

    You want to use it something like this:

    print "My object has fields %(foo)s; %(bar)s; %(spam)s; %(eggs)s." % obj

    except that doesn't work. So I would simply change the reference to obj to
    obj.__dict__ and it should do exactly what you want.

    Does that help?

    [snip]
    > Because *obviously* I don't know of these indexing and attribute
    > grabbing machineries you are talking about in my case. If you cared to
    > read my first post, all I asked was for the "normal", "built-in" way to
    > do it. Now, is there one, or not?


    I did read your first post. Unfortunately, you had not explained what you
    were trying to do very well. Your initial solution involved sub-classing
    dict. I made the fatal mistake of trying to guess what you needed from
    your sample code -- a natural mistake to make, given how vague your
    requirements were. Or rather, non-existent.

    It really does help to explain what your functional requirements are,
    instead of focusing on one, possibly pointless, implementation.

    If I have understood your functional requirements correctly, you don't
    need "to have a quick way to create dicts from object, so that a call to
    foo['bar'] would return obj.bar" at all.



    --
    Steven.
    Steven D'Aprano, Jul 24, 2005
    #19
  20. On Sun, 24 Jul 2005 12:07:02 +0300, Thanos Tsouanas wrote:

    > On Sat, Jul 23, 2005 at 06:59:43PM -0600, Steven Bethard wrote:
    >> Thanos Tsouanas wrote:
    >> > I would like to have a quick way to create dicts from object, so that a
    >> > call to foo['bar'] would return obj.bar.
    >> >
    >> > The following works, but I would prefer to use a built-in way if one
    >> > exists. Is there one?

    >>
    >> Maybe I'm not understanding your problem, but have you looked at the
    >> builtin "vars()"?

    >
    > I didn't know about it, but I knew about object.__dict__ which is, as I
    > see equivalent with vars(object). But it doesn't do the job for me,
    > since it fails to grab all obj.foo's, some of them being properties,
    > etc.


    You could have mentioned this earlier.

    But I don't think you are correct. As far as I can see, properties do have
    an entry in obj.__dict__ the same as other attributes, although there is
    certainly some strangeness going on with properties.

    Using the sample code from here:
    http://www.python.org/2.2.3/descrintro.html#property

    class C(object):
    def __init__(self):
    self.__x = 0
    def getx(self):
    return self.__x
    def setx(self, x):
    if x < 0: x = 0
    self.__x = x
    x = property(getx, setx)

    I see _C__x in C().__dict__, exactly as expected. (The _C is Python's
    standard name mangling of "semi-private" attributes starting with double
    underscores.)

    I can't see any way to inspect a Python object and get a list of
    properties, so you might have to keep your own list: add a class-attribute
    of your object which keeps a list of all the properties:

    class Obj:
    # various methods, attributes and properties
    ...
    # keep a list of special properties that don't show
    # up correctly in __dict__
    special = ['foo', 'bar']

    # now define a special method that makes a copy of
    # __dict__ and adds special properties to it

    def objdict(self):
    D = self.__dict__.copy()
    # assume shallow copy is enough
    for property_name in self.special:
    D[property_name] = self.__getattribute__(property_name)
    return D

    then call it when you need it:

    print "My object has fields %(foo)s and %(bar)s." % obj.objdict()


    It would be nice to see an easier way to introspect objects and get
    a list of properties.


    --
    Steven.
    Steven D'Aprano, Jul 24, 2005
    #20
    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. Ilias Lazaridis
    Replies:
    6
    Views:
    437
    Ilias Lazaridis
    Feb 21, 2006
  2. james_027
    Replies:
    1
    Views:
    322
    Marc 'BlackJack' Rintsch
    Aug 22, 2007
  3. Navkirat Singh
    Replies:
    6
    Views:
    3,027
    Navkirat Singh
    Jul 29, 2010
  4. Chris Rebert
    Replies:
    0
    Views:
    521
    Chris Rebert
    Jul 29, 2010
  5. Fox

    dictionary within dictionary

    Fox, Mar 8, 2005, in forum: ASP General
    Replies:
    5
    Views:
    184
    Michael D. Kersey
    Mar 13, 2005
Loading...

Share This Page