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

Custom left and right delimiters to flatten? #2

Open
wreleven opened this issue Jan 3, 2012 · 0 comments
Open

Custom left and right delimiters to flatten? #2

wreleven opened this issue Jan 3, 2012 · 0 comments

Comments

@wreleven
Copy link

wreleven commented Jan 3, 2012

I've customized flatten to allow for a two custom delimiters. I needed to be able to create post variables for a PHP api and your flatten function was the simplest thing to customize.

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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant