PyDottie is a version of Dottie.js written in Python 3.

Overview

PyDottie

PyDottie is a version of Dottie.js written in Python 3.

Table of Contents.

Description

PyDottie helps you to play with nested keys in dictionaries.

Installation

$ pip install pydottie

or

$ pip3 install pydottie

Basic Usage

Get

Gets nested value, or None if unreachable, or a default value if passed.

# PyDottie
import pydottie

values = {
    'some': {
        'nested': {
            'key': 'foobar',
        }
    },
    'some.dot.included': {
        'key': 'barfoo'
    }
}

# returns 'foobar'
print(pydottie.get(values, 'some.nested.key'))

# returns None
print(pydottie.get(values, 'some.undefined.key'))

# returns 'defaultval'
print(pydottie.get(values, 'some.undefined.key', 'defaultval'))

# returns 'barfoo'
print(pydottie.get(values, ['some.dot.included', 'key']))

Set value

Sets nested value, creates nested structure if needed.

# PyDottie
import pydottie

values = {
    'some': {
        'nested': {
            'key': 'foobar',
        }
    },
    'some.dot.included': {
        'key': 'barfoo'
    }
}

pydottie.set_value(values, 'some.nested.value', 'someValue')

pydottie.set_value(values, ['some.dot.included', 'value'], 'someValue')

# force overwrite defined non-object keys into objects if needed
pydottie.set_value(values, 'some.nested.object', 'someValue', force=True)

Transform object

Transform dictionary from keys with dottie notation to nested objects.

# PyDottie
import pydottie

values = {
  'user.name': 'Gummy Bear',
  'user.email': '[email protected]',
  'user.professional.title': 'King',
  'user.professional.employer': 'Candy Mountain'
};

transformed = pydottie.transform(values);

print(transformed);
"""
{
    'user': {
        'name': 'Gummy Bear',
        'email': '[email protected]',
        'professional': {
            'title': 'King',
            'employer': 'Candy Mountain'
        }
    }
}
"""

With a custom delimiter

# PyDottie
import pydottie

values = {
  'user_name': 'Mick Hansen',
  'user_email': '[email protected]'
}
transformed = pydottie.transform(values, delimiter='_');

print(transformed);
"""
{
    'user': {
        'name': 'Mick Hansen',
        'email': '[email protected]'
    }
}
"""

Get paths in dictionary

# PyDottie
import pydottie

values = {
    'a': 1,
    'b': {
        'c': 2,
        'd': {
            'e': 3
        }
    }
}

# ['a', 'b.c', 'b.d.e'];
print(pydottie.paths(values))

Tests

Run the unit tests with the next command.

python3 -m unittest

Credits

Checkout the original source code of dottie.js at https://github.com/mickhansen/dottie.js

LICENSE

MIT

You might also like...
A simple google translator telegram bot version 2

Translator-Bot-V2 A simple google translator telegram bot version 2 Made with Python3 (C) @FayasNoushad Copyright permission under MIT License License

Modified Version of mega.py package for Pyrogram Bots

Pyro Mega.py Python library for the Mega.co.nz API, currently supporting: login uploading downloading deleting searching sharing renaming moving files

This repository will (hopefully) always contain the latest version of the libProfessorP.asm.so shared object.

libPuhfessorP - Deploy Repo This repo should (hopefully) always contain the latest version of the libPuhfessorP.asm.so shared object, to be linked wit

A FORKED AND Modded version of TL:GD for 🅱️3R0K🧲support
A FORKED AND Modded version of TL:GD for 🅱️3R0K🧲support

for support join here working example group Leech Here For Any Issues/Imrovements or Discussions go here or here Please Leave A star And Fork this Rep

The open source version of Tentro - A multipurpose Discord bot.

Welcome to Tentro 👋 A multipurpose Discord bot. 🏠 Homepage Install pip install -r requirements.txt Usage py Tentro.py Contributors 👤 Tentro Dev Tea

A PowerPacked Version Of Telegram Leech Bot With Modern Easy-To-Use Interface & UI !
A PowerPacked Version Of Telegram Leech Bot With Modern Easy-To-Use Interface & UI !

FuZionX Leech Bot A Powerful Telegram Leech Bot Modded by MysterySD to directly Leech to Telegram, with Multi Direct Links Support for Enhanced Leechi

A link shortner telegram bot version 2 with advanced features

URL-Shortner-Bot-V2 A link shortner telegram bot version 2 with advanced features Made with Python3 (C) @FayasNoushad Copyright permission under MIT L

The Main Pythonic Version Of Twig Using Nextcord
The Main Pythonic Version Of Twig Using Nextcord

The Main Pythonic Version Of Twig Using Nextcord

Modified Version Of Media Search bot

Modified Version Of Media Search bot

Owner
Jose Noriega
Software Engineer
Jose Noriega
Jalali version of python calendar :date:

jcalendar jcalendar is Jalali implementation of Python's calendar module Status Install pip install jcalendar Documents This module almost follows Py

Iman Kermani 7 Aug 9, 2022
The Python version of the official Discord bot for the Astura Studios Discord community server.

About Astura (Python version) is the official Discord bot for the Astura Studios Discord community server developed and maintained by Ascendus and the

Ascendus 1 Apr 21, 2022
This is my personal version of Pac-Man using python, which is the first assignment of EA Software Engineering Virtual Experience Program from Forage.com

Vac-Man in Python This is my personal version of Vax-man game using python, which is the first task of EA Software Engineering Virtual Experience Prog

ZiXiang Luo 3 Jan 5, 2022
Scuttlecrab.py - Python Version of Scuttle Crab Bot

____ _ _ _ ____ _ / ___| ___ _ _| |_|

Fabrizo 4 Jul 8, 2022
A cut down version of QUANT containing just the model in Python (QUANTPy)

A cut down version of QUANT containing just the model in Python (QUANTPy)

null 2 Apr 15, 2022
Python version of PlaceNL's headless bot with automatic access token refresh

Reddit /r/place 2022 headless bot This headless Python bot will automatically login to reddit, obtain access tokens (and refreshes them when they expi

null 19 May 21, 2022
The most expensive version of Conway's Game of Life - running on the Ethereum Blockchain

GameOfLife The most expensive implementation of Conway's Game of Life ever - over $2,000 per step! (Probably the slowest too!) Conway's Game of Life r

null 75 Nov 26, 2022
“ HOLA HUMANS 👋 I'M DAISYX 2.0 „ LATEST VERSION OF DAISYX.. Source Code of @Daisyxbot

DaisyX 2.0 A Powerful, Smart And Simple Group Manager ... Written with AioGram , Pyrogram and Telethon... The first AioGram based modified groupmanage

TeamDaisyX 153 Dec 6, 2022
“ HOLA HUMANS 👋 I'M DAISYX 2.0 ❤️ „ LATEST VERSION OF DAISYX.. Source Code of @Daisyxbot

❤️ DaisyX 2.0 ❤️ A Powerful, Smart And Simple Group Manager ... Written with AioGram , Pyrogram and Telethon... ⭐️ Thanks to everyone who starred Dais

TeamDaisyX 153 Dec 6, 2022
This Is Advanced Version Of Old Radio Player, An Telegram Bot to Play Radio/Music in Channel or Group Voice Chats.

Telegram Radio Player V2 An Telegram Bot to Play Radio/Music in Channel or Group Voice Chats. This is also the source code of the bot which is being u

SAF ONE 81 Dec 3, 2022