Rule Formatting#
Functions#
simplify_rule#
- iguanas.rule_formatting.simplify_rule(rule: str) str[source]#
Simplify a rule by removing redundant conditions on the same column.
When multiple conditions exist on the same column, keeps only the most restrictive:
For lower bounds (>, >=): keeps the highest threshold, preferring > over >= when equal
For upper bounds (<, <=): keeps the lowest threshold, preferring < over <= when equal
- Parameters:
rule (str) – Rule string with conditions like (X[“col”] > val) & (X[“col”] >= val).
- Returns:
Simplified rule string with redundant conditions removed. Column order is preserved based on first appearance.
- Return type:
str
Examples
>>> simplify_rule('(X["amount"] >= 100.0) & (X["amount"] > 100.0)') '(X["amount"] > 100.0)'
>>> simplify_rule('(X["amount"] < 100.0) & (X["amount"] <= 100.0)') '(X["amount"] < 100.0)'
>>> simplify_rule('(X["a"] >= 50) & (X["b"] < 10) & (X["a"] > 100)') '(X["a"] > 100) & (X["b"] < 10)'
to_sql#
- iguanas.rule_formatting.to_sql(rule: str) str[source]#
Convert a rule expression string to a SQL WHERE clause fragment.
Replaces
X["col"]accessors with bare column names and converts&/|toAND/OR.- Parameters:
rule (str) – Rule string using
X["col"]notation with&/|operators.- Returns:
SQL WHERE clause fragment with column names unquoted and boolean operators uppercased.
- Return type:
str
Examples
>>> to_sql('(X["age"] >= 30) & (X["income"] < 50000)') '(age >= 30) AND (income < 50000)'
>>> to_sql('(X["score"] >= 0.8) | (X["flag"] == 1)') '(score >= 0.8) OR (flag == 1)'
rule_to_sql#
- iguanas.rule_formatting.rule_to_sql(rule: str, table_alias: str | None = None) str[source]#
Convert a rule expression string to a SQL WHERE clause.
Translates Iguanas rule notation (
X["col"] op value) into standard SQL predicate syntax suitable for use in aWHEREorCASE WHENclause.- Parameters:
rule (str) – Rule expression using
X["col"]notation with&/|operators, e.g.'(X["age"] > 30) & (X["income"] < 50000)'.table_alias (str | None, default=None) – Optional table or CTE alias to prefix column references with. For example,
table_alias="t"turnsage > 30intot.age > 30.
- Returns:
SQL WHERE clause string.
- Return type:
str
Examples
>>> rule_to_sql('(X["age"] > 30) & (X["income"] < 50000)') '(age > 30.0) AND (income < 50000.0)'
>>> rule_to_sql('(X["age"] > 30) | (X["flag"] == 1)', table_alias="t") '(t.age > 30.0) OR (t.flag = 1.0)'