项目作者: saintlyzero

项目描述 :
Syntactic sugar inspired by XPath to GET, SET, UPDATE and FLATTEN values from nested dictionaries and nested lists.
高级语言: Python
项目地址: git://github.com/saintlyzero/NestedFetch.git
创建时间: 2019-10-15T19:36:00Z
项目社区:https://github.com/saintlyzero/NestedFetch

开源协议:MIT License

下载


ℕ𝕖𝕤𝕥𝕖𝕕𝔽𝕖𝕥𝕔𝕙

Build Status GitHub PyPI - Python Version

Outline

  1. Overview
  2. Installation
  3. Usage
  4. Examples
    1. Fetch Value
    2. Set Value
    3. Flatten Nested Lists
  5. How to Contribute

Overview

  • NestedFetch provides syntactic sugar 🍬 inspired by XPath to deal with a nested python dictionary or a nested list 🐍
  • You can get, set, update and flatten values from a deeply nested dictionary or a list with a more concise, easier and a KeyError, IndexError free way 😌
  1. data = {
  2. "league": "Champions League",
  3. "matches": [
  4. {
  5. "match_id": "match_1",
  6. "goals": [
  7. {
  8. "time": 13,
  9. "scorrer": "Lionel Messi",
  10. "assist": "Luis Suarez"
  11. },
  12. {
  13. "time": 78,
  14. "scorrer": "Luis Suarez",
  15. "assist": "Ivan Rakitic"
  16. }]
  17. },
  18. {
  19. "match_id": "match_2",
  20. "goals": [
  21. {
  22. "time": 36,
  23. "scorrer": "C. Ronaldo",
  24. "assist": "Luka Modric"
  25. }]
  26. }]
  27. }
No Face normal code
Yes Face NestedFetch code

Installation

NestedFetch works with Python3.
You can directly install it via pip

  1. $ pip3 install nestedfetch

Usage

Import the methods from the package.

  1. from nestedfetch import nested_get, nested_set, flatten_data

No need to instantiate any object, just use the methods specifying valid parameters.

Examples

Fetch Data

  1. nested_get(data, keys, default=None, flatten=False)
  2. @Arguments
  3. data : dict / list
  4. keys => List of sequential keys leading to the desired value to fetch
  5. default => Specifies the default value to be returned if any specified key is not present. If not specified, it will be None
  6. flatten => Specifies whether to flatten the returned value
  7. @Return
  8. Returns the fetched value if it exists, or returns specified default value
  • Fetch simple nested data :
  1. data = {
  2. 'name': 'Jesse Pinkman',
  3. 'details': {
  4. 'address':{
  5. 'city': 'Albuquerque'
  6. }
  7. }
  8. }
  9. res = nested_get(data,['details','address','city'])
  10. # res = Albuquerque
  • Fetch simple nested data with default value:
  1. data = {
  2. 'name': 'Jesse Pinkman',
  3. 'details': {
  4. 'address':{
  5. 'city': 'Albuquerque'
  6. }
  7. }
  8. }
  9. res = nested_get(data,['details','address','state'], default=-1)
  10. # res = -1
  • Fetch nested data:
  1. data = {
  2. 'name': 'Jesse Pinkman',
  3. 'details': {
  4. 'address':[{
  5. 'city': 'Albuquerque'
  6. },{
  7. 'city': 'El Paso'
  8. }]
  9. }
  10. }
  11. res = nested_get(data,['details','address','city'])
  12. # res = ['Albuquerque','El Paso']
  • Fetch nested data with default value:
  1. data = {
  2. 'name': 'Jesse Pinkman',
  3. 'details': {
  4. 'address':[{
  5. 'city': 'Albuquerque'
  6. },{
  7. 'city': 'El Paso'
  8. },{
  9. 'state': 'New Mexico'
  10. }]
  11. }
  12. }
  13. res = nested_get(data,['details','address','city'], default= None)
  14. # res = ['Albuquerque','El Paso', None]
  • Fetch nested data by specifing index:
  1. data = {
  2. 'name': 'Walter White',
  3. 'details': {
  4. 'address':[{
  5. 'city': 'Albuquerque'
  6. },{
  7. 'city': 'El Paso'
  8. }]
  9. }
  10. }
  11. res = nested_get(data,['details','address','city', 0])
  12. # res = Albuquerque
  • Fetch nested data without flatten:
  1. data = {
  2. "league": "Champions League",
  3. "matches": [
  4. {
  5. "match_id": "match_1",
  6. "goals": [
  7. {
  8. "time": 13,
  9. "scorrer": "Lionel Messi",
  10. "assist": "Luis Suarez"
  11. },
  12. {
  13. "time": 78,
  14. "scorrer": "Luis Suarez",
  15. "assist": "Ivan Rakitic"
  16. }]
  17. },
  18. {
  19. "match_id": "match_2",
  20. "goals": [
  21. {
  22. "time": 36,
  23. "scorrer": "C. Ronaldo",
  24. "assist": "Luka Modric"
  25. }]
  26. }]
  27. }
  28. res = nested_get(data,['matches','goals','scorrer'])
  29. # res = [['Lionel Messi', 'Luis Suarez'], ['C. Ronaldo']]
  • Fetch nested data with flatten:
  1. data = {
  2. "league": "Champions League",
  3. "matches": [
  4. {
  5. "match_id": "match_1",
  6. "goals": [
  7. {
  8. "time": 13,
  9. "scorrer": "Lionel Messi",
  10. "assist": "Luis Suarez"
  11. },
  12. {
  13. "time": 78,
  14. "scorrer": "Luis Suarez",
  15. "assist": "Ivan Rakitic"
  16. }]
  17. },
  18. {
  19. "match_id": "match_2",
  20. "goals": [
  21. {
  22. "time": 36,
  23. "scorrer": "C. Ronaldo",
  24. "assist": "Luka Modric"
  25. }]
  26. }]
  27. }
  28. res = nested_get(data,['matches','goals','scorrer'], flatten=True)
  29. # res = ['Lionel Messi', 'Luis Suarez', 'C. Ronaldo']

Set / Update Data

  1. nested_set(data, keys, value, create_missing=False):
  2. @Arguments
  3. data => dict / list
  4. keys => List of sequential keys leading to the desired value to set / update
  5. value => Specifies the value to set / update
  6. create_missing => Specifies whether to create new key while building up if the specified key does not exists
  7. @Return
  8. Returns the number of values updated
  • Update value of simple nested data :
  1. data = {
  2. 'name': 'Jesse Pinkman',
  3. 'details': {
  4. 'address':{
  5. 'city': 'Albuquerque'
  6. }
  7. }
  8. }
  9. res = nested_set(data,['details','address','city'], "Denver")
  10. # res = 1
  11. # data = {
  12. # 'name': 'Jesse Pinkman',
  13. # 'details': {
  14. # 'address':{
  15. # 'city': 'Denver'
  16. # }
  17. # }
  18. # }
  • Update nested data:
  1. data = {
  2. 'name': 'Jesse Pinkman',
  3. 'details': {
  4. 'address':[{
  5. 'city': 'Albuquerque'
  6. },{
  7. 'city': 'El Paso'
  8. }]
  9. }
  10. }
  11. res = nested_set(data,['details','address','city'], "Denver")
  12. # res = 2
  13. # data = {
  14. # 'name': 'Jesse Pinkman',
  15. # 'details': {
  16. # 'address':[{
  17. # 'city': 'Denver'
  18. # },{
  19. # 'city': 'Denver'
  20. # }]
  21. # }
  22. # }
  • Update nested data with index:
  1. data = {
  2. 'name': 'Jesse Pinkman',
  3. 'details': {
  4. 'address':[{
  5. 'city': 'Albuquerque'
  6. },{
  7. 'city': 'El Paso'
  8. }]
  9. }
  10. }
  11. res = nested_set(data,['details','address',0,'city'], "Denver")
  12. # res = 1
  13. # data = {
  14. # 'name': 'Jesse Pinkman',
  15. # 'details': {
  16. # 'address':[{
  17. # 'city': 'Denver'
  18. # },{
  19. # 'city': 'El Paso'
  20. # }]
  21. # }
  22. # }
  • Set nested data with create_missing :
  1. data = {
  2. 'name': 'Jesse Pinkman',
  3. 'details': {
  4. 'address':{
  5. 'city': 'Albuquerque'
  6. }
  7. }
  8. }
  9. res = nested_set(data,['details','address','state'], "New Mexico", create_missing=True)
  10. # res = 1
  11. # data = {
  12. # 'name': 'Jesse Pinkman',
  13. # 'details': {
  14. # 'address':{
  15. # 'city': 'Denver',
  16. # 'state': 'New Mexico'
  17. # }
  18. # }
  19. # }

Flatten Nested Lists

  1. flatten_data(data):
  2. @Arguments
  3. data => list of list
  4. @Return
  5. Returns the flattened list
  • Flatten List of Lists
  1. data = [[
  2. ['This','is'],
  3. ['flattened', 'data']
  4. ]]
  5. res = flatten_data(data)
  6. # res = ['This','is','flattened','data']

How to contribute

Contributions are welcome 😇.
Feel free to submit a patch, report a bug 🐛 or ask for a feature 🐣.
Please open an issue first to encourage and keep track of potential discussions 📝.