You could use a Bunch class which transforms the dictionary into something that accepts dot notation.
You could use a Bunch class, which transforms the dictionary into something that accepts dot notation.
Just looked at its source, that's a slightly more polished version of my simple AttrDict (providing the correct AttributeError rather than KeyError, providing __repr__ and __delattr__ and providing a recursive conversion method). I doubt I'd ever have found it myself... "Bunch" seems a fairly non-descriptive name. – Chris Morgan Nov 21 '10 at 12:41 1 Thanks for knowing about/finding this class, that's really helpful.
– flexxy Nov 22 '10 at 2:09.
In JavaScript they're equivalent (often useful; I mention it in case you didn't know as you're doing web development), but in Python they're different - items versus .attributes. It's easy to write something which allows access through attributes, using __getattr__: class AttrDict(dict): def __getattr__(self, attr): return selfattr def __setattr__(self, attr, value): selfattr = value Then just use it as you'd use a dict (it'll accept a dict as a parameter, as it's extending dict), but you can do things like item. Image_url and it'll map it to item.
Image_url, getting or setting.
I recommend this way... – swcai Nov 21 '10 at 11:42 Nice, good suggestion. I'm going to accept @Daniel's answer since he found/knew a complete class for this, but it should be cool since you got 7 upvotes, including mine. Thanks!
– flexxy Nov 22 '10 at 2:10 Yep, if you don't mind an extra dependency (or just want to pull it into the main tree), Bunch is a more accurate solution. Normally this would be sufficient but that one is more accurate (AttributeError instead of KeyError and providing del support in particular). Thanks :-) – Chris Morgan Nov 22 '10 at 2:15.
I don't know what the implications will be, but I would add a method to the django model which reads the dictionary into itself, so you can access the data through the model.
I haven't actually worked with customizing models much, so I have to admit I'm not sure what that means. Using .values() to get a ValuesQuerySet is another option, too, which would simply change the model object into a dictionary. – flexxy Nov 21 '10 at 11:29.
Attributes must be accessed with dot notation. Coming from the API, the data is a dictionary and is accessed through subscript notation. In either case, some processing is done on the data.
I'm trying to find a more elegant, DRY way to do this. Is there a way to get/set by key that works on either dictionaries or objects?
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.