Skip to main content

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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

booleanDictFilter-1.0.1.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

booleanDictFilter-1.0.1-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file booleanDictFilter-1.0.1.tar.gz.

File metadata

  • Download URL: booleanDictFilter-1.0.1.tar.gz
  • Upload date:
  • Size: 4.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.10.4

File hashes

Hashes for booleanDictFilter-1.0.1.tar.gz
Algorithm Hash digest
SHA256 f50db350e0c8d3cf88292dcd91e6890c2a8f5c18e2f994880f4cee91b99833b2
MD5 9888706180cde8bb3848c57a6e138112
BLAKE2b-256 c612a32eb37341722f51e2eef52eaab802745fa588437b38a1685281235bb542

See more details on using hashes here.

File details

Details for the file booleanDictFilter-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for booleanDictFilter-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fb5a46ec0bb0301a02af98a605a27647d9a09ba816e0a560e428dd4b8904b9d1
MD5 8ef1aa19b786ac479b63c51f91eb4ce5
BLAKE2b-256 8a60a3cc46e12fd1b706056fc8867f1b35ac6c3de0662b7f4facccc1ea545864

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page