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

69 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

trace-dkey

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

https://img.shields.io/pypi/v/trace-dkey https://pepy.tech/badge/trace-dkey https://coveralls.io/repos/github/Agent-Hellboy/trace-dkey/badge.svg?branch=main

Installation

For stable version
  • pip install trace-dkey
For development

Documentation

For more details, you can refer to the documentation.

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)

API Examples

Added example scripts demonstrating how to use trace-dkey with responses from popular APIs. These examples can be found in the examples folder:

  1. GitHub API Example (github_api_example.py): Demonstrates fetching repository information
  2. OpenWeatherMap API Example (openweathermap_api_example.py): Retrieves current weather data
  3. JSONPlaceholder API Example (jsonplaceholder_api_example.py): Shows how to interact with a mock REST API

To run these examples:

  1. Install the required dependencies: pip install -r examples/requirements.txt
  2. For the OpenWeatherMap example, you'll need to sign up for a free API key at https://openweathermap.org/ and set it as an environment variable: export OPENWEATHERMAP_API_KEY=your_api_key_here
  3. Run the examples: python examples/github_api_example.py python examples/openweathermap_api_example.py python examples/jsonplaceholder_api_example.py

These examples demonstrate how to use trace-dkey to find specific keys in nested JSON responses from real-world APIs.

Someone made a nice comparison 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.