Skip to content

Commit 0957174

Browse files
committed
Add the upper_scope method to Variable and Rule
Thus, this entails the serialization of the Variable or Rule operand as prefixed by a dot.
1 parent 1de3579 commit 0957174

File tree

2 files changed

+58
-2
lines changed

2 files changed

+58
-2
lines changed

khiops/core/dictionary.py

Lines changed: 38 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,23 @@ def _quote_value(value):
8181
return quoted_value
8282

8383

84+
class _ScopedOperand:
85+
def __init__(self, operand):
86+
assert type(operand) in (Variable, Rule), type_error_message(
87+
"operand", operand, Variable, Rule
88+
)
89+
self.operand = operand
90+
91+
def __repr__(self):
92+
stream = io.BytesIO()
93+
writer = KhiopsOutputWriter(stream)
94+
if isinstance(self.operand, Variable):
95+
writer.write(_format_name(self.operand.name))
96+
else:
97+
self.operand.write(writer)
98+
return "." + str(stream.getvalue(), encoding="utf8", errors="replace")
99+
100+
84101
class DictionaryDomain(KhiopsJSONObject):
85102
"""Main class containing the information of a Khiops dictionary file
86103
@@ -1206,6 +1223,10 @@ def __str__(self):
12061223
self.write(writer)
12071224
return str(stream.getvalue(), encoding="utf8", errors="replace")
12081225

1226+
def upper_scope(self):
1227+
"""Adds the '.' upper-scope prefix to the serialization of the operand."""
1228+
return _ScopedOperand(self)
1229+
12091230
def copy(self):
12101231
"""Copies this variable instance
12111232
@@ -1583,10 +1604,20 @@ def __init__(self, name, *operands):
15831604
if not isinstance(name, str):
15841605
raise TypeError(type_error_message("name", name, str))
15851606
for operand in operands:
1586-
if not isinstance(operand, (str, int, float, Variable, Rule)):
1607+
if not isinstance(
1608+
operand, (str, int, float, Variable, Rule, _ScopedOperand)
1609+
):
15871610
raise TypeError(
15881611
type_error_message(
1589-
f"Operand '{operand}'", operand, str, int, float, Variable, Rule
1612+
f"Operand '{operand}'",
1613+
operand,
1614+
str,
1615+
int,
1616+
float,
1617+
Variable,
1618+
Rule,
1619+
"upper-scoped Variable",
1620+
"upper-scoped Rule",
15901621
)
15911622
)
15921623
if not name:
@@ -1602,6 +1633,10 @@ def __repr__(self):
16021633
self.write(writer)
16031634
return str(stream.getvalue(), encoding="utf8", errors="replace")
16041635

1636+
def upper_scope(self):
1637+
"""Adds the '.' upper-scope prefix to the serialization of the operand."""
1638+
return _ScopedOperand(self)
1639+
16051640
def copy(self):
16061641
"""Copies this rule instance
16071642
@@ -1640,6 +1675,7 @@ def write(self, writer):
16401675
writer.write(f'"{operand}"')
16411676
elif isinstance(operand, float) and not math.isfinite(operand):
16421677
writer.write("#Missing")
1678+
# int, finite float or _ScopedOperand cases
16431679
else:
16441680
writer.write(str(operand))
16451681
if i < len(self.operands) - 1:

tests/test_core.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1977,8 +1977,10 @@ def test_dictionary_rule_construction(self):
19771977
"SomeRule()",
19781978
'SomeRule("some_operand", 2)',
19791979
'SomeRule("some_operand", 2, SomeVariable)',
1980+
'SomeRule("some_operand", 2, .SomeVariable)',
19801981
'SomeRule("some_operand", #Missing)',
19811982
'SomeRule("some_operand", 2, SomeEmbeddedRule("some_other_operand"))',
1983+
'SomeRule("some_operand", 2, .SomeEmbeddedRule("some_other_operand"))',
19821984
(
19831985
'SomeRule("some_operand", 2, SomeEmbeddedRule("some_other_operand", '
19841986
'SomeOtherRule("some_embedded_operand", #Missing, 3)))'
@@ -2003,6 +2005,16 @@ def test_dictionary_rule_construction(self):
20032005
),
20042006
),
20052007
],
2008+
[
2009+
kh.Rule(
2010+
"SomeRule",
2011+
"some_operand",
2012+
2,
2013+
kh.Variable(
2014+
json_data={"name": "SomeVariable", "type": "Categorical"}
2015+
).upper_scope(),
2016+
),
2017+
],
20062018
[
20072019
kh.Rule("SomeRule", "some_operand", math.nan),
20082020
kh.Rule("SomeRule", "some_operand", float("inf")),
@@ -2023,6 +2035,14 @@ def test_dictionary_rule_construction(self):
20232035
)
20242036
),
20252037
],
2038+
[
2039+
kh.Rule(
2040+
"SomeRule",
2041+
"some_operand",
2042+
2,
2043+
kh.Rule("SomeEmbeddedRule", "some_other_operand").upper_scope(),
2044+
),
2045+
],
20262046
[
20272047
kh.Rule(
20282048
"SomeRule",

0 commit comments

Comments
 (0)