Skip to content

Commit 397f77a

Browse files
author
Thierry RAMORASOAVINA
committed
List all possible variable types of class Variable
1 parent ef17036 commit 397f77a

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

khiops/core/dictionary.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -971,6 +971,13 @@ class Variable:
971971
True if the variable is used.
972972
type : str
973973
Variable type.
974+
It can be either native (``Categorical``, ``Numerical``, ``Time``,
975+
``Date``, ``Timestamp``, ``TimestampTZ``, ``Text``),
976+
internal (``TextList``, ``Structure``)
977+
- See https://khiops.org/11.0.0/api-docs/kdic/text-list-rules/
978+
- See https://khiops.org/api-docs/kdic/structures-introduction/
979+
or relational (``Entity`` - 0-1 relationship, ``Table`` - 0-n relationship)
980+
- See https://khiops.org/tutorials/kdic_multi_table/
974981
object_type : str
975982
Type complement for the ``Table`` and ``Entity`` types.
976983
structure_type : str
@@ -986,6 +993,11 @@ class Variable:
986993
List of variable comments.
987994
meta_data : `MetaData`
988995
Variable metadata.
996+
997+
Examples
998+
--------
999+
See the following function of the ``samples.py`` documentation script:
1000+
- `samples.create_dictionary()`
9891001
"""
9901002

9911003
def __init__(self, json_data=None):

khiops/samples/samples.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import khiops
1515
import os
1616
from khiops import core as kh
17+
from khiops.core import KhiopsOutputWriter
1718

1819
# Disable open files without encoding because samples are simple code snippets
1920
# pylint: disable=unspecified-encoding
@@ -65,6 +66,85 @@ def build_dictionary_from_data_table():
6566
)
6667

6768

69+
def create_dictionary_domain():
70+
"""Creates a dictionary domain (a set of dictionaries) from scratch
71+
with all the possible variable types
72+
"""
73+
# Imports
74+
import os
75+
from khiops import core as kh
76+
77+
# Creates a Root dictionary
78+
root_dictionary = kh.Dictionary(
79+
json_data={"name": "dict_from_scratch", "root": True, "key": ["Id"]}
80+
)
81+
82+
# Starts with simple variables to declare
83+
simple_variables = [
84+
{"name": "Id", "type": "Categorical"},
85+
{"name": "Num", "type": "Numerical"},
86+
{"name": "text", "type": "Text"},
87+
{"name": "hour", "type": "Time"},
88+
{"name": "date", "type": "Date"},
89+
{"name": "ambiguous_ts", "type": "Timestamp"},
90+
{"name": "ts", "type": "TimestampTZ"},
91+
]
92+
for var_spec in simple_variables:
93+
var = kh.Variable()
94+
var.name = var_spec["name"]
95+
var.type = var_spec["type"]
96+
root_dictionary.add_variable(var)
97+
98+
# Creates a second dictionary (not linked yet)
99+
second_dictionary = kh.Dictionary(
100+
json_data={"name": "Service", "key": ["Id", "id_product"]}
101+
)
102+
second_dictionary.add_variable(
103+
kh.Variable(json_data={"name": "Id", "type": "Categorical"})
104+
)
105+
second_dictionary.add_variable(
106+
kh.Variable(json_data={"name": "id_product", "type": "Categorical"})
107+
)
108+
# Creates a third dictionary (not linked yet)
109+
third_dictionary = kh.Dictionary(json_data={"name": "Address", "key": ["Id"]})
110+
third_dictionary.add_variable(
111+
kh.Variable(json_data={"name": "StreetNumber", "type": "Numerical"})
112+
)
113+
third_dictionary.add_variable(
114+
kh.Variable(json_data={"name": "StreetName", "type": "Categorical"})
115+
)
116+
third_dictionary.add_variable(
117+
kh.Variable(json_data={"name": "id_city", "type": "Categorical"})
118+
)
119+
120+
# Adds the variables used in a Multi-tables context in the first dictionary.
121+
# They link the root dictionary to the additional ones
122+
root_dictionary.add_variable(
123+
kh.Variable(json_data={"name": "Services", "type": "Table(Service)"})
124+
)
125+
root_dictionary.add_variable(
126+
kh.Variable(json_data={"name": "Address", "type": "Entity(Address)"})
127+
)
128+
129+
# Creates a DictionaryDomain (set of dictionaries)
130+
dictionary_domain = kh.DictionaryDomain()
131+
dictionary_domain.add_dictionary(root_dictionary)
132+
dictionary_domain.add_dictionary(second_dictionary)
133+
dictionary_domain.add_dictionary(third_dictionary)
134+
135+
output_dir = os.path.join("kh_samples", "create_dictionary_domain")
136+
dictionary_file_path = os.path.join(output_dir, "dict_from_scratch.kdic")
137+
138+
# Create the output directory if needed
139+
if not os.path.isdir(output_dir):
140+
os.mkdir(output_dir)
141+
142+
# Writes the dictionary domain to a file
143+
with open(dictionary_file_path, "w") as report_file:
144+
report_file_writer = KhiopsOutputWriter(report_file)
145+
dictionary_domain.write(report_file_writer)
146+
147+
68148
def detect_data_table_format():
69149
"""Detects the format of a data table with and without a dictionary file
70150
@@ -1987,6 +2067,7 @@ def build_deployed_dictionary():
19872067
exported_samples = [
19882068
get_khiops_version,
19892069
build_dictionary_from_data_table,
2070+
create_dictionary_domain,
19902071
detect_data_table_format,
19912072
check_database,
19922073
export_dictionary_files,

0 commit comments

Comments
 (0)