Skip to content

Commit 6ad654c

Browse files
committed
Put rule getters and setters on variable and variable blocks
Thus, rules are treated in the same way as the other Variable / VariableBlock fields.
1 parent 4c5d3cd commit 6ad654c

File tree

5 files changed

+56
-48
lines changed

5 files changed

+56
-48
lines changed

doc/samples/samples.rst

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -658,8 +658,7 @@ Samples
658658
dictionary.add_variable(fold_index_variable)
659659
660660
# Create fold indexing rule and set it on `fold_index_variable`
661-
dictionary.set_variable_rule(
662-
fold_index_variable.name,
661+
dictionary.get_variable(fold_index_variable.name).set_rule(
663662
kh.Rule("Ceil", kh.Rule("Product", fold_number, kh.Rule("Random()"))),
664663
)
665664
@@ -670,8 +669,7 @@ Samples
670669
is_in_train_dataset_variable.type = "Numerical"
671670
is_in_train_dataset_variable.used = False
672671
dictionary.add_variable(is_in_train_dataset_variable)
673-
dictionary.set_variable_rule(
674-
is_in_train_dataset_variable.name,
672+
dictionary.get_variable(is_in_train_dataset_variable.name).set_rule(
675673
kh.Rule("NEQ", fold_index_variable, fold_index),
676674
)
677675

khiops/core/dictionary.py

Lines changed: 42 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -905,38 +905,6 @@ def remove_variable_block(
905905

906906
return removed_block
907907

908-
def set_variable_rule(self, variable_name, rule):
909-
"""Sets a rule on a specified variable in the dictionary
910-
911-
Parameters
912-
----------
913-
variable_name : str
914-
Name of the variable the rule is set on.
915-
rule : `Rule`
916-
The rule to be set on the variable whose name is ``variable_name``.
917-
918-
Raises
919-
------
920-
`TypeError`
921-
If ``rule`` is not of type `Rule`
922-
If ``variable_name`` is not of type `str`
923-
924-
`ValueError`
925-
If ``variable_name`` is the empty string
926-
927-
`KeyError`
928-
If no variable of name ``variable_name`` exists in the dictionary
929-
"""
930-
if not is_string_like(variable_name):
931-
raise TypeError(
932-
type_error_message("variable_name", variable_name, "string-like")
933-
)
934-
if not variable_name:
935-
raise ValueError("'variable_name' must not be empty")
936-
if not isinstance(rule, Rule):
937-
raise TypeError(type_error_message("rule", rule, Rule))
938-
self.get_variable(variable_name).rule = repr(rule)
939-
940908
def set_variable_block_rule(self, variable_block_name, rule):
941909
"""Sets a rule on a specified variable block in the dictionary
942910
@@ -1331,6 +1299,27 @@ def full_type(self):
13311299
full_type += f"({self.structure_type})"
13321300
return full_type
13331301

1302+
def get_rule(self):
1303+
"""Gets `Rule` from a specified variable"""
1304+
return Rule(name=self.rule)
1305+
1306+
def set_rule(self, rule):
1307+
"""Sets a rule on a specified variable in the dictionary
1308+
1309+
Parameters
1310+
----------
1311+
rule : `Rule`
1312+
The rule to be set on the variable.
1313+
1314+
Raises
1315+
------
1316+
`TypeError`
1317+
If ``rule`` is not of type `Rule`
1318+
"""
1319+
if not isinstance(rule, Rule):
1320+
raise TypeError(type_error_message("rule", rule, Rule))
1321+
self.rule = repr(rule)
1322+
13341323
def write(self, writer):
13351324
"""Writes the domain to a file writer in ``.kdic`` format
13361325
@@ -1506,6 +1495,27 @@ def get_value(self, key):
15061495
"""
15071496
return self.meta_data.get_value(key)
15081497

1498+
def get_rule(self):
1499+
"""Gets `Rule` from a specified variable block"""
1500+
return Rule(name=self.rule)
1501+
1502+
def set_rule(self, rule):
1503+
"""Sets a rule on a specified variable block in the dictionary
1504+
1505+
Parameters
1506+
----------
1507+
rule : `Rule`
1508+
The rule to be set on the variable block
1509+
1510+
Raises
1511+
------
1512+
`TypeError`
1513+
If ``rule`` is not of type `Rule`
1514+
"""
1515+
if not isinstance(rule, Rule):
1516+
raise TypeError(type_error_message("rule", rule, Rule))
1517+
self.rule = repr(rule)
1518+
15091519
def write(self, writer):
15101520
"""Writes the variable block to a file writer in ``.kdic`` format
15111521

khiops/samples/samples.ipynb

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -878,8 +878,7 @@
878878
"dictionary.add_variable(fold_index_variable)\n",
879879
"\n",
880880
"# Create fold indexing rule and set it on `fold_index_variable`\n",
881-
"dictionary.set_variable_rule(\n",
882-
" fold_index_variable.name,\n",
881+
"dictionary.get_variable(fold_index_variable.name).set_rule(\n",
883882
" kh.Rule(\"Ceil\", kh.Rule(\"Product\", fold_number, kh.Rule(\"Random()\"))),\n",
884883
")\n",
885884
"\n",
@@ -890,8 +889,7 @@
890889
" is_in_train_dataset_variable.type = \"Numerical\"\n",
891890
" is_in_train_dataset_variable.used = False\n",
892891
" dictionary.add_variable(is_in_train_dataset_variable)\n",
893-
" dictionary.set_variable_rule(\n",
894-
" is_in_train_dataset_variable.name,\n",
892+
" dictionary.get_variable(is_in_train_dataset_variable.name).set_rule(\n",
895893
" kh.Rule(\"NEQ\", fold_index_variable, fold_index),\n",
896894
" )\n",
897895
"\n",

khiops/samples/samples.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -731,8 +731,7 @@ def train_predictor_with_cross_validation():
731731
dictionary.add_variable(fold_index_variable)
732732

733733
# Create fold indexing rule and set it on `fold_index_variable`
734-
dictionary.set_variable_rule(
735-
fold_index_variable.name,
734+
dictionary.get_variable(fold_index_variable.name).set_rule(
736735
kh.Rule("Ceil", kh.Rule("Product", fold_number, kh.Rule("Random()"))),
737736
)
738737

@@ -743,8 +742,7 @@ def train_predictor_with_cross_validation():
743742
is_in_train_dataset_variable.type = "Numerical"
744743
is_in_train_dataset_variable.used = False
745744
dictionary.add_variable(is_in_train_dataset_variable)
746-
dictionary.set_variable_rule(
747-
is_in_train_dataset_variable.name,
745+
dictionary.get_variable(is_in_train_dataset_variable.name).set_rule(
748746
kh.Rule("NEQ", fold_index_variable, fold_index),
749747
)
750748

tests/test_core.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1881,7 +1881,7 @@ def test_dictionary_accessors(self):
18811881
# Set the block as non-native add, and remove it
18821882
dictionary_copy.add_variable_block(block)
18831883
block_rule = kh.Rule("SomeBlockCreatingRule()")
1884-
dictionary_copy.set_variable_block_rule(block.name, block_rule)
1884+
dictionary_copy.get_variable_block(block.name).set_rule(block_rule)
18851885
self.assertEqual(block, dictionary_copy.get_variable_block(block.name))
18861886
removed_block = dictionary_copy.remove_variable_block(
18871887
block.name,
@@ -1936,11 +1936,15 @@ def test_dictionary_accessors(self):
19361936
2,
19371937
kh.Rule("SomeEmbeddedRule()"),
19381938
)
1939-
dictionary_copy.set_variable_rule(variable_name, some_rule)
1939+
dictionary_copy.get_variable(variable_name).set_rule(some_rule)
19401940
self.assertEqual(
19411941
dictionary_copy.get_variable(variable_name).rule,
19421942
repr(some_rule),
19431943
)
1944+
self.assertEqual(
1945+
repr(dictionary_copy.get_variable(variable_name).get_rule()),
1946+
repr(some_rule),
1947+
)
19441948
self.assertEqual(
19451949
repr(dictionary_copy.get_variable_rule(variable_name)),
19461950
repr(some_rule),
@@ -1957,8 +1961,8 @@ def test_dictionary_accessors(self):
19571961
2,
19581962
kh.Rule("SomeEmbeddedRule()"),
19591963
)
1960-
dictionary_copy.set_variable_block_rule(
1961-
variable_block_name, some_rule
1964+
dictionary_copy.get_variable_block(variable_block_name).set_rule(
1965+
some_rule
19621966
)
19631967
self.assertEqual(
19641968
dictionary_copy.get_variable_block(variable_block_name).rule,

0 commit comments

Comments
 (0)