Created
August 28, 2017 21:25
-
-
Save LuanComputacao/a44213c47cb8f89116b1b7c9dbcdba9d to your computer and use it in GitHub Desktop.
This file contains 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 characters
class DotExpandedDict(dict): | |
""" | |
- A special dictionary constructor that takes a dictionary in which the | |
- keys may contain dots to specify inner dictionaries. It's | |
- confusing, but this | |
- example should make sense. | |
- | |
- >>> d = DotExpandedDict({'person.1.firstname': ['Simon'], \ | |
- 'person.1.lastname': ['Willison'], \ | |
- 'person.2.firstname': ['Adrian'], \ | |
- 'person.2.lastname': ['Holovaty']}) | |
- >>> d | |
- {'person': { | |
- '1': {'lastname': ['Willison'], 'firstname': ['Simon']}, | |
- '2': {'lastname': ['Holovaty'], 'firstname': ['Adrian']} | |
- } | |
- } | |
- >>> d['person'] | |
- { | |
- '1': {'lastname': ['Willison'], 'firstname': ['Simon']}, | |
- '2': {'lastname': ['Holovaty'], 'firstname': ['Adrian']} | |
- } | |
- | |
- >>> d['person']['1'] | |
- {'lastname': ['Willison'], 'firstname': ['Simon']} | |
- | |
- # Gotcha: Results are unpredictable if the dots are "uneven": | |
- >>> DotExpandedDict({'c.1': 2, 'c.2': 3, 'c': 1}) | |
- {'c': 1} | |
- """ | |
def __init__(self, key_to_list_mapping): | |
for k, v in key_to_list_mapping.items(): | |
current = self | |
bits = k.split('.') | |
for bit in bits[:-1]: | |
current = current.setdefault(bit, {}) | |
# Now assign value to current position | |
try: | |
current[bits[-1]] = v | |
except TypeError: # Special-case if current isn't a dict. | |
current = {bits[-1]: v} | |
class BracketsExpandedDict(dict): | |
""" | |
A special dictionary constructor that takes a dictionary in which the keys | |
may contain dots to specify inner dictionaries. It's confusing, but this | |
example should make sense. | |
d = DotExpandedDict({ | |
u'company[id]': [u'1'], | |
u'csrfmiddlewaretoken': [u'JvIuFUcRxm31WvDMkJuZbK7h7r5tRhd0'], | |
u'member[1][email]': [u'luan.santos@crossknowledge.com'], | |
u'member[1][first_name]': [u'Roger'], | |
u'member[1][last_name]': [u'Luan'], | |
u'member[2][email]': [u'mauricio.bento@crossknowledge.com'], | |
u'member[2][first_name]': [u'Mauricio'], | |
u'member[2][last_name]': [u'Bento'], | |
u'member[3][email]': [u''], | |
u'member[3][first_name]': [u''], | |
u'member[3][last_name]': [u''], | |
u'member[4][email]': [u''], | |
u'member[4][first_name]': [u''], | |
u'member[4][last_name]': [u''], | |
u'personal_message': [u'Some message'], | |
u'team[name]': [u'CrosTest'] | |
}) | |
- >>> d | |
{'company': {'id': [u'1']}, | |
'csrfmiddlewaretoken': [u'JvIuFUcRxm31WvDMkJuZbK7h7r5tRhd0'], | |
'member': {'1': {'email': [u'luan.santos@crossknowledge.com'], | |
'first_name': [u'Roger'], | |
'last_name': [u'Luan']}, | |
'2': {'email': [u'mauricio.bento@crossknowledge.com'], | |
'first_name': [u'Mauricio'], | |
'last_name': [u'Bento']}, | |
'3': {'email': [u''], 'first_name': [u''], 'last_name': [u'']}, | |
'4': {'email': [u''], 'first_name': [u''], 'last_name': [u'']}}, | |
'personal_message': [u'Some message'], | |
'team': {'name': [u'CrosTest']}} | |
- >>> d['member'] | |
{ | |
'1': {'email': [u'luan.santos@crossknowledge.com'], | |
'first_name': [u'Roger'], | |
'last_name': [u'Luan']}, | |
'2': {'email': [u'mauricio.bento@crossknowledge.com'], | |
'first_name': [u'Mauricio'], | |
'last_name': [u'Bento']}, | |
'3': {'email': [u''], 'first_name': [u''], 'last_name': [u'']}, | |
'4': {'email': [u''], 'first_name': [u''], 'last_name': [u'']} | |
} | |
- >>> d['member']['1'] | |
{ | |
'email': [u'luan.santos@crossknowledge.com'], | |
'first_name': [u'Roger'], | |
'last_name': [u'Luan'] | |
} | |
- >>> d['member']['1']['email'][0] | |
# Gotcha: Results are unpredictable if the Brakets pair are "uneven": | |
- >>> DotExpandedDict({'c.1': 2, 'c.2': 3, 'c': 1}) | |
{'c': 1} | |
""" | |
def __init__(self, key_to_list_mapping): | |
import re | |
for k, v in key_to_list_mapping.items(): | |
current = self | |
bits = str( | |
re.sub( | |
'\]*$', | |
'', | |
re.sub( | |
'(\[|\]\[)', | |
'.', | |
re.sub( | |
'\[\]*$', | |
'._', | |
k)))).split('.') | |
for bit in bits[:-1]: | |
current = current.setdefault(bit, {}) | |
# Now assign value to current position | |
try: | |
current[bits[-1]] = v | |
except TypeError: # Special-case if current isn't a dict. | |
current = {bits[-1]: v} |
This file contains 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 characters
class DotExpandedDictTests(TestCase): | |
dict_uniform = { | |
'person[1][firstname]': ['Simon'], | |
'person[1][lastname]': ['Willison'], | |
'person[2][firstname]': ['Adrian'], | |
'person[2][lastname]': ['Holovaty'] | |
} | |
dict_uniform_wrong = { | |
'person[1[firstname[]]': ['Simon'], | |
'person[1[lastname[]]': ['Willison'], | |
'person[2[firstname[]]': ['Adrian'], | |
'person[2[lastname[]]': ['Holovaty'] | |
} | |
dict_uniform_expected = { | |
'person': { | |
'1': {'lastname': ['Willison'], 'firstname': ['Simon']}, | |
'2': {'lastname': ['Holovaty'], 'firstname': ['Adrian']} | |
} | |
} | |
dict_multiform = { | |
'person[1][firstname]': ['Simon'], | |
'person[1][lastname]': ['Willison'], | |
'person[2][firstname]': ['Adrian'], | |
'person[2][lastname]': ['Holovaty'], | |
'person[firstname]': ['John'], | |
'person[lastname]': ['Lenon']} | |
dict_multiform_expected = { | |
'person': { | |
'1': {'lastname': ['Willison'], 'firstname': ['Simon']}, | |
'lastname': ['Lenon'], | |
'2': {'lastname': ['Holovaty'], 'firstname': ['Adrian']}, | |
'firstname': ['John'] | |
} | |
} | |
def test_uniform_dict(self): | |
"""Test an uniform levels dict""" | |
self.assertEquals(BracketsExpandedDict(self.dict_uniform), | |
self.dict_uniform_expected) | |
def test_multiform_dict(self): | |
"""Test an multiform levels dict""" | |
self.assertEquals(BracketsExpandedDict(self.dict_multiform), | |
self.dict_multiform_expected) | |
class BracketsExpandedDictTests(TestCase): | |
dict_uniform = { | |
'person[1][firstname]': ['Simon'], | |
'person[1][lastname]': ['Willison'], | |
'person[2][firstname]': ['Adrian'], | |
'person[2][lastname]': ['Holovaty'] | |
} | |
dict_uniform_wrong = { | |
'person[1[firstname[]]': ['Simon'], | |
'person[1[lastname[]]': ['Willison'], | |
'person[2[firstname[]]': ['Adrian'], | |
'person[2[lastname[]]': ['Holovaty'] | |
} | |
dict_uniform_expected = { | |
'person': { | |
'1': {'lastname': ['Willison'], 'firstname': ['Simon']}, | |
'2': {'lastname': ['Holovaty'], 'firstname': ['Adrian']} | |
} | |
} | |
dict_multiform = { | |
'person[1][firstname]': ['Simon'], | |
'person[1][lastname]': ['Willison'], | |
'person[2][firstname]': ['Adrian'], | |
'person[2][lastname]': ['Holovaty'], | |
'person[firstname]': ['John'], | |
'person[lastname]': ['Lenon']} | |
dict_multiform_expected = { | |
'person': { | |
'1': {'lastname': ['Willison'], 'firstname': ['Simon']}, | |
'lastname': ['Lenon'], | |
'2': {'lastname': ['Holovaty'], 'firstname': ['Adrian']}, | |
'firstname': ['John'] | |
} | |
} | |
def test_uniform_dict(self): | |
"""Test an uniform levels dict""" | |
self.assertEquals(BracketsExpandedDict(self.dict_uniform), | |
self.dict_uniform_expected) | |
def test_multiform_dict(self): | |
"""Test an multiform levels dict""" | |
self.assertEquals(BracketsExpandedDict(self.dict_multiform), | |
self.dict_multiform_expected) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment