ish/dottedish — Issue #2: Custom left and right delimiters to flatten?
Author: wreleven
Created: 2012-01-03 21:21:33+00:00
State: open

def flatten(o, left='.', right=''):

left	= '%s' % left
right	= '%s' % right
stack	= [(wrap(o).iteritems(), None)]

while stack:
items_iter, parent_key = stack[-1]
for (key, value) in items_iter:
if parent_key is None:
full_key = key
else:
full_key = left.join([parent_key, '%s%s' % (key, right)] )
if value is not unwrap(value):
stack.append((iter(value.items()), full_key))
break
yield full_key, value
else:
stack.pop()