All Projects → mewwts → Addict

mewwts / Addict

Licence: mit
The Python Dict that's better than heroin.

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Addict

Box
Python dictionaries with advanced dot notation access
Stars: ✭ 1,804 (-15.74%)
Mutual labels:  dictionaries, addict
Dictionaries
One dictionary to rule them all -- a browser extension to help you learn languages
Stars: ✭ 134 (-93.74%)
Mutual labels:  dictionaries
Goherokuname
[Mirror] Heroku-like Random Names in Go
Stars: ✭ 41 (-98.09%)
Mutual labels:  dictionaries
Eazydict
简单易用的命令行词典 📕 📙 📗 📘 📓
Stars: ✭ 92 (-95.7%)
Mutual labels:  dict
Dictfier
Python library to convert/serialize class instances(Objects) both flat and nested into a dictionary data structure. It's very useful in converting Python Objects into JSON format
Stars: ✭ 67 (-96.87%)
Mutual labels:  dict
Zatt
Python implementation of the Raft algorithm for distributed consensus
Stars: ✭ 119 (-94.44%)
Mutual labels:  dictionaries
Ext Saladict
🥗 All-in-one professional pop-up dictionary and page translator which supports multiple search modes, page translations, new word notebook and PDF selection searching.
Stars: ✭ 8,418 (+293.18%)
Mutual labels:  dictionaries
Bitextor
Bitextor generates translation memories from multilingual websites.
Stars: ✭ 168 (-92.15%)
Mutual labels:  dictionaries
Chrome Extensions Youdaowithwordnode
有道词典chrome划词插件,原版不带生词本,在其源码基础上,添加了添加生词本的功能,生词本用的有道官方API
Stars: ✭ 74 (-96.54%)
Mutual labels:  dict
Flatdict
Python module for interacting with nested dicts as a single level dict with delimited keys.
Stars: ✭ 73 (-96.59%)
Mutual labels:  dict
Popup Dict
Linux 下的划词翻译小工具
Stars: ✭ 69 (-96.78%)
Mutual labels:  dict
Memorize
🚀 Japanese-English-Mongolian dictionary. It lets you find words, kanji and more quickly and easily
Stars: ✭ 72 (-96.64%)
Mutual labels:  dictionaries
Inside python dict
An explorable explanation of python dictionaries
Stars: ✭ 122 (-94.3%)
Mutual labels:  dict
Libgenerics
libgenerics is a minimalistic and generic library for C basic data structures.
Stars: ✭ 42 (-98.04%)
Mutual labels:  dict
Ipa Dict
Monolingual wordlists with pronunciation information in IPA
Stars: ✭ 139 (-93.51%)
Mutual labels:  dictionaries
Google Ime Dictionary
日英変換・英語略語展開のための IME 追加辞書 📙 日本語から英語への和英変換や英語略語の展開を Google 日本語入力や ATOK などで可能にする IME 拡張辞書です
Stars: ✭ 30 (-98.6%)
Mutual labels:  dictionaries
Glom
☄️ Python's nested data operator (and CLI), for all your declarative restructuring needs. Got data? Glom it! ☄️
Stars: ✭ 1,341 (-37.37%)
Mutual labels:  dictionaries
Sdcv
Stars: ✭ 171 (-92.01%)
Mutual labels:  dictionaries
Scalpl
A lightweight wrapper to operate on nested dictionaries seamlessly. 👌
Stars: ✭ 153 (-92.85%)
Mutual labels:  dict
Dynamictranslator
Instant translation application for windows in .NET 🎪
Stars: ✭ 131 (-93.88%)
Mutual labels:  dictionaries

addict

Tests Coverage Status PyPI version Anaconda-Server Badge

addict is a Python module that gives you dictionaries whose values are both gettable and settable using attributes, in addition to standard item-syntax.

This means that you don't have to write dictionaries like this anymore:

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

Instead, you can simply write the following three lines:

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

Installing

You can install via pip

pip install addict

or through conda

conda install addict -c conda-forge

Addict runs on Python 2 and Python 3, and every build is tested towards 2.7, 3.6 and 3.7.

Usage

addict inherits from dict, but is more flexible in terms of accessing and setting its values. Working with dictionaries are now a joy! Setting the items of a nested Dict is a dream:

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

If the Dict is instantiated with any iterable values, it will iterate through and clone these values, and turn dicts into Dicts. Hence, the following works

>>> mapping = {'a': [{'b': 3}, {'b': 3}]}
>>> dictionary = Dict(mapping)
>>> dictionary.a[0].b
3

but mapping['a'] is no longer the same reference as dictionary['a'].

>>> mapping['a'] is dictionary['a']
False

This behavior is limited to the constructor, and not when items are set using attribute or item syntax, references are untouched:

>>> a = Dict()
>>> b = [1, 2, 3]
>>> a.b = b
>>> a.b is b
True

Stuff to keep in mind

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

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

However feel free to mix the two syntaxes:

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

Attributes like keys, items etc.

addict will not let you override attributes that are native to dict, so the following will not work

>>> mapping = Dict()
>>> mapping.keys = 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "addict/addict.py", line 53, in __setattr__
    raise AttributeError("'Dict' object attribute '%s' is read-only" % name)
AttributeError: 'Dict' object attribute 'keys' is read-only

However, the following is fine

>>> a = Dict()
>>> a['keys'] = 2
>>> a
{'keys': 2}
>>> a['keys']
2

just like a regular dict. There are no restrictions (other than what a regular dict imposes) regarding what keys you can use.

Default values

For keys that are not in the dictionary, addict behaves like defaultdict(Dict), so missing keys return an empty Dict rather than raising KeyError. If this behaviour is not desired, it can be overridden using

>>> class DictNoDefault(Dict):
>>>     def __missing__(self, key):
>>>         raise KeyError(key)

but beware that you will then lose the shorthand assignment functionality (addicted.a.b.c.d.e = 2).

Recursive Fallback to dict

If you don't feel safe shipping your addict around to other modules, use the to_dict()-method, which returns a regular dict clone of the addict dictionary.

>>> regular_dict = my_addict.to_dict()
>>> regular_dict.a = 2
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'dict' object has no attribute 'a'

This is perfect for when you wish to create a nested Dict in a few lines, and then ship it on to a different module.

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

Counting

Dict's ability to easily access and modify deeply-nested attributes makes it ideal for counting. This offers a distinct advantage over collections.Counter, as it will easily allow for counting by multiple levels.

Consider this data:

data = [
    {'born': 1980, 'gender': 'M', 'eyes': 'green'},
    {'born': 1980, 'gender': 'F', 'eyes': 'green'},
    {'born': 1980, 'gender': 'M', 'eyes': 'blue'},
    {'born': 1980, 'gender': 'M', 'eyes': 'green'},
    {'born': 1980, 'gender': 'M', 'eyes': 'green'},
    {'born': 1980, 'gender': 'F', 'eyes': 'blue'},
    {'born': 1981, 'gender': 'M', 'eyes': 'blue'},
    {'born': 1981, 'gender': 'F', 'eyes': 'green'},
    {'born': 1981, 'gender': 'M', 'eyes': 'blue'},
    {'born': 1981, 'gender': 'F', 'eyes': 'blue'},
    {'born': 1981, 'gender': 'M', 'eyes': 'green'},
    {'born': 1981, 'gender': 'F', 'eyes': 'blue'}
]

If you want to count how many people were born in born of gender gender with eyes eyes, you can easily calculate this information:

counter = Dict()

for row in data:
    born = row['born']
    gender = row['gender']
    eyes = row['eyes']

    counter[born][gender][eyes] += 1

print(counter)
{1980: {'M': {'blue': 1, 'green': 3}, 'F': {'blue': 1, 'green': 1}}, 1981: {'M': {'blue': 2, 'green': 1}, 'F': {'blue': 2, 'green': 1}}}

Update

addicts update functionality is altered for convenience from a normal dict. Where updating nested item using a dict would overwrite it:

>>> d = {'a': {'b': 3}}
>>> d.update({'a': {'c': 4}})
>>> print(d)
{'a': {'c': 4}}

addict will recurse and actually update the nested Dict.

>>> D = Dict({'a': {'b': 3}})
>>> D.update({'a': {'c': 4}})
>>> print(D)
{'a': {'b': 3, 'c': 4}}

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 addict instead.

Perks

As it is a dict, it will serialize into JSON perfectly, and with the to_dict()-method you can feel safe shipping your addict anywhere.

Testing, Development and CI

Issues and Pull Requests are more than welcome. Feel free to open an issue to spark a discussion around a feature or a bug, or simply reply to the existing ones. As for Pull Requests, keeping in touch with the surrounding code style will be appreciated, and as such, writing tests are crucial. Pull requests and commits will be automatically run against TravisCI and coveralls.

The unit tests are implemented in the test_addict.py file and use the unittest python framework. Running the tests is rather simple:

python -m unittest -v test_addict

# - or -
python test_addict.py

Testimonials

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

@some guy on Hacker News - "...the purpose itself is grossly unpythonic"

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].