Skip to content

Commit ea6ecf5

Browse files
author
Thierry RAMORASOAVINA
committed
Accept a more specific variable rule in Dictionary.add_variable_from_spec instead of a verbatim string
1 parent 4824303 commit ea6ecf5

File tree

5 files changed

+32
-8
lines changed

5 files changed

+32
-8
lines changed

doc/samples/samples.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,12 @@ Samples
9393
third_dictionary.add_variable_from_spec(name="StreetNumber", type="Numerical")
9494
third_dictionary.add_variable_from_spec(name="StreetName", type="Categorical")
9595
third_dictionary.add_variable_from_spec(name="id_city", type="Categorical")
96+
# Add a variable with a rule
97+
third_dictionary.add_variable_from_spec(
98+
name="computed",
99+
type="Numerical",
100+
rule=kh.Rule("Ceil", kh.Rule("Product", 3, kh.Rule("Random()"))),
101+
)
96102
97103
# Add the variables used in a multi-table context in the first dictionary.
98104
# They link the root dictionary to the additional ones

khiops/core/dictionary.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -845,8 +845,8 @@ def add_variable_from_spec(
845845
Object type. Ignored if variable type not in ["Entity", "Table"]
846846
structure_type : str, optional
847847
Structure type. Ignored if variable type is not "Structure"
848-
rule : str, optional
849-
Variable rule (in verbatim).
848+
rule : `Rule`, optional
849+
Variable rule.
850850
meta_data : dict, optional
851851
A Python dictionary which holds the metadata specification.
852852
The dictionary keys are str.
@@ -901,8 +901,8 @@ def add_variable_from_spec(
901901
type_error_message("structure_type", structure_type, "string-like")
902902
)
903903
if rule is not None:
904-
if not is_string_like(rule):
905-
raise TypeError(type_error_message("rule", rule, "string-like"))
904+
if not isinstance(rule, Rule):
905+
raise TypeError(type_error_message("rule", rule, Rule))
906906

907907
# Variable initialization
908908
variable = Variable()
@@ -919,7 +919,7 @@ def add_variable_from_spec(
919919
if structure_type is not None:
920920
variable.structure_type = structure_type
921921
if rule is not None:
922-
variable.rule = Rule(verbatim=rule)
922+
variable.rule = repr(rule)
923923
self.add_variable(variable)
924924

925925
def remove_variable(self, variable_name):

khiops/samples/samples.ipynb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@
105105
"third_dictionary.add_variable_from_spec(name=\"StreetNumber\", type=\"Numerical\")\n",
106106
"third_dictionary.add_variable_from_spec(name=\"StreetName\", type=\"Categorical\")\n",
107107
"third_dictionary.add_variable_from_spec(name=\"id_city\", type=\"Categorical\")\n",
108+
"# Add a variable with a rule\n",
109+
"third_dictionary.add_variable_from_spec(\n",
110+
" name=\"computed\",\n",
111+
" type=\"Numerical\",\n",
112+
" rule=kh.Rule(\"Ceil\", kh.Rule(\"Product\", 3, kh.Rule(\"Random()\"))),\n",
113+
")\n",
108114
"\n",
109115
"# Add the variables used in a multi-table context in the first dictionary.\n",
110116
"# They link the root dictionary to the additional ones\n",

khiops/samples/samples.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,12 @@ def create_dictionary_domain():
107107
third_dictionary.add_variable_from_spec(name="StreetNumber", type="Numerical")
108108
third_dictionary.add_variable_from_spec(name="StreetName", type="Categorical")
109109
third_dictionary.add_variable_from_spec(name="id_city", type="Categorical")
110+
# Add a variable with a rule
111+
third_dictionary.add_variable_from_spec(
112+
name="computed",
113+
type="Numerical",
114+
rule=kh.Rule("Ceil", kh.Rule("Product", 3, kh.Rule("Random()"))),
115+
)
110116

111117
# Add the variables used in a multi-table context in the first dictionary.
112118
# They link the root dictionary to the additional ones

tests/test_core.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1882,8 +1882,8 @@ def test_dictionary_accessors(self):
18821882
dictionary.add_variable_from_spec(
18831883
name="fresh_one", type="Entity", object_type={}
18841884
)
1885-
# rule must be a str
1886-
with self.assertRaisesRegex(TypeError, "'rule'.*string-like"):
1885+
# rule must be Rule object
1886+
with self.assertRaisesRegex(TypeError, "'rule'.*Rule"):
18871887
dictionary.add_variable_from_spec(
18881888
name="fresh_one", type="Categorical", rule={}
18891889
)
@@ -1892,17 +1892,23 @@ def test_dictionary_accessors(self):
18921892
dictionary.add_variable_from_spec(
18931893
name="fresh_one", type="Categorical", meta_data="str"
18941894
)
1895-
# successful adding (with meta-data)
1895+
# successful adding (with meta-data and rule)
18961896
dictionary.add_variable_from_spec(
18971897
name="fresh_one",
18981898
type="Categorical",
18991899
meta_data={"a": 1, "b": 2},
1900+
rule=kh.Rule("Ceil", kh.Rule("Product", 3, kh.Rule("Random()"))),
19001901
)
19011902
self.assertEqual(
19021903
2,
19031904
dictionary.get_variable("fresh_one").get_value("b"),
19041905
"Variable meta-data must be set correctly",
19051906
)
1907+
self.assertEqual(
1908+
"Ceil(Product(3, Random()))",
1909+
repr(dictionary.get_variable("fresh_one").get_rule()),
1910+
"Variable rule must be set correctly",
1911+
)
19061912

19071913
# Test Dictionary variable block accessors
19081914
# Create a simple block

0 commit comments

Comments
 (0)