Skip to main content

PyDottie is a library based on dottie.js written in Python 3.

Project description

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': 'gummybear@candymountain.com',
  'user.professional.title': 'King',
  'user.professional.employer': 'Candy Mountain'
};

transformed = pydottie.transform(values);

print(transformed);
"""
{
    'user': {
        'name': 'Gummy Bear',
        'email': 'gummybear@candymountain.com',
        'professional': {
            'title': 'King',
            'employer': 'Candy Mountain'
        }
    }
}
"""

With a custom delimiter

# PyDottie
import pydottie

values = {
  'user_name': 'Mick Hansen',
  'user_email': 'maker@mhansen.io'
}
transformed = pydottie.transform(values, delimiter='_');

print(transformed);
"""
{
    'user': {
        'name': 'Mick Hansen',
        'email': 'maker@mhansen.io'
    }
}
"""

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydottie-0.0.2.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydottie-0.0.2-py3-none-any.whl (7.4 kB view details)

Uploaded Python 3

File details

Details for the file pydottie-0.0.2.tar.gz.

File metadata

  • Download URL: pydottie-0.0.2.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.2

File hashes

Hashes for pydottie-0.0.2.tar.gz
Algorithm Hash digest
SHA256 5b2a6fb8f74928970f370920d6d2f3d43368b597041dcb41bdc8cdc7fd4bdf27
MD5 c42d23a30aacb74df49816bff652c6ac
BLAKE2b-256 202da811b3db5478959026c3755ae86e8b7a11673e9e58152a9803f53bed40eb

See more details on using hashes here.

File details

Details for the file pydottie-0.0.2-py3-none-any.whl.

File metadata

  • Download URL: pydottie-0.0.2-py3-none-any.whl
  • Upload date:
  • Size: 7.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.2

File hashes

Hashes for pydottie-0.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 31deed3765fe42c583837872c23f7b119502fca9b48dd2e79b498f9e8e502281
MD5 10262856c74463ebfdbb6476826b4b21
BLAKE2b-256 0ba839b3d07c3b9ccd282ec9c5e82422f1756f66bdbca5589401f8d72941cf47

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page