Skip to content

Commit 1dd711b

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

1 file changed

Lines changed: 92 additions & 0 deletions

File tree

khiops/core/dictionary.py

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

16651750
def __init__(self, *name_and_operands, verbatim=None, is_reference=False):
@@ -1742,6 +1827,13 @@ def copy(self):
17421827
def write(self, writer):
17431828
"""Writes the rule to a file writer in the ``.kdic`` format
17441829
1830+
This method ensures proper `Rule` serialization, automatically handling:
1831+
1832+
- back-quote recoding in variable names
1833+
- double-quote recoding in categorical constants
1834+
- missing data (``inf``, ``-inf``, ``NaN``) serialization as ``#Missing``
1835+
- upper-scope operator serialization as ``.``
1836+
17451837
Parameters
17461838
----------
17471839
writer : `.KhiopsOutputWriter`

0 commit comments

Comments
 (0)