Coder Social home page Coder Social logo

unflatten's Introduction

unflatten - convert flat dict to nested dict/array

Latest Version Python versions build status

Description

This package provides a function which can unpack a flat dictionary into a structured dict with nested sub-dicts and/or sub-lists.

Development takes place on github. The package is installable from PyPI

Synopsis

Nested dicts:

>>> from unflatten import unflatten

>>> unflatten({'foo.bar': 'val'})
{'foo': {'bar': 'val'}}

Nested list:

>>> unflatten({'foo[0]': 'x', 'foo[1]': 'y'})
{'foo': ['x', 'y']}

Nested lists and dicts, intermixed:

>>> unflatten({
...     'foo[0][0]': 'a',
...     'foo[0][1]': 'b',
...     'foo[1].x': 'c',
...      })
{'foo': [['a', 'b'], {'x': 'c'}]}

Notes

Unflatten takes a single argument which should either be a dict (or an object with a dict-like .items() or .iteritems() method) or a sequence of (key, value) pairs. All keys in the dict or sequence must be strings. (Under python 2, keys must be instances of basestring; under python 3, keys just be instances of str.)

Unflatten always returns a dict. By way of example:

>>> unflatten([('[0]', 'x')])
{'': ['x']}

For list-valued nodes, all indexes must be present in the input (flattened) mapping, otherwise a ValueError will be thrown:

>>> unflatten({'a[0]': 'x', 'a[2]': 'y'})
Traceback (most recent call last):
...
ValueError: missing key 'a[1]'

See Also

The morph and flattery packages purport to implement similar functions.

Authors

Jeff Dairiki

unflatten's People

Contributors

dairiki avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.