Skip to main content

This package will help you to smart filter dictionary fields.

Project description

py-dict-filter

This package allowed you to perform smart filtering for your dictionary.

deny_filter(filtering_dict, filter_list, current_path="")

When you use this method, you specify which fields to filter out. For example, we have next dictionary:

{
    "top_key_1": "top_value",
    "top_key_2": {
        "middle_key_1": "middle_value",
        "middle_key_2": "middle_value",
        "middle_key_3": "middle_value"
    },
    "top_key_3": [
        {
            "middle_key_4": "middle_value",
            "middle_key_5": "middle_value"
        }
    ],
    "top_key_4": "top_value"
}

If you want to deny some filed, you need to add this filed in the filter_list. Here cases filter string and examples:

  • Deny top level fields: top_key_1, top_key_3 etc.
  • Deny all top level fields: *
  • Deny fields from nested dict: top_key_2.middle_key_2, top_key_3.middle_key_5
  • Deny all fields from nested dict: top_key_2.*, top_key_3.*

For example if you call this method with example dictionary as filtering_dict and filter_list=["top_key_4", "top_key_3.middle_key_4", "top_key_2.*"] you will get next result:

{
    "top_key_1": "top_value",
    "top_key_3": [
        {
            "middle_key_5": "middle_value"
        }
    ],
    "top_key_2": {}
}

allow_filter(filtering_dict, filter_list, current_path=""):

When you use this method, you specify which fields should be present in a dictionary. For example, we have next dictionary:

{
    "top_key_1": "top_value",
    "top_key_2": {
        "middle_key_1": "middle_value",
        "middle_key_2": "middle_value",
        "middle_key_3": "middle_value",
        "middle_key_4": {
            "low_level_key_1": "low_value",
            "low_level_key_2": "low_value"
        }
    },
    "top_key_3": [
        {
            "middle_key_4": "middle_value",
            "middle_key_5": "middle_value"
        }
    ],
    "top_key_4": "top_value"
}

If you want to deny some filed, you need to add this filed in the filter_list. Here cases filter string and examples:

  • Allow top level fields: top_key_1, top_key_3 etc.
  • Allow all top level fields: *
  • Allow fields from nested dict: top_key_2.middle_key_2, top_key_3.middle_key_5, top_key_2.middle_key_4.low_level_key_2
  • Allow all fields from nested dict: top_key_2.*, top_key_3.*, top_key_2.middle_key_4.*

For example if you call this method with example dictionary as filtering_dict and filter_list=["top_key_1", "top_key_2", "top_key_3", "top_key_3.*", "top_key_2.middle_key_1", "top_key_2.middle_key_2", "top_key_2.middle_key_4.*"] you will get next result:

{
    "top_key_1": "top_value",
    "top_key_3": [
        {
            "middle_key_4": "middle_value",
            "middle_key_5": "middle_value"
        }
    ],
    "top_key_2": {
        "middle_key_2": "middle_value",
        "middle_key_1": "middle_value"
    }
}

PAY ATTENTION! Despite the fact that all fields for top_key_2.middle_key_4 were allowed, they did not present in the result. This happened because top_key_2.middle_key_4 was not allowed in top_key_2 dictionary.

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

pydict-filter-0.1.1.tar.gz (2.8 kB view hashes)

Uploaded Source

Built Distribution

pydict_filter-0.1.1-py3-none-any.whl (2.6 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