Skip to main content

A config util package to flatten-reduce long keys

Project description

Config (YAML/JSON) Keys Flatten-Reducer

conduce - A simple config package which flatten-reduces all keys of either a YAML or JSON config to allow a simple way to get values from long keys.

Installation

pip install conduce


some.yaml

alpha:
    beta:
        gamma: "hello"


some.json:

{"alpha": {"beta": {"gamma": "world"}}}


some_deep_nested.yaml:

alpha:
    beta:
        gamma:
            delta:
                theta:
                    phi:
                        "finally!!!"
    hello:
        - 1
        - 2
        - 3
        - rho:
            - 10
            - 100
            - fellow: "end of the road"


example.py reading YAML/JSON into ConfigReader:

from conduce import conduce
# read config with nstruct=False as default for ConfigReader conversion
c1 = conduce.read_yaml(config_name="some.yaml", root_path="some/path/to/the/yaml")
c2 = conduce.read_json(config_name="some.json", root_path="some/path/to/the/json")
c3 = conduce.read_yaml(config_name="some_deep_nested.yaml", root_path="some/path/to/the/yaml")
c4 = conduce.read_config(config_type="json", config_name="some.json", root_path="some/path/to/the/yaml")

# get values
c1('alpha.beta.gamma') # returns value "hello"
c2('alpha.beta.gamma') # returns value "world"
c3('alpha.beta.gamma.delta.theta.phi') # returns value "finally!!!"
c4('alpha.beta.gamma') # returns value "world" (same as c2)
c4() # empty key returns full config dictionary of some.json


example2.py reading YAML/JSON into NStruct:

from conduce import conduce
# NStruct (class object) example
c5 = conduce.read_yaml(config_name="some_deep_nested.yaml", 
                       root_path="some/path/to/the/yaml",
                       nstruct=True)  # set nstruct=True for NStruct
                                      # set nstruct=False for ConfigReader

c5.alpha.hello[3].rho[2].fellow # returns value "end of the road"
c5.value() # returns the full config dictionary
c5.alpha.hello[3].value() # returns the full dictionary of "rho"


example3.py converting dictionary into NStruct:

from conduce import conduce
ex_dict = {"a": 1, "b": {"c": [1, 2, {"d": 3}]}, "d": []}  # example dic
ex_obj = conduce.NStruct(**ex_dict)  # convert dict to NStruct
print(ex_obj.b.c[2].d)  # returns 3
print(ex_obj.value())  # returns whole ex_dict dictionary


example4.py converting dictionary into ConfigReader:

ex_dict = {"a": 1, "b": {"c": [1, 2, {"d": 3}]}, "d": []}  # example dic
ex_obj = conduce.ConfigReader(ex_dict).get  # convert dict to ConfigReader
print(ex_obj("b.c")[2]["d"])  # returns 3
print(ex_obj())  # returns whole ex_dict dictionary


Function signatures:

# read config (general)
def read_config(
        config_type: str,
        config_name: str,
        root_path="",
        nstruct=False
):
    """
    general config reader
    :param config_type: json or yaml
    :param config_name: yaml/json file name
    :param root_path: yaml/json file path
    :param nstruct: True if return obj is NStruct otherwise is ConfigReader
    :return: ConfigReader object containing config
    """

# read config (yaml)
def read_yaml(
        config_name: str,
        root_path="",
        nstruct=False
):
    """
    read yaml config into ConfigReader object
    :param config_name: yaml file name
    :param root_path: yaml file path
    :param nstruct: True if return obj is NStruct otherwise is ConfigReader
    :return: ConfigReader object
    """

# read config (json)
def read_json(
        config_name: str,
        root_path="",
        nstruct=False
):
    """
    read json config into ConfigReader object
    :param config_name: json file name
    :param root_path: json file path
    :param nstruct: True if return obj is NStruct otherwise is ConfigReader
    :return: ConfigReader object
    """

# reduce dic calls to a single string with '.' delimitation  (ConfigReader)
def reduce_get(cfg: dict, key: str):
    """
    gets value from dictionary based on flat 'key' provided
    e.g. instead of dict1["alpha"]["beta"]["gamma"], we do: key = "alpha.beta.gamma"
    :param cfg: config dictionary
    :param key: flat key e.g. "alpha.beta.gamma"
    :return: value from dictionary
    """

def traverse_nstruct(dic: dict) -> dict:
    """
    traverses through list converting NStruct to dictionaries
    :param dic: input list
    :return: returns transformed list where dics are converted to NStruct
    """

def traverse_list(lss: list, obj_type) -> list:
    """
    traverses through list converting dictionaries to NStruct
    :param lss: input list
    :param obj_type: either NStruct or dict
    :return: returns transformed list where dics are converted to NStruct
    """

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

conduce-1.1.5.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

conduce-1.1.5-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file conduce-1.1.5.tar.gz.

File metadata

  • Download URL: conduce-1.1.5.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.6

File hashes

Hashes for conduce-1.1.5.tar.gz
Algorithm Hash digest
SHA256 a7698b5ce8c90b7529883006bab746d19d66e37880c7b05782492ee082d0908d
MD5 95bbffe38a1a51da093e3aa723ebd6a3
BLAKE2b-256 d56d8d5161bdc19cd23d4048636727cb60ead393a0d20ce69fe17f75494d03a8

See more details on using hashes here.

File details

Details for the file conduce-1.1.5-py3-none-any.whl.

File metadata

  • Download URL: conduce-1.1.5-py3-none-any.whl
  • Upload date:
  • Size: 5.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.9.6

File hashes

Hashes for conduce-1.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 2651695d2be61af02cc0a87338fc74394686cf98e50b11baf52eb2c38ae223c8
MD5 2361318ec3179eb5b9403b988bbbc073
BLAKE2b-256 f344cfdd26c3eff4f37a37d5c1f52ace6bf0f7b13dbacefdf09d5d05109ed7c1

See more details on using hashes here.

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