A lightweight, chainable, and prioritized rule engine for Python that makes business logic management simple and elegant.
pip install rulifyfrom rulify import Rule, RuleEngine
def is_hot(ctx):
return ctx["temp"] > 30
def turn_on_ac(ctx):
ctx["ac"] = True
print("AC turned ON")
return ctx
# Create engine and add rules
engine = RuleEngine()
engine.add_rule(Rule("Hot Check", is_hot, turn_on_ac, priority=10))
# Run with context
context = {"temp": 35}
result = engine.run(context)
print(result) # {'temp': 35, 'ac': True}- π― Rule Prioritization: Execute rules in order of importance
- π Rule Chaining: Chain rules together for complex workflows
- β‘ Lightweight: Zero external dependencies
- π Pythonic: Clean, intuitive API design
- π§ Flexible: Works with any Python data structure
from rulify import Rule, RuleEngine
# Define conditions and actions
def is_high_temp(ctx):
return ctx.get("temp", 0) > 30
def cool_down(ctx):
ctx["ac"] = True
ctx["fan_speed"] = "high"
print("Cooling system activated")
return ctx
def is_low_temp(ctx):
return ctx.get("temp", 0) < 20
def heat_up(ctx):
ctx["heater"] = True
print("Heating system activated")
return ctx
# Create engine and add rules
engine = RuleEngine()
engine.add_rule(Rule("Too Hot", is_high_temp, cool_down, priority=10))
engine.add_rule(Rule("Too Cold", is_low_temp, heat_up, priority=5))
# Process context
context = {"temp": 33}
result = engine.run(context)def check_weather(ctx):
return ctx.get("weather") == "rainy"
def grab_umbrella(ctx):
ctx["umbrella"] = True
print("Grabbed umbrella")
return ctx
def check_location(ctx):
return ctx.get("location") == "outdoor"
def stay_inside(ctx):
ctx["location"] = "indoor"
print("Staying inside")
return ctx
# Chain rules together
umbrella_rule = Rule("Grab Umbrella", check_weather, grab_umbrella)
location_rule = Rule("Stay Inside", check_location, stay_inside, next_rule=umbrella_rule)
engine = RuleEngine()
engine.add_rule(location_rule)
context = {"weather": "rainy", "location": "outdoor"}
result = engine.run(context)def is_vip_customer(ctx):
return ctx.get("customer_tier") == "VIP"
def apply_vip_discount(ctx):
ctx["discount"] = 0.15
ctx["priority_shipping"] = True
print("VIP discount applied")
return ctx
def is_high_value_order(ctx):
return ctx.get("order_total", 0) > 1000
def apply_bulk_discount(ctx):
ctx["discount"] = ctx.get("discount", 0) + 0.10
print("Bulk discount applied")
return ctx
def is_express_shipping(ctx):
return ctx.get("shipping_type") == "express"
def calculate_shipping_cost(ctx):
base_cost = 10
if ctx.get("priority_shipping"):
base_cost += 5
ctx["shipping_cost"] = base_cost
return ctx
# Build order processing engine
engine = RuleEngine()
engine.add_rule(Rule("VIP Customer", is_vip_customer, apply_vip_discount, priority=20))
engine.add_rule(Rule("High Value", is_high_value_order, apply_bulk_discount, priority=15))
engine.add_rule(Rule("Express Shipping", is_express_shipping, calculate_shipping_cost, priority=10))
# Process order
order = {
"customer_tier": "VIP",
"order_total": 1200,
"shipping_type": "express"
}
result = engine.run(order)
print(result)The main engine for managing and executing rules.
add_rule(rule): Add a rule to the enginerun(context): Execute all rules against the given context
Represents a single rule with condition, action, and metadata.
Rule(name, condition, action, priority=0, next_rule=None)name(str): Human-readable name for the rulecondition(callable): Function that returns True/False based on contextaction(callable): Function to execute when condition is Truepriority(int): Higher numbers execute first (default: 0)next_rule(Rule): Optional rule to chain after this one
Contributions are welcome! Please feel free to submit a Pull Request.
- Fork the repository
- Create your feature branch (
git checkout -b feature/AmazingFeature) - Commit your changes (
git commit -m 'Add some AmazingFeature') - Push to the branch (
git push origin feature/AmazingFeature) - Open a Pull Request
This project is licensed under the MIT License - see the LICENSE file for details.
- Inspired by business rule engines and workflow management systems
- Built with simplicity and performance in mind
Made with β€οΈ by ErtuΔrul Kara