Skip to content

Commit 9a43909

Browse files
committed
Improve Rule documentation
- add rule creation examples - add details to the docstring of the `Rule.write` method.
1 parent bc285ff commit 9a43909

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

khiops/core/dictionary.py

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,6 +1661,77 @@ class Rule:
16611661
16621662
.. note::
16631663
This attribute cannot be changed on a `Rule` instance.
1664+
1665+
Examples
1666+
--------
1667+
- basic rule, with variables as operands:
1668+
- verbatim:
1669+
1670+
>>> Product(PetalLength, PetalWidth)
1671+
1672+
- object construction:
1673+
1674+
>>> petal_length_var = kh.Variable()
1675+
>>> petal_length_var.name = "PetalLength"
1676+
>>> petal_length_var.type = "Numerical"
1677+
>>> petal_width_var = kh.Variable()
1678+
>>> petal_width_var.name = "PetalWidth"
1679+
>>> petal_width_var.type = "Numerical"
1680+
>>> rule = kh.Rule("Product", petal_length_var, petal_width_var)
1681+
1682+
- multi-table rule:
1683+
- verbatim:
1684+
1685+
>>> TableCount(TableSelection(Vehicles, EQ(PassengerNumber, 1)))
1686+
1687+
- object construction:
1688+
1689+
>>> vehicles_var = accidents_dictionary.get_variable("Vehicles")
1690+
>>> passenger_number_var = vehicles_dictionary.get_variable(
1691+
... "PassengerNumber"
1692+
... )
1693+
>>> rule = kh.Rule(
1694+
... "TableCount",
1695+
... kh.Rule(
1696+
... "TableSelection",
1697+
... vehicles_var,
1698+
... kh.Rule("EQ", passenger_number_var, 1)
1699+
... )
1700+
... )
1701+
1702+
- multi-table rule with upper-scoped operands (advanced usage):
1703+
- verbatim:
1704+
1705+
>>> TableSelection(
1706+
... Vehicles,
1707+
... EQ(
1708+
... PassengerNumber,
1709+
... .TableMax(Vehicles, PassengerNumber)
1710+
... )
1711+
... )
1712+
1713+
- object construction:
1714+
1715+
>>> vehicles_var = accidents_dictionary.get_variable("Vehicles")
1716+
>>> passenger_number_var = vehicles_dictionary.get_variable(
1717+
... "PassengerNumber"
1718+
... )
1719+
>>> rule = kh.Rule(
1720+
... "TableSelection",
1721+
... vehicles_var,
1722+
... kh.Rule(
1723+
... "EQ",
1724+
... passenger_number_var,
1725+
... kh.upper_scope(
1726+
... kh.Rule(
1727+
... "TableMax",
1728+
... vehicle_var,
1729+
... passenger_number_var
1730+
... )
1731+
... )
1732+
... )
1733+
... )
1734+
16641735
"""
16651736

16661737
def __init__(self, *name_and_operands, verbatim=None, is_reference=False):
@@ -1743,6 +1814,13 @@ def copy(self):
17431814
def write(self, writer):
17441815
"""Writes the rule to a file writer in the ``.kdic`` format
17451816
1817+
This method ensures proper `Rule` serialization, automatically handling:
1818+
1819+
- back-quote recoding in variable names
1820+
- double-quote recoding in categorical constants
1821+
- missing data (``inf``, ``-inf``, ``NaN``) serialization as ``#Missing``
1822+
- upper-scope operator serialization as ``.``
1823+
17461824
Parameters
17471825
----------
17481826
writer : `.KhiopsOutputWriter`

0 commit comments

Comments
 (0)