Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added option to change separator #2

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 24 additions & 18 deletions morph/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,56 +111,62 @@ def tolist(obj, flat=True, split=True):
return [obj]

#------------------------------------------------------------------------------
def flatten(obj):
def flatten(obj, separator='.'):
'''
TODO: add docs
'''
if len(separator) != 1:
raise ValueError('Separator must be a single character')

if isseq(obj):
ret = []
for item in obj:
if isseq(item):
ret.extend(flatten(item))
ret.extend(flatten(item, separator))
else:
ret.append(item)
return ret
if isdict(obj):
ret = dict()
for key, value in obj.items():
for skey, sval in _relflatten(value):
for skey, sval in _relflatten(value, separator):
ret[key + skey] = sval
return ret
raise ValueError(
'only list- and dict-like objects can be flattened, not %r' % (obj,))
def _relflatten(obj):
def _relflatten(obj, separator):
if isseq(obj):
for idx, subval in enumerate(obj):
for skey, sval in _relflatten(subval):
for skey, sval in _relflatten(subval, separator):
yield '[' + str(idx) + ']' + skey, sval
return
if isdict(obj):
for skey, sval in flatten(obj).items():
yield '.' + skey, sval
for skey, sval in flatten(obj, separator).items():
yield separator + skey, sval
return
yield '', obj

#------------------------------------------------------------------------------
def unflatten(obj):
def unflatten(obj, separator='.'):
'''
TODO: add docs
'''
if len(separator) != 1:
raise ValueError('Separator must be a single character')

if not isdict(obj):
raise ValueError(
'only dict-like objects can be unflattened, not %r' % (obj,))
ret = dict()
sub = dict()
for key, value in obj.items():
if '.' not in key and '[' not in key:
if separator not in key and '[' not in key:
ret[key] = value
continue
if '.' in key and '[' in key:
idx = min(key.find('.'), key.find('['))
elif '.' in key:
idx = key.find('.')
if separator in key and '[' in key:
idx = min(key.find(separator), key.find('['))
elif separator in key:
idx = key.find(separator)
else:
idx = key.find('[')
prefix = key[:idx]
Expand All @@ -171,18 +177,18 @@ def unflatten(obj):
if pfx in ret:
raise ValueError(
'conflicting scalar vs. structure for prefix: %s' % (pfx,))
ret[pfx] = _relunflatten(pfx, values)
ret[pfx] = _relunflatten(pfx, values, separator)
return ret
def _relunflatten(pfx, values):
def _relunflatten(pfx, values, separator):
if len(values) == 1 and list(values.keys())[0] == '':
return list(values.values())[0]
typ = set([k[0] for k in values.keys()])
if len(typ) != 1:
raise ValueError(
'conflicting structures (dict vs. list) for prefix: %s' % (pfx,))
typ = list(typ)[0]
if typ == '.':
return unflatten({k[1:]: v for k, v in values.items()})
if typ == separator:
return unflatten({k[1:]: v for k, v in values.items()}, separator)
tmp = dict()
for skey, sval in values.items():
if skey[0] != '[':
Expand All @@ -203,7 +209,7 @@ def _relunflatten(pfx, values):
if pos not in tmp:
tmp[pos] = dict()
tmp[pos][skey[idx + 1:]] = sval
return [_relunflatten(pfx + '[' + str(pos) + ']', tmp[pos])
return [_relunflatten(pfx + '[' + str(pos) + ']', tmp[pos], separator)
for pos in sorted(tmp.keys())]

#------------------------------------------------------------------------------
Expand Down
45 changes: 45 additions & 0 deletions morph/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,24 @@ def test_flatten(self):
'a.b[1][2]': 6,
})

#----------------------------------------------------------------------------
def test_flatten_separator(self):
self.assertEqual(
morph.flatten({'a': {'b': 'c'}}, separator='/'),
{'a/b': 'c'})
self.assertEqual(
morph.flatten({'a': {'b': 1, 'c': [2, {'d': 3, 'e': 4}]}}, separator='/'),
{'a/b': 1, 'a/c[0]': 2, 'a/c[1]/d': 3, 'a/c[1]/e': 4})
self.assertEqual(
morph.flatten({'a': {'b': [[1, 2], [3, {'x': 4, 'y': 5}, 6]]}}, separator='/'),
{'a/b[0][0]': 1,
'a/b[0][1]': 2,
'a/b[1][0]': 3,
'a/b[1][1]/x': 4,
'a/b[1][1]/y': 5,
'a/b[1][2]': 6,
})

#----------------------------------------------------------------------------
def test_unflatten_fail(self):
with self.assertRaises(ValueError) as cm:
Expand All @@ -137,6 +155,33 @@ def test_unflatten_fail(self):
self.assertEqual(
str(cm.exception),
'invalid list syntax (bad index) in key "a[NADA]"')
with self.assertRaises(ValueError) as cm:
morph.unflatten({'a': 'b', 'a.b.c': 'c'},separator='+/')
self.assertEqual(
str(cm.exception),
'Separator must be a single character')

#----------------------------------------------------------------------------
def test_unflatten_separator_ok(self):
self.assertEqual(
morph.unflatten({'a/b' : 'c', 'd' : 'e'}, separator='/'),
{'a' : {'b' : 'c'}, 'd' : 'e'})
self.assertEqual(
morph.unflatten({'a/b': 1, 'a/c[0]': 2, 'a/c[1]': 3, 'a/c[2]': 4}, separator='/'),
{'a': {'b': 1, 'c': [2, 3, 4]}})
self.assertEqual(
morph.unflatten({'a/b': 1, 'a/c[0]': 2, 'a/c[1]/d': 3, 'a/c[1]/e': 4}, separator='/'),
{'a': {'b': 1, 'c': [2, {'d': 3, 'e': 4}]}})
self.assertEqual(
morph.unflatten({
'a/b[0][0]': 1,
'a/b[0][1]': 2,
'a/b[1][0]': 3,
'a/b[1][1]/x': 4,
'a/b[1][1]/y': 5,
'a/b[1][2]': 6,
}, separator='/'),
{'a': {'b': [[1, 2], [3, {'x': 4, 'y': 5}, 6]]}})

#----------------------------------------------------------------------------
def test_unflatten_ok(self):
Expand Down