Skip to main content

Python library to trace path of a particular key inside a nested dict

Project description

trace-dkey

Python library to trace path of a particular key inside a nested dict

https://img.shields.io/pypi/v/trace-dkey https://github.com/Agent-Hellboy/trace-dkey/actions/workflows/python-app.yml/badge.svg https://img.shields.io/pypi/pyversions/trace-dkey.svg https://img.shields.io/pypi/l/trace-dkey.svg https://pepy.tech/badge/trace-dkey https://img.shields.io/pypi/format/trace-dkey.svg https://coveralls.io/repos/github/Agent-Hellboy/trace-dkey/badge.svg?branch=main

Installation

For stable version
  • pip install trace-dkey

For developement

Example

you can use the library as python3 -m trace_dkey --file=test.json --key=name, test.json is

a json file containing name as key

>>> from trace_dkey import trace
>>> l={'a':{'b':{'c':{'d':{'e':{'f':1}}}}}}
>>> print(trace(l,'f'))
[['a', 'b', 'c', 'd', 'e', 'f']]

Now you can query it as l['a']['b']['c']['d']['e']['f']

>>> l['a']['b']['c']['d']['e']['f']
1

refer Tests_. for other examples

General Info

  • The value returned by the trace function is an array of paths, where each path is an array of dictionary keys.

  • Because of that, the library can be used in a practical way by taking advantage of this format.

  • In the example below we use the returned path to iterate over the dictionary keys and print the key value:

from trace_dkey import trace
l={'a':{'b':{'c':{'d':{'e':{'f':1}}}}}}

paths = trace(l,'f')

for path in paths:
   dic = l
   for key in path:
      dic = dic[key]
   print(dic)
Someone made a nice comparision of this lib(trace-dkey) with one of the famous lib(yamlpath) which is doing the similar thing
/images/img.png

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

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

trace-dkey-0.0.6.tar.gz (5.0 kB view hashes)

Uploaded Source

Built Distribution

trace_dkey-0.0.6-py3-none-any.whl (4.6 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