Code Monkey home page Code Monkey logo

dixi's Introduction

Dixi - Deep Dictionaries for Python

Installation

pip install dixi

Examples

from dixi import Dixi

data = Dixi({
    'Chris': {
        'age': 25,
        'address': {
            'city': 'Amsterdam',
            'country': 'Netherlands',
        },
    },
    'Anna': {
        'age': 19,
        'address': {
            'city': 'Zürich',
            'country': 'Switzerland',
        },
    },
    'John': {
        'age': 44,
        'address': {
            'city': 'London',
            'country': 'United Kingdom',
        },
    },
})

Deep indexing

data['John', 'age']
# >> 44

Partial indexing

data['Chris', 'address']
# >> {'city': 'Amsterdam', 'country': 'Netherlands'}

NumpPy style slicing

data[:, 'address', 'country']
# >> Dixi({'Chris': 'Netherlands', 'Anna': 'Switzerland', 'John': 'United Kingdom'})
data[['Chris', 'Anna'], 'age']
# >> {'Chris': 25, 'Anna': 19}

Setting

data['Derek', 'hobbies'] = ['Sewing', 'Archery']

Iteration

for key in data: # or key in data.leafkeys()
    print(key)
# >> ('Chris', 'age')
# >> ('Anna', 'age')
# >> ('Anna', 'address', 'city')
# >> ('Anna', 'address', 'country')
# >> ('John', 'age')
# >> ('John', 'address', 'city')
# >> ('John', 'address', 'country')
# >> ('Derek', 'hobbies')
for key in data.keys():
    print(key)
# >> Chris
# >> Anna
# >> John
# >> Derek
for key, value in data.items():
    print(key, value)
# >> Chris {'age': 25}
# >> Anna {'age': 19, 'address': {'city': 'Zürich', 'country': 'Switzerland'}}
# >> John {'age': 44, 'address': {'city': 'London', 'country': 'United Kingdom'}}
# >> Derek {'hobbies': ['Sewing', 'Archery']}
data = Dixi({
    0: {  0: 'a', 1: 'b' },
    1: { 0: 'c', 1: 'd' },
})
for keys, value in data.iterleaves():
    print(keys, value)
# >> (0, 0) a
# >> (0, 1) b
# >> (1, 0) c
# >> (1, 1) d

Deletion

del data['Chris', 'address']

Todo

  • Allow indexing for arrays

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.