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.

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.

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.2.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.2-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: booleanDictFilter-1.0.2.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.2.tar.gz
Algorithm Hash digest
SHA256 4cda59feabbfc797f5f30b3b31248066275bffc0f75910b4d55ffe3889517947
MD5 9f23c7a6eeb4738b8a8d80cb7e6ff2af
BLAKE2b-256 47af28b46a26985d761fdeb6bfa61b69971a9394763d96aa090a414a92131a87

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for booleanDictFilter-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 3a6d360172e35796c15a334c4357c086582e50ff2d9ed54d0c8e1238b11f1731
MD5 5b3103c22c6ccff70999efa412ce279b
BLAKE2b-256 875cc9741f3439b14dbcfae8d6e4d15a19d129a86ebd20be56f3237a37eaacdc

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