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_3etc. - 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_3etc. - 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_4etc. - 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 '.'.
Metadata
Release files for pydict-filter 0.1.7
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 | |
|---|---|---|---|---|
| pydict_filter-0.1.7.tar.gz | 4.0 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pydict_filter-0.1.7-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 7.7 kB
Release files / pydict_filter-0.1.7.tar.gz
| Download URL | pydict_filter-0.1.7.tar.gz |
|---|---|
| Size | 4.0 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
641c2baff998cf139f59ff3a9a7db0b83a2767735b12af5a1d8263ecf9d21c91
|
|
BLAKE2b-256 checksum How to use checksums |
8ef7ba8d08f147fc686fa59cc1e68a1df4d0b52f4988d61716b3f35ee2bc9790
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.2.2 CPython/3.10.12 Linux/5.15.0-91-generic
|
Release files / pydict_filter-0.1.7-py3-none-any.whl
| Download URL | pydict_filter-0.1.7-py3-none-any.whl |
|---|---|
| Size | 3.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
53bff3765fbc91d0377838a089e206eff39ebb848078a7f09bd38b5232448f5b
|
|
BLAKE2b-256 checksum How to use checksums |
d2bad01b3293e9bbaf2712b5cda32aa18ae3ed188bc13495bde2d567eb7d7441
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/1.2.2 CPython/3.10.12 Linux/5.15.0-91-generic
|
Security for pydict-filter 0.1.7
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.