Skip to content

Commit bc285ff

Browse files
committed
Improve no-operand rule specification: automatically insert parentheses
Indeed, parenthesized no-operand rule specification can only be done via the `verbatim` parameter.
1 parent ce77989 commit bc285ff

5 files changed

Lines changed: 21 additions & 20 deletions

File tree

doc/samples/samples.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ Samples
9797
third_dictionary.add_variable_from_spec(
9898
name="computed",
9999
type="Numerical",
100-
rule=str(kh.Rule("Ceil", kh.Rule("Product", 3, kh.Rule("Random()")))),
100+
rule=str(kh.Rule("Ceil", kh.Rule("Product", 3, kh.Rule("Random")))),
101101
)
102102
103103
# Add the variables used in a multi-table context in the first dictionary.
@@ -650,7 +650,7 @@ Samples
650650
# Create fold indexing rule and set it on `fold_index_variable`
651651
fold_index_variable = dictionary.get_variable("FoldIndex")
652652
fold_index_variable.rule = str(
653-
kh.Rule("Ceil", kh.Rule("Product", fold_number, kh.Rule("Random()"))),
653+
kh.Rule("Ceil", kh.Rule("Product", fold_number, kh.Rule("Random"))),
654654
)
655655
656656
# Add variables that indicate if the instance is in the train dataset:

khiops/core/dictionary.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,9 +1756,9 @@ def write(self, writer):
17561756
raise TypeError(type_error_message("writer", writer, KhiopsOutputWriter))
17571757

17581758
# Write standard rule
1759-
rule_pattern = r"^[A-Z]([a-zA-Z]*)\(?.*\)?$"
1760-
rule_regex = re.compile(rule_pattern)
1761-
bytes_rule_regex = re.compile(bytes(rule_pattern, encoding="ascii"))
1759+
rule_name_pattern = r"^[A-Z]([a-zA-Z]*)$"
1760+
rule_name_regex = re.compile(rule_name_pattern)
1761+
bytes_rule_name_regex = re.compile(bytes(rule_name_pattern, encoding="ascii"))
17621762
if self.operands:
17631763
if self.is_reference:
17641764
writer.write("[")
@@ -1789,11 +1789,14 @@ def write(self, writer):
17891789
# Write no-operand rule
17901790
elif (
17911791
isinstance(self.name, str)
1792-
and rule_regex.match(self.name)
1792+
and rule_name_regex.match(self.name)
17931793
or isinstance(self.name, bytes)
1794-
and bytes_rule_regex.match(self.name)
1794+
and bytes_rule_name_regex.match(self.name)
17951795
):
1796-
writer.write(self.name)
1796+
writer.write(_format_name(self.name))
1797+
1798+
# Add parentheses automatically
1799+
writer.write("()")
17971800
# Write verbatim-given rule
17981801
elif self._verbatim:
17991802
writer.write(self._verbatim)

khiops/samples/samples.ipynb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@
109109
"third_dictionary.add_variable_from_spec(\n",
110110
" name=\"computed\",\n",
111111
" type=\"Numerical\",\n",
112-
" rule=str(kh.Rule(\"Ceil\", kh.Rule(\"Product\", 3, kh.Rule(\"Random()\")))),\n",
112+
" rule=str(kh.Rule(\"Ceil\", kh.Rule(\"Product\", 3, kh.Rule(\"Random\")))),\n",
113113
")\n",
114114
"\n",
115115
"# Add the variables used in a multi-table context in the first dictionary.\n",
@@ -870,7 +870,7 @@
870870
"# Create fold indexing rule and set it on `fold_index_variable`\n",
871871
"fold_index_variable = dictionary.get_variable(\"FoldIndex\")\n",
872872
"fold_index_variable.rule = str(\n",
873-
" kh.Rule(\"Ceil\", kh.Rule(\"Product\", fold_number, kh.Rule(\"Random()\"))),\n",
873+
" kh.Rule(\"Ceil\", kh.Rule(\"Product\", fold_number, kh.Rule(\"Random\"))),\n",
874874
")\n",
875875
"\n",
876876
"# Add variables that indicate if the instance is in the train dataset:\n",

khiops/samples/samples.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ def create_dictionary_domain():
111111
third_dictionary.add_variable_from_spec(
112112
name="computed",
113113
type="Numerical",
114-
rule=str(kh.Rule("Ceil", kh.Rule("Product", 3, kh.Rule("Random()")))),
114+
rule=str(kh.Rule("Ceil", kh.Rule("Product", 3, kh.Rule("Random")))),
115115
)
116116

117117
# Add the variables used in a multi-table context in the first dictionary.
@@ -725,7 +725,7 @@ def train_predictor_with_cross_validation():
725725
# Create fold indexing rule and set it on `fold_index_variable`
726726
fold_index_variable = dictionary.get_variable("FoldIndex")
727727
fold_index_variable.rule = str(
728-
kh.Rule("Ceil", kh.Rule("Product", fold_number, kh.Rule("Random()"))),
728+
kh.Rule("Ceil", kh.Rule("Product", fold_number, kh.Rule("Random"))),
729729
)
730730

731731
# Add variables that indicate if the instance is in the train dataset:

tests/test_core.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,9 +1904,7 @@ def test_dictionary_accessors(self):
19041904
name="fresh_one",
19051905
type="Categorical",
19061906
meta_data={"a": 1, "b": 2},
1907-
rule=str(
1908-
kh.Rule("Ceil", kh.Rule("Product", 3, kh.Rule("Random()")))
1909-
),
1907+
rule=str(kh.Rule("Ceil", kh.Rule("Product", 3, kh.Rule("Random")))),
19101908
)
19111909
self.assertEqual(
19121910
2,
@@ -2020,7 +2018,7 @@ def test_dictionary_accessors(self):
20202018
"SomeRuleForVariable" + variable_index * "i",
20212019
"an_operand",
20222020
2,
2023-
kh.Rule("SomeEmbeddedRule()"),
2021+
kh.Rule("SomeEmbeddedRule"),
20242022
)
20252023
dictionary_copy.get_variable(variable_name).rule = str(some_rule)
20262024
self.assertEqual(
@@ -2062,7 +2060,7 @@ def test_dictionary_accessors(self):
20622060
"SomeRuleForVariableBlock" + variable_block_index * "i",
20632061
"an_operand",
20642062
2,
2065-
kh.Rule("SomeEmbeddedRule()"),
2063+
kh.Rule("SomeEmbeddedRule"),
20662064
)
20672065
dictionary_copy.get_variable_block(variable_block_name).rule = str(
20682066
some_rule
@@ -2083,8 +2081,8 @@ def test_dictionary_accessors(self):
20832081
def test_dictionary_rule_construction(self):
20842082
"""Tests the Rule construction and serialization"""
20852083
rule_verbatims = [
2086-
"SomeRule",
2087-
b"SomeRule",
2084+
"SomeRule()",
2085+
b"SomeRule()",
20882086
'SomeRule("some_operand", 2)',
20892087
b'SomeRule("some_operand", 2)',
20902088
'SomeRule("some""operand", 2)',
@@ -2260,7 +2258,7 @@ def test_dictionary_rule_construction(self):
22602258
kh.Rule(b"SomeEmbeddedRule", b"some_other_operand"),
22612259
),
22622260
kh.Rule(
2263-
(
2261+
verbatim=(
22642262
b'SomeRule("some_operand", 2, '
22652263
b'SomeEmbeddedRule("some_other_operand"))'
22662264
)

0 commit comments

Comments
 (0)