Skip to main content

unirules is a Python library for building declarative rule sets with a friendly DSL over discrete and interval domains, then turning them into runtime resolvers and analyzers that fit into your applications.

Project description

unirules

unirules is a Python library for building declarative rule sets with a friendly DSL over discrete and interval domains, then turning them into runtime resolvers and analyzers that fit into your applications.

Installation

pip install unirules

Features

  • Resolver with explanations. Rule sets compile down to a resolver that respects first-wins and priority policies, evaluates contexts, and reports the matched path alongside every tested rule.
  • Symbolic analyzer. Inspect coverage across discrete and interval domains, with per-rule value sets and uncovered segments that honor nested structures and optional context filters.
  • Expressive DSL. Declare fields with equality, membership, and numeric operators, then combine when, ruleset, and otherwise clauses—with optional names and priorities—to build nested decision trees.
  • Dynamic loading. Execute DSL snippets at runtime with load_ruleset_from_code, which preloads the necessary helpers so rule sets can live in configuration files or databases.

Usage

Build and resolve a ruleset

Use the DSL to declare fields, nest rulesets, resolve a context, and inspect the resolver’s explanation of what matched.

from unirules import DiscreteDomain, IntervalDomain, field, otherwise, ruleset, when

credit_score = field("credit_score", IntervalDomain(300, 850))
income_level = field("income_level", DiscreteDomain({"LOW", "HIGH"}))

loan_rules = ruleset(
    when(income_level == "HIGH", name="High income").then(
        ruleset(
            when(credit_score.between(700, 850), name="Top tier").then(
                {"decision": "APPROVE", "rate": 3.5}
            ),
            otherwise({"decision": "REVIEW", "rate": 7.0}, name="High fallback"),
        )
    ),
    when(credit_score < 500, name="Low credit reject").then({"decision": "REJECT", "rate": None}),
    otherwise({"decision": "REVIEW", "rate": None}, name="Default review"),
)

resolver = loan_rules.to_resolver()

decision = resolver.resolve({"income_level": "HIGH", "credit_score": 720})
explanation = resolver.explain({"income_level": "HIGH", "credit_score": 720})
print(decision)          # {'decision': 'APPROVE', 'rate': 3.5}
print(explanation.path)  # ['High income', 'Top tier']

Analyze coverage

Analyze coverage for a field while applying optional context filters; the analyzer reports covered values per rule along with any remaining uncovered portion of the domain.

analyzer = loan_rules.to_analyzer()

analysis = analyzer.analyze(target=income_level, ctx={"credit_score": 720})
print(analysis.covered_values())   # {'LOW', 'HIGH'}
print(analysis.uncovered_values()) # set()
print(analysis.by_rule)            # [((0, 0), {'HIGH'}, {...}), ((2,), {'LOW'}, {...})]

Load rulesets from code

Load a ruleset from a code string—complete with DSL helpers already in scope—and use it like any other ruleset.

from unirules import load_ruleset_from_code

rules_source = """
segment = field("segment", DiscreteDomain({"core", "edge"}))

RULESET = ruleset(
    when(segment == "core", name="Core branch", priority=5).then("CORE"),
    otherwise("EDGE", name="Fallback"),
    policy="priority",
)
"""

loaded_rules = load_ruleset_from_code(rules_source)
print(loaded_rules.to_resolver().resolve({"segment": "edge"}))  # EDGE

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

unirules-0.1.4.tar.gz (18.9 kB view details)

Uploaded Source

Built Distribution

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

unirules-0.1.4-py3-none-any.whl (30.3 kB view details)

Uploaded Python 3

File details

Details for the file unirules-0.1.4.tar.gz.

File metadata

  • Download URL: unirules-0.1.4.tar.gz
  • Upload date:
  • Size: 18.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.7

File hashes

Hashes for unirules-0.1.4.tar.gz
Algorithm Hash digest
SHA256 50fa7dfa01ba1cc0ef5b5a29ecea0dbdea19dda8413733a2750d4d3263651568
MD5 da89cd4aaf6fd02a1f5d26df755609be
BLAKE2b-256 e653a81936aae2391cbc367f4afdc72c76411afb68021ee8c61e8925f695fa4b

See more details on using hashes here.

File details

Details for the file unirules-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: unirules-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 30.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.7

File hashes

Hashes for unirules-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 0819f3d76badeaf4dc40580ab299848a1d5d97c3dbce0cf2332bcdfae67ff89d
MD5 0ba48f73daad0d8e6aaa961b64a5bc72
BLAKE2b-256 41f9fff8eeb76f38d66bcd499e0ceae8f849989aeebcb819ab64f2c5150f25b1

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