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 fields, you need to add these fields 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 fields, you need to add this fields 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.

none_values_filter(filtering_dict, filter_list, current_path="", recursive=True, is_reversed=False)

When you use this method, you specify which fields should be checked and depend on is_reversed, method will allow or deny None values for these fields.
If is_reversed=False, the method will remove None fields that present in the filter_list.
If is_reversed=True, the method will remove None fields that not present in the filter_list.
If recursive=True, the method will check all dictionary fields.
If recursive=False, the method will check only top-level dictionary fields.

For example, we have next dictionary:

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

If you want to deny (is_reversed=False) or allow (is_reversed=True) None value for some fields, you need to add this fields in the filter_list. Here cases filter string and examples:

  • Allow or deny top level fields: top_key_1, top_key_4 etc.
  • Allow or deny all top level fields: *
  • Allow or deny 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 or deny 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.*"], recursive=True, is_reversed=False you will get next result:

{
    "top_key_1": "top_value",
    "top_key_3": [
        {
            "middle_key_4": "middle_value"
        }
    ],
    "top_key_2": {
        "middle_key_3": None,
        "middle_key_1": "middle_value",
        "middle_key_4": {
            "low_level_key_1": "low_value"
        }
    }
}

Dots in the dict keys

There can also be a situation when the dict keys contain a "." symbol that obviously will confront the filter_list declaration.

If you need to identify such a field in the filter_list you need to wrap the "." into "()" -> "(.)".

For example we have such a dictionary:

{
    "top.key_1": "top_value",
    "top.key_2": [
        {
            "middle_key_1": "middle_value"
        }
    ],
    "top_key_3": {
        "middle.key_1": "middle_value",
        "middle_key_2": {
            "low_level_key_1": "low_value"
        },
        "middle.key_3": "middle_value"
    }
}

To identify middle_key_1 we need to use such filter config: ['top(.)key_2', 'top(.)key_2.middle_key_1']. But the '*' still works fine: ['*', '*.middle_key_1'].

Another example: ['*', '*.middle(.)key_1', '*.middle_key_2', '*.middle_key_2.low_level_key_1']

PAY ATTENTION! You need to use '(.)' only if you need to specify the exact field name that contains '.'.

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.7.tar.gz (4.0 kB view details)

Uploaded Source

Built Distribution

pydict_filter-0.1.7-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file pydict_filter-0.1.7.tar.gz.

File metadata

  • Download URL: pydict_filter-0.1.7.tar.gz
  • Upload date:
  • Size: 4.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.10.12 Linux/5.15.0-91-generic

File hashes

Hashes for pydict_filter-0.1.7.tar.gz
Algorithm Hash digest
SHA256 641c2baff998cf139f59ff3a9a7db0b83a2767735b12af5a1d8263ecf9d21c91
MD5 fc85b01b83c2a8637eb3c5c12b91c116
BLAKE2b-256 8ef7ba8d08f147fc686fa59cc1e68a1df4d0b52f4988d61716b3f35ee2bc9790

See more details on using hashes here.

File details

Details for the file pydict_filter-0.1.7-py3-none-any.whl.

File metadata

  • Download URL: pydict_filter-0.1.7-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.10.12 Linux/5.15.0-91-generic

File hashes

Hashes for pydict_filter-0.1.7-py3-none-any.whl
Algorithm Hash digest
SHA256 53bff3765fbc91d0377838a089e206eff39ebb848078a7f09bd38b5232448f5b
MD5 a64ee83aff8ac9669cf1f5de24508785
BLAKE2b-256 d2bad01b3293e9bbaf2712b5cda32aa18ae3ed188bc13495bde2d567eb7d7441

See more details on using hashes here.

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