0

I have a dictionary like this with different permissions per users or per groups:

"permissions": [
            {
                "user": 4, 
                "type": "admin"
            }, 
            {
                "user": 3, 
                "type": "read"
            }, 
            {
                "group": 3, 
                "type": "admin"
            }
        ]

What is the most efficient way to obtain a grouping by permission type:

The numbers represent id of objects(users/groups) so I also have to "translate" them a little bit in the result:

"permissions": [
            {
                "type": "admin",
                "users":[
                 {"id":4}
                ],
                "groups":[
                 {"id":3}
                ]
            },
            {
                "type": "read",
                "users":[
                 {"id":3}
                ]
            },
]

Thanks for helping !

StackedUser
  • 327
  • 2
  • 13
  • 1
    What is your current code for doing this? Have you noticed a particular performance problem? – jonrsharpe Nov 13 '14 at 10:28
  • @jonrsharpe: I'm getting this data using Django Rest Framework by serializing multiple models: the user and group permissions are fetched from tho different db tables and aggregated in the permissions field. – StackedUser Nov 13 '14 at 10:36
  • @StackedUser that answers neither of my questions. – jonrsharpe Nov 13 '14 at 10:39
  • @jonrsharpe Sorry I misunderstood the question. I'm new to python. I don't currently have code for this yet, I am looking for the right direction, using maybe [link](https://docs.python.org/2/library/itertools.html#itertools.groupby). I'm trying to avoid a classic method using for, and I'm looking to take advantage of the power of python. thanks – StackedUser Nov 13 '14 at 11:08
  • @StackedUser this isn't a code-writing service, you should make a bit more effort and try implementing something yourself. If you're not tied to your current structure, I would suggest that `permissions = {'admin': {'users': [3], 'groups': [3]}, ...}` would make this easier. – jonrsharpe Nov 13 '14 at 11:08
  • @jonrsharpe ok, thanks, you are really friendly. That was not my intention to be given the full code for that, as in my OP I said looking for a way... just a pointer: eg. use group by to [etc], then convert the result to [etc] – StackedUser Nov 13 '14 at 11:16
  • @Trilarion How can this be a duplicate for getting a key from a dictionary ? I'm not trying to get a key by the value , I'm trying to group, efficiently, the values from a dictionary by a field's value. I hope I was clear enough – StackedUser Nov 13 '14 at 11:21

1 Answers1

0

To answer my own question, I've found the following solution:

My code, nothing smart, with simple iterations. In the first run I create an intermediary dictionary,grouping by permission type:

permissions_bytype={}
for permission_entry in permissions: #iterating over something like: [{'user': 1, 'type': u'read'}, {'user': 4, 'type': u'admin'}, {'user': 4, 'type': u'read'}]
    if permission_entry["type"] not in permissions_bytype:
        permissions_bytype[permission_entry["type"]] = {}
    for key,val in permission_entry.iteritems():
        if key <> "type": #is user or group
            if key not in permissions_bytype[permission_entry["type"]]:
                permissions_bytype[permission_entry["type"]][key] = []
            permissions_bytype[permission_entry["type"]][key].append({"id":val})

Then build the result:

result=[]
for perm_type,permission_value in permissions_bytype.iteritems():
        permission_value["type"] = perm_type
        result.append(permission_value)
StackedUser
  • 327
  • 2
  • 13