Dict Style Attribute Assignment

Create issue
Issue #19 new
Former user created an issue

Hey all!

I'm wondering if you'd consider adding a feature that allows users to assign attribute values via dictionary style assignment, eg:

phone = Phone.get('blah')
phone['number'] = '18002223333'
phone['name'] = 'randall'

This would make some integration things I'm trying to accomplish much simpler.

It'd also be nice to be able to use the .update() method on objects to store dict data, eg:

phone = Phone.get('blah')
data = {'number': '18002223333', 'name': 'randall'}

What do you think?

Comments (5)

  1. Randall Degges

    Whoops, I accidentally posted the above as an anonymous user instead of logging in. Just posting here so I can see if anyone replies.

  2. Jean-Tiare Le Bigot

    I'm afraid the dictionnary style access would add some complexieties and confusion in the long run. As far as I remember, we deliberatly did not support it. But still, need to discuss it.

    Concerning the second part of your request, I think this would make sense as a utility function. @maxnoel, what it your opinion on this ?

  3. Max Noel

    Indeed, not supporting dict-style access is a point where we deliberately deviated from Mongokit's inspiration. I strongly dislike classes that basically alias __getitem__ et al. to __getattr__ and friends: they make code that uses them harder to understand, mess up IDE autocompletion and make the user believe they have free access to an arbitrary dict keyspace (and stuff like d["__table__"] = None will break messily), while providing very little benefit (namely, it occasionally allows you to not call getattr()/setattr() directly).

    Not to mention, implementing collections.MutableMapping takes a nontrivial amount of code that I'd rather we not have to maintain. -1 on supporting this as far as I'm concerned.

    The update thing may deserve some looking into, though, if it's a common use case. If you're not afraid of magic, for the time being you can use the following:

       phone = Phone.get('blah')
       data = {'number': '18002223333', 'name': 'randall'}
  4. Randall Degges

    This makes sense from a maintenance point of view.

    I still think the dict-style attributes would be cool. Whenever I've got data in an object, my first thought of accessing it is always via some sort of dictionary--especially when you've got models with lots of data points, it becomes cumbersome to reference each attribute.

    I think the primary use case for this is working with APIs. I'm using dynamodb-mapper extensively in several of the companies I run, primarily to speed up otherwise slow metadata fetching from a traditional relational DB like PostgreSQL (1 -> 5ms GET vs 25ms+). Since most of the stuff I do are APIs, I'm constantly doing serialization / deserialization type stuff with my dynamodb models, so thinking in dicts (if you will), seems a lot more natural to me.

    I'm going to currently update my codebases to do the meta magic you mentioned above, eg:


    Thank you again for looking this over. You guys have built something really awesome here, and it's really great to use it ^^

  5. Log in to comment