Library for applying a boolean filter string to a dictionary, allowing for searches of keyvalue pairs.
Project description
BooleanDictFilter
Sometimes it is necessary to be able to make a filter which is a string, that evaluates the contents of a dictionary object. Sure we could write that in Python and use eval, but that would be considered a security risk. Instead we use a conditional boolean string, which can handle the comparisons we need.
Operands
| Operand | Description | example |
|---|---|---|
| and | logical and | (condition) and (condition) |
| or | logical or | (condition) or (condition) |
| not | logical not | not (condition) |
| >= | key is greater then or equal to value | key >= integer |
| <= | key is less then or equal to value | key <= integer |
| > | key is greater then value | key > integer |
| < | key less then value | key <= integer |
| == | key equals value | key == integer , key == string |
| contains | key contains string | key contains string |
| anyof | any of the defined keys are in dict | anyof(key, [keyname1, keyname2]) |
| noneof | none of the defined keys are in dict | noneof(key, [keyname1, keyname2]) |
Features
Other then typical boolean operations and evaluations. I added the following evaluators to the flow, to make things a bit simpler.
- contains if the value of key is a string, and contains the string specified, this returns true. If key is not a string this returns false.
- anyof if the value of key is any of one the values specified in the list, then this returns true. Useful to remove a bunch of OR statements.
- noneof if the value of key is not in the list of values specified, then this returns true.
If any of the key does not exist in the dictionary, then result is false. However this is dependent on the key being reached. For instance:
(name == "John") or (nonexistentkey == "X")
Would be true, if the key "name" was "John" in the given dictionary.
Installation
To install, run:
pip install booleanDictFilter
Examples
Allows for testing a single, or group of dictionaries against a text defined filter.
import BooleanDictFilter from BooleanDictFilter
filter_str = "(anyof(role, ['admin', 'moderator']) and age >= 30)"
bf = BooleanDictFilter(filter_str)
print (bf.evaluate({"role": "admin", "age": 35, "status": "active", "name": "John"})) # True
print (bf.evaluate({"role": "user", "age":34, "status": "active", "name": "June"})) # False
The same filter can be used to evaluate a list of dictionaries, returning only those dictionaries which passed the evaluation:
import BooleanDictFilter from BooleanDictFilter
filter_str = "(anyof(role, ['admin', 'moderator']) and age >= 30)"
bf = BooleanDictFilter(filter_str)
data_list = [
{"role": "admin", "age": 35, "status": "active", "name": "John"},
{"role": "user", "age":34, "status": "active", "name": "June"}
]
filtered_data = bf.filter_dicts(data_list)
for item in filtered_data:
print(item)
Result:
[
{"role": "admin", "age": 35, "status": "active", "name": "John"}
]
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file booleanDictFilter-1.0.0.tar.gz.
File metadata
- Download URL: booleanDictFilter-1.0.0.tar.gz
- Upload date:
- Size: 3.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b7fdec981f180c6e13be277b46fb894cedc0ac91f4883f2f529fa7c8ed876aa9
|
|
| MD5 |
d6bb49dad0c1f51c097d66ebeda3d63e
|
|
| BLAKE2b-256 |
2a89b6087e552aa3fabb4a036afef3efc47f4719bc3bc0955d0984cef06bac91
|
File details
Details for the file booleanDictFilter-1.0.0-py3-none-any.whl.
File metadata
- Download URL: booleanDictFilter-1.0.0-py3-none-any.whl
- Upload date:
- Size: 3.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
06c7435800f7e9b55238239d1d0bc89e31b9309a708928a330f64d1f1e6177ef
|
|
| MD5 |
d442f85a21f1292f5a46025780d290fb
|
|
| BLAKE2b-256 |
8f1d4ae604b3be0a42ba15d86c0dd61760ee6bc5e3de390876117a3eef35dd6d
|