Skip to main content

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.

from BooleanDictFilter import BooleanDictFilter

filter_str = "(anyof(role, ['admin', 'moderator']) and age >= 30)"
bf = BooleanDictFilter.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:

from BooleanDictFilter import BooleanDictFilter
filter_str = "(anyof(role, ['admin', 'moderator']) and age >= 30)"  
bf = BooleanDictFilter.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.

Metadata

Release files for booleanDictFilter 1.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for booleanDictFilter 1.0.2
File Size Uploaded
booleanDictFilter-1.0.2.tar.gz 4.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for booleanDictFilter 1.0.2
File Interpreter ABI Platform
booleanDictFilter-1.0.2-py3-none-any.whl Python 3 none any Details

Total release size: 10.1 kB

Release files / booleanDictFilter-1.0.2.tar.gz

Download URL booleanDictFilter-1.0.2.tar.gz
Size 4.9 kB
Tags Source
SHA-256 checksum
How to use checksums
4cda59feabbfc797f5f30b3b31248066275bffc0f75910b4d55ffe3889517947
BLAKE2b-256 checksum
How to use checksums
47af28b46a26985d761fdeb6bfa61b69971a9394763d96aa090a414a92131a87
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.4

Release files / booleanDictFilter-1.0.2-py3-none-any.whl

Download URL booleanDictFilter-1.0.2-py3-none-any.whl
Size 5.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3a6d360172e35796c15a334c4357c086582e50ff2d9ed54d0c8e1238b11f1731
BLAKE2b-256 checksum
How to use checksums
875cc9741f3439b14dbcfae8d6e4d15a19d129a86ebd20be56f3237a37eaacdc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.10.4

Release history Release notifications | RSS feed

This release

1.0.2 This release

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page