Last active
August 29, 2015 14:21
-
-
Save cauethenorio/7b9ca57ed5cb856ed55e to your computer and use it in GitHub Desktop.
Revisions
-
cauethenorio revised this gist
May 23, 2015 . 1 changed file with 28 additions and 22 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,16 +1,19 @@ # coding: utf8 import re from functools import reduce def merge_dicts(*dicts): # http://stackoverflow.com/questions/7204805/dictionaries-of-dictionaries-merge if not reduce(lambda x, y: isinstance(y, dict) and x, dicts, True): raise TypeError("Object in *dicts not of type dict") if len(dicts) < 2: raise ValueError("Requires 2 or more dict objects") def merge(a, b): for d in set(a.keys()).union(list(b.keys())): if d in a and d in b: if type(a[d]) == type(b[d]): if not isinstance(a[d], dict): @@ -20,36 +23,39 @@ def merge(a, b): else: yield (d, dict(merge(a[d], b[d]))) else: raise TypeError("Conflicting key:value type assignment") elif d in a: yield (d, a[d]) elif d in b: yield (d, b[d]) else: raise KeyError return reduce(lambda x, y: dict(merge(x, y)), dicts[1:], dicts[0]) def convert_php_array_to_python_dict(forms_tuple): regexp = r'(?:^([a-zA-Z0-9_]*)|\[([a-zA-Z0-9_]+)\])' root = {} for raw_field, value in forms_tuple: matches = re.findall(regexp, raw_field) keys = [k1 or k2 for k1, k2 in matches] for i, key in enumerate(reversed(keys)): field = {key if not key.isdigit() else int(key): value.strip() if i == 0 else field} root = merge_dicts(root, field) return root if __name__ == '__main__': from django.conf import settings settings.configure() ## necessário para usar o QueryDict fora de uma aplicação Django from django.http import QueryDict querydict = QueryDict("data[id]=D6adasd7065C2C0A84C96B47EA17705991B14&data[status]=paid&event=invoice") print(convert_php_array_to_python_dict(querydict.items())) # {'event': 'invoice', 'data': {'id': 'D6adasd7065C2C0A84C96B47EA17705991B14', 'status': 'paid'}} -
cauethenorio created this gist
May 22, 2015 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,55 @@ import re def merge_dicts(*dicts): # http://stackoverflow.com/questions/7204805/dictionaries-of-dictionaries-merge if not reduce(lambda x, y: isinstance(y, dict) and x, dicts, True): raise TypeError, "Object in *dicts not of type dict" if len(dicts) < 2: raise ValueError, "Requires 2 or more dict objects" def merge(a, b): for d in set(a.keys()).union(b.keys()): if d in a and d in b: if type(a[d]) == type(b[d]): if not isinstance(a[d], dict): ret = list({a[d], b[d]}) if len(ret) == 1: ret = ret[0] yield (d, sorted(ret)) else: yield (d, dict(merge(a[d], b[d]))) else: raise TypeError, "Conflicting key:value type assignment" elif d in a: yield (d, a[d]) elif d in b: yield (d, b[d]) else: raise KeyError return reduce(lambda x, y: dict(merge(x, y)), dicts[1:], dicts[0]) def convert_php_array_to_python_dict(forms_tuple): regexp = r'(?:^([a-zA-Z0-9_]*)|\[([a-zA-Z0-9_]+)\])' root = {} for raw_field, value in forms_tuple: matches = re.findall(regexp, raw_field) keys = [k1 or k2 for k1, k2 in matches] for i, key in enumerate(reversed(keys)): field = {key if not key.isdigit() else int(key): value.strip() if i == 0 else field} root = merge_dicts(root, field) return root # querystring source = "data[id]=D6adasd7065C2C0A84C96B47EA17705991B14&data[status]=paid&event=invoice" from django.http import QueryDict querydict = QueryDict(source) convert_php_array_to_python_dict(querydict.items()) # {u'data': {u'status': u'paid', u'id': u'D6adasd7065C2C0A84C96B47EA17705991B14'}, u'event': u'invoice'}