Dict Style Attribute Assignment

Anonymous avatarAnonymous 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'
phone.save()

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'}
phone.update(data)
phone.save()

What do you think?

Comments (5)

  1. 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. Max Noel, what it your opinion on this ?

  2. 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'}
       phone.__dict__.update(data)
       phone.save()
    
  3. 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:

    phone.__dict__.update(data)
    

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

  4. Log in to comment
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.