Skip to content

Filter python dictionaries based on a list of field names.

License

Notifications You must be signed in to change notification settings

cpressland/dictfilter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

19 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

dictfilter

installation

pip install dictfilter

usage

from dictfilter import query

bsg = {
    'class': 'Battlestar',
    'model': 'Jupiter',
    'name': 'Galactica',
    'crew': {
        'commander': 'William Adama',
        'xo': 'Saul Tigh',
        'cag': 'Kara Thrace',
    }
}

result = query(bsg, ['class', 'name', 'crew.commander'])

# {
#     'class': 'Battlestar',
#     'name': 'Galactica',
#     'crew': {
#         'commander': 'William Adama',
#     }
# }

The default delimiter used in field names is dot . however this can be changed with the delimiter keyword argument to query:

result = query(bsg, ['class', 'name', 'crew > commander'], delimiter=' > ')

# {
#     'class': 'Battlestar',
#     'name': 'Galactica',
#     'crew': {
#         'commander': 'William Adama',
#     }
# }

django integration

Register the dictfilter middleware in settings.py:

MIDDLEWARE = [
    ...
    'dictfilter.django.middleware.DictFilterMiddleware',
]

By default, every 2xx series response will be filtered based on a comma-separated fields parameter in the query string.

About

Filter python dictionaries based on a list of field names.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages