Skip to main content

Filter dictionaries based on a list of field names.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dictfilter-2.1.tar.gz (12.4 kB view hashes)

Uploaded Source

Built Distribution

dictfilter-2.1-py3-none-any.whl (5.8 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page