1

Take the following code for instance:

def facebook_sync_album(album_id, thumbnails_only=False):
    args = {'fields':'id,images,source'}
    if thumbnails_only:
        args['limit'] = ALBUM_THUMBNAIL_LIMIT
    response = facebook_graph_query(album_id, 'photos', args=args)

Instead I was wondering if something similar to the following is possible:

def facebook_sync_album(album_id, thumbnails_only=False):
    photo_limit_arg = {'limit': ALBUM_THUMBNAIL_LIMIT}  if thumbnails_only else None
    response = facebook_graph_query_by_user_profile(album_id, 'photos', args={'fields':'id,images,source', photo_limit_arg})

So instead needing to predefine args in order to add an optional element (limit) I could instead pass a variable which expands to a value:key. Somewhat similar to the way you can expand a dict to kwargs using `kwargs

Is this possible?

DanH
  • 5,498
  • 4
  • 49
  • 72
  • Is your `args = {'fields':'id,images,source', photo_limit_arg}` in the 1st code block really a `args = {'fields':'id,images,source'}`? The `, photo_limit_arg` part doesn't make sense here. – glglgl Jan 17 '13 at 08:34
  • Yeah good point, I'll fix that, thanks! D: – DanH Jan 17 '13 at 09:06

2 Answers2

1

You're searching for the .update()-method of Python's dict. You could do:

def facebook_sync_album(album_id, thumbnails_only=False):
    args = {'fields':'id,images,source'}
    args.update({'limit': ALBUM_THUMBNAIL_LIMIT}  if thumbnails_only else {})
    response = facebook_graph_query_by_user_profile(album_id, 'photos', args=args)

Edit

The +-operator for dictionaries, as suggested in the comments, could be like:

class MyDict(dict):
    def __add__(self, other):
        if not isinstance(other, dict):
            return super(MyDict, self).__add__(other)
        return MyDict(self, **other)

    def __iadd__(self, other):
        if not isinstance(other, dict):
            return super(MyDict, self).__iadd__(other)
        self.update(other)
        return self

if __name__ == "__main__":
    print MyDict({"a":5, "b":3}) + MyDict({"c":5, "d":3})
    print MyDict({"a":5, "b":3}) + MyDict({"a":3})

    md = MyDict({"a":5, "b":3})
    md += MyDict({"a":7, "c":6})
    print md
Thorsten Kranz
  • 12,492
  • 2
  • 39
  • 56
  • This is no cleaner than the original. It'd be nice to have a builtin that works like + does for lists (which copies one and then calls update and returns the result), but alas there isn't one. – Danica Jan 17 '13 at 07:36
  • I like this, however eventually I got there with `dict(dict1, **dict2)`. Thanks! – DanH Jan 17 '13 at 07:41
  • 1
    For `+`: You could create your own dict-class, inheriting `dict`. It's fun, indeed (see my edit). – Thorsten Kranz Jan 17 '13 at 07:51
  • This is pretty interesting. Currently though I only have a single use case for this question, however this is a nice snippet. – DanH Jan 17 '13 at 07:56
  • 1
    Was only meant *just for fun*. In most cases, this will be an overkill. I always prefer using the basic container types. This makes your code more readable. Everybody who ever worked with C#, especially in a WCF-environment, nows what I'm talking about. – Thorsten Kranz Jan 17 '13 at 07:58
  • 2
    Thanks for your feedback. I added this to my sample. – Thorsten Kranz Jan 17 '13 at 08:37
0

Finally came up with the following thanks to https://stackoverflow.com/a/1552420/698289

def facebook_sync_album(album_id, thumbnails_only=False):
    photo_limit_arg = {'limit': ALBUM_THUMBNAIL_LIMIT}  if thumbnails_only else {}
    response = facebook_graph_query_by_user_profile(album_id, 'photos', args=dict({'fields':'id,images,source'}, **photo_limit_arg))
Community
  • 1
  • 1
DanH
  • 5,498
  • 4
  • 49
  • 72