Skip to main content

A library for accessing deep structures from simple text statements

Project description

The problem

Say you have a configuration file, or command line options, or whatever. And you want to have user-specified elements reference deep portions of a structure that you've defined, read in from yaml or json, or whatever. How?

This solution

(because there are likely others)

Users don't understand complex syntax, so this makes it easy to access deep dict or arrays with keys and numbers separated by a "." character.

A working demonstration

import dotnest

data = {
    'subdict': {
        'arrrr': ["there", "she", "blows",]
    },
    'list': [
        {'name': 'element1'}, {'name': 'element2'}
    ]
}

dn = dotnest.DotNest(data)
print(dn.get("subdict.arrrr.1"))       # she
print(dn.get("list.1.name"))           # element1

dn.set("subdict.newentry", "bar")
print(dn.get("subdict.newentry"))      # bar
print(dn.get("list.0"))                # {'name': 'element1'}

dn.get("does.not.exist")               # raises ValueError
dn.get("does.not.exist", 
       return_none=True)               # None

Used by

This class can be best married with tools that read in and need to referenc YAML or JSON as mentioned. As such, the original source of this effort comes from the package that converts from a netbox nestetd structure to ansible: nb2an.

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

dotnest-1.1.tar.gz (4.9 kB view hashes)

Uploaded Source

Built Distribution

dotnest-1.1-py3-none-any.whl (5.4 kB view hashes)

Uploaded Python 3

Supported by

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