ℕ𝕖𝕤𝕥𝕖𝕕𝔽𝕖𝕥𝕔𝕙
Outline
Overview
- NestedFetch provides syntactic sugar 🍬 to deal with a nested python
dictionaryor a nestedlist🐍 - You can
get,set,updateandflattenvalues from a deeply nested dictionary or a list with a more concise, easier and aKeyError,IndexErrorfree way 😌
data = {
"league": "Champions League",
"matches": [
{
"match_id": "match_1",
"goals": [
{
"time": 13,
"scorrer": "Lionel Messi",
"assist": "Luis Suarez"
},
{
"time": 78,
"scorrer": "Luis Suarez",
"assist": "Ivan Rakitic"
}]
},
{
"match_id": "match_2",
"goals": [
{
"time": 36,
"scorrer": "C. Ronaldo",
"assist": "Luka Modric"
}]
}]
}
Installation
NestedFetch works with Python3.
You can directly install it via pip
$ pip3 install nestedfetch
Usage
Import the methods from the package.
from nestedfetch import nested_get, nested_set
No need to instantiate any object, just use the methods specifying valid parameters.
Examples
Fetch Data
nested_get(data, keys, default=None, flatten=False)
@Arguments
data : dict / list
keys => List of sequential keys leading to the desired value to fetch
default => Specifies the default value to be returned if any specified key is not present. If not specified, it will be None
flatten => Specifies whether to flatten the returned value
@Return
Returns the fetched value if it exists, or returns specified default value
- Fetch simple nested data :
data = {
'name': 'Jesse Pinkman',
'details': {
'address':{
'city': 'Albuquerque'
}
}
}
res = nested_get(data,['details','address','city'])
# res = Albuquerque
- Fetch simple nested data with
defaultvalue:
data = {
'name': 'Jesse Pinkman',
'details': {
'address':{
'city': 'Albuquerque'
}
}
}
res = nested_get(data,['details','address','state'], default=-1)
# res = -1
- Fetch nested data:
data = {
'name': 'Jesse Pinkman',
'details': {
'address':[{
'city': 'Albuquerque'
},{
'city': 'El Paso'
}]
}
}
res = nested_get(data,['details','address','city'])
# res = ['Albuquerque','El Paso']
- Fetch nested data with
defaultvalue:
data = {
'name': 'Jesse Pinkman',
'details': {
'address':[{
'city': 'Albuquerque'
},{
'city': 'El Paso'
},{
'state': 'New Mexico'
}]
}
}
res = nested_get(data,['details','address','city'], default= None)
# res = ['Albuquerque','El Paso', None]
- Fetch nested data by specifing
index:
data = {
'name': 'Walter White',
'details': {
'address':[{
'city': 'Albuquerque'
},{
'city': 'El Paso'
}]
}
}
res = nested_get(data,['details','address','city', 0])
# res = Albuquerque
- Fetch nested data without
flatten:
data = {
"league": "Champions League",
"matches": [
{
"match_id": "match_1",
"goals": [
{
"time": 13,
"scorrer": "Lionel Messi",
"assist": "Luis Suarez"
},
{
"time": 78,
"scorrer": "Luis Suarez",
"assist": "Ivan Rakitic"
}]
},
{
"match_id": "match_2",
"goals": [
{
"time": 36,
"scorrer": "C. Ronaldo",
"assist": "Luka Modric"
}]
}]
}
res = nested_get(data,['matches','goals','scorrer'])
# res = [['Lionel Messi', 'Luis Suarez'], ['C. Ronaldo']]
- Fetch nested data with
flatten:
data = {
"league": "Champions League",
"matches": [
{
"match_id": "match_1",
"goals": [
{
"time": 13,
"scorrer": "Lionel Messi",
"assist": "Luis Suarez"
},
{
"time": 78,
"scorrer": "Luis Suarez",
"assist": "Ivan Rakitic"
}]
},
{
"match_id": "match_2",
"goals": [
{
"time": 36,
"scorrer": "C. Ronaldo",
"assist": "Luka Modric"
}]
}]
}
res = nested_get(data,['matches','goals','scorrer'], flatten=True)
# res = ['Lionel Messi', 'Luis Suarez', 'C. Ronaldo']
Set / Update Data
nested_set(data, keys, value, create_missing=False):
@Arguments
data => dict / list
keys => List of sequential keys leading to the desired value to set / update
value => Specifies the value to set / update
create_missing => Specifies whether to create new key while building up if the specified key does not exists
@Return
Returns the number of values updated
- Update value of simple nested data :
data = {
'name': 'Jesse Pinkman',
'details': {
'address':{
'city': 'Albuquerque'
}
}
}
res = nested_set(data,['details','address','city'], "Denver")
# res = 1
# data = {
# 'name': 'Jesse Pinkman',
# 'details': {
# 'address':{
# 'city': 'Denver'
# }
# }
# }
- Update nested data:
data = {
'name': 'Jesse Pinkman',
'details': {
'address':[{
'city': 'Albuquerque'
},{
'city': 'El Paso'
}]
}
}
res = nested_set(data,['details','address','city'], "Denver")
# res = 2
# data = {
# 'name': 'Jesse Pinkman',
# 'details': {
# 'address':[{
# 'city': 'Denver'
# },{
# 'city': 'Denver'
# }]
# }
# }
- Update nested data with
index:
data = {
'name': 'Jesse Pinkman',
'details': {
'address':[{
'city': 'Albuquerque'
},{
'city': 'El Paso'
}]
}
}
res = nested_set(data,['details','address',0,'city'], "Denver")
# res = 1
# data = {
# 'name': 'Jesse Pinkman',
# 'details': {
# 'address':[{
# 'city': 'Denver'
# },{
# 'city': 'El Paso'
# }]
# }
# }
- Set nested data with
create_missing:
data = {
'name': 'Jesse Pinkman',
'details': {
'address':{
'city': 'Albuquerque'
}
}
}
res = nested_set(data,['details','address','state'], "New Mexico", create_missing=True)
# res = 1
# data = {
# 'name': 'Jesse Pinkman',
# 'details': {
# 'address':{
# 'city': 'Denver',
# 'state': 'New Mexico'
# }
# }
# }
Flatten Nested Lists
flatten_data(data):
@Arguments
data => list of list
@Return
Returns the flattened list
- Flatten List of Lists
data = [[
['This','is'],
['flattened', 'data']
]]
res = flatten_data(data)
# 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 📝.
Metadata
Release files for nestedfetch 0.1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| nestedfetch-0.1.2.tar.gz | 4.9 kB | Without provenance | Details for nestedfetch-0.1.2.tar.gz |
Release files / nestedfetch-0.1.2.tar.gz
| Download URL | nestedfetch-0.1.2.tar.gz |
|---|---|
| Size | 4.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
19c4b16602965f57f27231350ea3f26822621e8d75a7dcfe1dcedc59666bab7c
|
|
BLAKE2b-256 checksum How to use checksums |
74cc5b1db68f0d012401e01546c61d29b0638ca40133bec0b7109fda2f998af6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/2.0.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.4.0 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.6.8
|
Security for nestedfetch 0.1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.