Coder Social home page Coder Social logo

rj-jesus / flatdict Goto Github PK

View Code? Open in Web Editor NEW

This project forked from gmr/flatdict

0.0 2.0 0.0 41 KB

Python module for interacting with nested dicts as a single level dict with delimited keys.

Home Page: http://flatdict.readthedocs.org

License: BSD 3-Clause "New" or "Revised" License

Python 100.00%

flatdict's Introduction

FlatDict

Version Downloads Status Coverage License

FlatDict is a dict object that allows for single level, delimited key/value pair mapping of nested dictionaries. You can interact with FlatDict like a normal dictionary and access child dicts as you normally would or with the composited key.

For example:

foo = {'foo': {'bar': 'baz', 'qux': 'corge'}}

is represented as:

{'foo:bar': 'baz',
 'foo:qux': 'corge'}

And can still be accessed as:

foo['foo']['bar']

and

foo['foo:bar']

Additionally, lists and tuples are also converted into dicts using enumerate().

For example:

d = {'list': ['a', 'b', 'c',]}

Will be flattened as follows:

flat = {'list:0': 'a', 'list:1': 'b', 'list:2': 'c'}

API

FlatDict has the same methods as dict in Python 2.6. In addition, it has a FlatDict.as_dict method which will return a pure nested dictionary from a FlatDict value.

Documentation is available at http://flatdict.readthedocs.org

Installation

$ pip install flatdict

Example Use

import flatdict

values = {'foo': {'bar': {'baz': 0,
                          'qux': 1,
                          'corge': 2},
                  'grault': {'baz': 3,
                             'qux': 4,
                             'corge': 5}},
          'garply': {'foo': 0, 'bar': 1, 'baz': 2, 'qux': {'corge': 3}}}

flat = flatdict.FlatDict(values)

print flat['foo:bar:baz']

flat['test:value:key'] = 10

del flat['test']

for key in flat:
    print key

for values in flat.itervalues():
    print key

print repr(flat.as_dict())

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.