Skip to content

Commit e0c6e5d

Browse files
committed
Make upper_scope a global and composable function
Thus, `upper_scope` can be composed with itself an arbitrary number of times, thus allowing to support multi-level upper scopes in snowflake multi-table topologies.
1 parent 652fd7e commit e0c6e5d

File tree

2 files changed

+75
-18
lines changed

2 files changed

+75
-18
lines changed

khiops/core/dictionary.py

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ def _quote_value(value):
8383

8484
class _ScopedOperand:
8585
def __init__(self, operand):
86-
assert type(operand) in (Variable, Rule), type_error_message(
87-
"operand", operand, Variable, Rule
86+
assert type(operand) in (Variable, Rule, _ScopedOperand), type_error_message(
87+
"operand", operand, Variable, Rule, "upper-scoped Variable or Rule"
8888
)
8989
self.operand = operand
9090

@@ -105,6 +105,35 @@ def __repr__(self):
105105
return str(stream.getvalue(), encoding="utf8", errors="replace")
106106

107107

108+
def upper_scope(operand):
109+
"""Adds the '.' upper-scope prefix to the serialization of the operand.
110+
111+
Parameters
112+
----------
113+
operand : `Variable`, `Rule`, upper-scoped `Variable` or upper-scoped `Rule`
114+
Operand that is upper-scoped.
115+
116+
Raises
117+
------
118+
`TypeError`
119+
If the type of ``operand`` is not `Variable`, `Rule`, upper-scoped `Variable`
120+
or upper-scoped `Rule`.
121+
122+
Returns
123+
-------
124+
upper-scoped operand
125+
Upper-scoped operand, serialized as prefixed with ``.``.
126+
127+
"""
128+
if not isinstance(operand, (Variable, Rule, _ScopedOperand)):
129+
raise TypeError(
130+
type_error_message(
131+
"operand", operand, Variable, Rule, "upper-scoped Variable or Rule"
132+
)
133+
)
134+
return _ScopedOperand(operand)
135+
136+
108137
class DictionaryDomain(KhiopsJSONObject):
109138
"""Main class containing the information of a Khiops dictionary file
110139
@@ -1198,10 +1227,6 @@ def __str__(self):
11981227
self.write(writer)
11991228
return str(stream.getvalue(), encoding="utf8", errors="replace")
12001229

1201-
def upper_scope(self):
1202-
"""Adds the '.' upper-scope prefix to the serialization of the operand."""
1203-
return _ScopedOperand(self)
1204-
12051230
def copy(self):
12061231
"""Copies this variable instance
12071232
@@ -1659,10 +1684,6 @@ def __repr__(self):
16591684
self.write(writer)
16601685
return str(stream.getvalue(), encoding="utf8", errors="replace")
16611686

1662-
def upper_scope(self):
1663-
"""Adds the '.' upper-scope prefix to the serialization of the operand."""
1664-
return _ScopedOperand(self)
1665-
16661687
def copy(self):
16671688
"""Copies this rule instance
16681689
@@ -1682,7 +1703,8 @@ def write(self, writer):
16821703
Output writer.
16831704
16841705
.. note::
1685-
If ``self.is_reference`` is set, then ``self.name`` is not included in the serialization.
1706+
If ``self.is_reference`` is set, then ``self.name`` is not included
1707+
in the serialization.
16861708
"""
16871709
# Check the type of the writer
16881710
if not isinstance(writer, KhiopsOutputWriter):

tests/test_core.py

Lines changed: 42 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1991,11 +1991,13 @@ def test_dictionary_rule_construction(self):
19911991
b'SomeRule("some_operand", 2, `Some``Variable`)',
19921992
'SomeRule("some_operand", 2, .SomeVariable)',
19931993
b'SomeRule("some_operand", 2, .SomeVariable)',
1994+
'SomeRule("some_operand", 2, ..SomeVariable)',
19941995
'SomeRule("some_operand", #Missing)',
19951996
b'SomeRule("some_operand", #Missing)',
19961997
'SomeRule("some_operand", 2, SomeEmbeddedRule("some_other_operand"))',
19971998
b'SomeRule("some_operand", 2, SomeEmbeddedRule("some_other_operand"))',
19981999
'SomeRule("some_operand", 2, .SomeEmbeddedRule("some_other_operand"))',
2000+
'SomeRule("some_operand", 2, ..SomeEmbeddedRule("some_other_operand"))',
19992001
(
20002002
'SomeRule("some_operand", 2, SomeEmbeddedRule("some_other_operand", '
20012003
'SomeOtherRule("some_embedded_operand", #Missing, 3)))'
@@ -2083,19 +2085,40 @@ def test_dictionary_rule_construction(self):
20832085
"SomeRule",
20842086
"some_operand",
20852087
2,
2086-
kh.Variable(
2087-
json_data={"name": "SomeVariable", "type": "Categorical"}
2088-
).upper_scope(),
2088+
kh.upper_scope(
2089+
kh.Variable(
2090+
json_data={"name": "SomeVariable", "type": "Categorical"}
2091+
)
2092+
),
20892093
),
20902094
],
20912095
[
20922096
kh.Rule(
20932097
b"SomeRule",
20942098
b"some_operand",
20952099
2,
2096-
kh.Variable(
2097-
json_data={"name": b"SomeVariable", "type": b"Categorical"}
2098-
).upper_scope(),
2100+
kh.upper_scope(
2101+
kh.Variable(
2102+
json_data={"name": b"SomeVariable", "type": b"Categorical"}
2103+
)
2104+
),
2105+
),
2106+
],
2107+
[
2108+
kh.Rule(
2109+
"SomeRule",
2110+
"some_operand",
2111+
2,
2112+
kh.upper_scope(
2113+
kh.upper_scope(
2114+
kh.Variable(
2115+
json_data={
2116+
"name": "SomeVariable",
2117+
"type": "Categorical",
2118+
}
2119+
)
2120+
)
2121+
),
20992122
),
21002123
],
21012124
[
@@ -2143,7 +2166,19 @@ def test_dictionary_rule_construction(self):
21432166
"SomeRule",
21442167
"some_operand",
21452168
2,
2146-
kh.Rule("SomeEmbeddedRule", "some_other_operand").upper_scope(),
2169+
kh.upper_scope(kh.Rule("SomeEmbeddedRule", "some_other_operand")),
2170+
),
2171+
],
2172+
[
2173+
kh.Rule(
2174+
"SomeRule",
2175+
"some_operand",
2176+
2,
2177+
kh.upper_scope(
2178+
kh.upper_scope(
2179+
kh.Rule("SomeEmbeddedRule", "some_other_operand")
2180+
)
2181+
),
21472182
),
21482183
],
21492184
[

0 commit comments

Comments
 (0)