Skip to main content

Rule Evaluator

Lightweight Python-based nested rule evaluator.

Installation:

pip install rule_evaluator

Usage:

import rule_evaluator as rl

Rule evaluation:

import rule_evaluator as rl
from collections import OrderedDict

# Define the rules
rules = [
    'R00351', # False
    'R01325+R01900,R01324', # True
    'R01899+R00268,R00267,R00709', # True
    'R00621+R03316,R01700', # True
    'R02570', # True
    'R07618', # True
    'R01197', # True
    'R00405,R00432,R00727,R10343', # False
    'R02164', # True
    'R01082', # True
    'R00342,R00361'# True
]

# List of elements to query
elements = {
    # "R00351", 
    # "R01325",
    # 'R01900',
    "R01324",
    'R00267',
    'R00342',
    'R00361',
    'R00621',
    'R03316',
    'R00709',
    # 'R00405',
    # "R01700",
    # 'R00727',
    # 'R00432',
    # 'R10343'
    'R01082',
    'R01197',
    'R01899',
    'R00268',
    'R02164',
    'R02570',
    'R07618',
}

rule_to_bool = OrderedDict()
for rule in rules:
    rule_to_bool[rule] = rl.Rule(rule).evaluate(elements)
rule_to_bool
# OrderedDict([('R00351', False),
#              ('R01325+R01900,R01324', True),
#              ('R01899+R00268,R00267,R00709', True),
#              ('R00621+R03316,R01700', True),
#              ('R02570', True),
#              ('R07618', True),
#              ('R01197', True),
#              ('R00405,R00432,R00727,R10343', False),
#              ('R02164', True),
#              ('R01082', True),
#              ('R00342,R00361', True)])

Definition evaluation:

import rule_evaluator as rl

# Define the nested rules
name="M00357"
definition = '((K00925 K00625),K01895) (K00193+K00197+K00194) (K00577+K00578+K00579+K00580+K00581-K00582-K00583+K00584) (K00399+K00401+K00402) (K22480+K22481+K22482,K03388+K03389+K03390,K08264+K08265,K03388+K03389+K03390+K14127+(K14126+K14128,K22516+K00125))'

# List of elements to check against
elements = {
    'K00925',
    # 'K00625',
    # 'K01895',
    'K00193',
    'K00197',
    'K00194',
    'K00577',
    'K00578',
    'K00579',
    'K00580',
    'K00581',
    'K00582',
    'K00584',
    'K00399',
    'K00401',
    'K00402',
    # 'K22480',
    # 'K22481',
    # 'K22482',
    # 'K03388',
    # 'K03389',
    # 'K03390',
    # 'K08264',
    # 'K08265',
    # 'K14127',
    # 'K14126',
    # 'K14128',
    # 'K22516',
    # 'K00125'
}

# Define
d = rl.Definition(definition, name=name, element_type="ko")

# View Defintion
d
# ========================================
# Definition(name:M00357, element_type:ko)
# ========================================
# Properties:
#     - number_of_elements: 30
#     - number_of_rules: 5
# Rules:
#     - 0: ((K00925 K00625),K01895)
#     - 1: (K00193+K00197+K00194)
#     - 2: (K00577+K00578+K00579+K00580+K00581-K00582-K00583+K00584)
#     - 3: (K00399+K00401+K00402)
#     - 4: (K22480+K22481+K22482,K03388+K03389+K03390,K08264+K08265,K03388+K03389+K03390+K14127+(K14126+K14128,K22516+K00125))

# View Rule
d.rules[2]
# =========================================================
# Rule(name:2, element_type:ko)
# =========================================================
# (K00577+K00578+K00579+K00580+K00581-K00582-K00583+K00584)
# _________________________________________________________
# Properties:
#     - number_of_elements: 8


# Evaluate
d.evaluate(elements)
# OrderedDict([('((K00925 K00625),K01895)', False),
#              ('(K00193+K00197+K00194)', True),
#              ('(K00577+K00578+K00579+K00580+K00581-K00582-K00583+K00584)',
#               True),
#              ('(K00399+K00401+K00402)', True),
#              ('(K22480+K22481+K22482,K03388+K03389+K03390,K08264+K08265,K03388+K03389+K03390+K14127+(K14126+K14128,K22516+K00125))',
#               False)])

# Score
d.evaluate(elements, score=True)
# 0.6 

Acknowledgements:

This package would not be possible without the grammar parsing help of @flakes.

Metadata

Release files for rule-evaluator 2024.8.14.post1

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

Source distribution (sdist)

Source distribution for rule-evaluator 2024.8.14.post1
File Size Uploaded Provenance Actions
rule_evaluator-2024.8.14.post1.tar.gz 19.2 kB Without provenance Details for rule_evaluator-2024.8.14.post1.tar.gz

Release files / rule_evaluator-2024.8.14.post1.tar.gz

Download URL rule_evaluator-2024.8.14.post1.tar.gz
Size 19.2 kB
Tags Source
SHA-256 checksum
How to use checksums
7e70854323cc41574461dac7ef45ec2a728045d74e6a2ee7776a32e26b2adfa8
BLAKE2b-256 checksum
How to use checksums
e24e5c2541f136250c51c1e2697470fed27feefbfea20bfa40eb5968bc97c550
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/5.0.0 CPython/3.12.3

Security for rule-evaluator 2024.8.14.post1

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.

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