Giter VIP home page Giter VIP logo

addict's Introduction

addict - the Python Dict that's better than heroin.

build Status Coverage Status

addict is a Python module that gives you a dictionary whos values are both gettable and settable using both attribute and getitem syntax.

Never again will you have to write code like this:

body = {
    'query': {
        'filtered': {
            'query': {
                'match': {'description': 'addictive'}
            },
            'filter': {
                'term': {'created_by': 'Mats'}
            }
        }
    }
}

From now on, you may simply write the following three lines:

body = Dict()
body.query.filtered.query.match.description = 'addictive'
body.query.filtered.filter.term.created_by = 'Mats'

###Installing To install simply type

pip install addict

###Usage addict inherits from dict, but is way more flexible in terms of accessing and setting it's values. Working with dictionaries have never been more awesome than this! Setting the items of a nested Dict is a dream:

>>> from addict import Dict
>>> my_new_shiny_dict = Dict()
>>> my_new_shiny_dict.a.b.c.d.e = 2
>>> my_new_shiny_dict
{'a': {'b': {'c': 'd': {'e': 2}}}}

###Pruning It behaves much like a defaultdict, in the way that trying to get a nonexistent key will return a new, empty Dict instance. So trying to peek at an empty item will result in

>>> my_dict = Dict()
>>> my_dict.a = 2
>>> my_dict.b.c.d.e
{}
>>> my_dict
{'a': 2, 'b': {'c': 'd': {'e': {}}}}

But don't you worry, if you by mistake added some empty Dicts in your Dict, you can recursively delete those by running .prune() on your Dict

>>> my_dict.prune()
{'a': 2}

Also, remember that ints are not valid attribute names, so keys of the dict that are not strings must be set/get with the get-/setitem syntax

>>> my_other_shiny_dict = Dict()
>>> my_other_shiny_dict.a.b.c.d.e = 2
>>> my_other_shiny_dict[2] = [1, 2, 3]
{2: [1, 2, 3], 'a': {'b': {'c': 'd': {'e': 2}}}}

However feel free to mix the two syntaxes:

>>> my_other_shiny_dict.a.b['c'].d.e
2

###When is this especially useful?

This module rose from the entirely tiresome creation of elasticsearch queries in Python. Whenever you find yourself writing out dicts over multiple lines, just remember that you don't have to. Use a Dict instead.

###Perks As it is essentially a dict, it will also serialize into JSON perfectly. Ready for use with hopefully all other libraries that handles dicts.

###Testimonials @spiritsack - "Mother of God, this changes everything."

addict's People

Contributors

mewwts avatar spiritsack avatar svisser avatar

Watchers

 avatar  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.