Skip to content

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

License

Notifications You must be signed in to change notification settings

Agent-Hellboy/trace-dkey

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

51 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

trace-dkey

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

image

image

image

image

image

image

image

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

image

Contributing

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