Skip to content

List all possible variable types in class Variable docstring #452

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions khiops/core/dictionary.py
Original file line number Diff line number Diff line change
Expand Up @@ -971,6 +971,13 @@ class Variable:
True if the variable is used.
type : str
Variable type.
It can be either native (``Categorical``, ``Numerical``, ``Time``,
``Date``, ``Timestamp``, ``TimestampTZ``, ``Text``),
internal (``TextList``, ``Structure``)
- See https://khiops.org/11.0.0/api-docs/kdic/text-list-rules/
- See https://khiops.org/api-docs/kdic/structures-introduction/
or relational (``Entity`` - 0-1 relationship, ``Table`` - 0-n relationship)
- See https://khiops.org/tutorials/kdic_multi_table/
object_type : str
Type complement for the ``Table`` and ``Entity`` types.
structure_type : str
Expand All @@ -986,6 +993,11 @@ class Variable:
List of variable comments.
meta_data : `MetaData`
Variable metadata.

Examples
--------
See the following function of the ``samples.py`` documentation script:
- `samples.create_dictionary()`
"""

def __init__(self, json_data=None):
Expand Down
81 changes: 81 additions & 0 deletions khiops/samples/samples.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import khiops
import os
from khiops import core as kh
from khiops.core import KhiopsOutputWriter

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


def create_dictionary_domain():
"""Creates a dictionary domain (a set of dictionaries) from scratch
with all the possible variable types
"""
# Imports
import os
from khiops import core as kh

# Creates a Root dictionary
root_dictionary = kh.Dictionary(
json_data={"name": "dict_from_scratch", "root": True, "key": ["Id"]}
)

# Starts with simple variables to declare
simple_variables = [
{"name": "Id", "type": "Categorical"},
{"name": "Num", "type": "Numerical"},
{"name": "text", "type": "Text"},
{"name": "hour", "type": "Time"},
{"name": "date", "type": "Date"},
{"name": "ambiguous_ts", "type": "Timestamp"},
{"name": "ts", "type": "TimestampTZ"},
]
for var_spec in simple_variables:
var = kh.Variable()
var.name = var_spec["name"]
var.type = var_spec["type"]
root_dictionary.add_variable(var)

# Creates a second dictionary (not linked yet)
second_dictionary = kh.Dictionary(
json_data={"name": "Service", "key": ["Id", "id_product"]}
)
second_dictionary.add_variable(
kh.Variable(json_data={"name": "Id", "type": "Categorical"})
)
second_dictionary.add_variable(
kh.Variable(json_data={"name": "id_product", "type": "Categorical"})
)
# Creates a third dictionary (not linked yet)
third_dictionary = kh.Dictionary(json_data={"name": "Address", "key": ["Id"]})
third_dictionary.add_variable(
kh.Variable(json_data={"name": "StreetNumber", "type": "Numerical"})
)
third_dictionary.add_variable(
kh.Variable(json_data={"name": "StreetName", "type": "Categorical"})
)
third_dictionary.add_variable(
kh.Variable(json_data={"name": "id_city", "type": "Categorical"})
)

# Adds the variables used in a Multi-tables context in the first dictionary.
# They link the root dictionary to the additional ones
root_dictionary.add_variable(
kh.Variable(json_data={"name": "Services", "type": "Table(Service)"})
)
root_dictionary.add_variable(
kh.Variable(json_data={"name": "Address", "type": "Entity(Address)"})
)

# Creates a DictionaryDomain (set of dictionaries)
dictionary_domain = kh.DictionaryDomain()
dictionary_domain.add_dictionary(root_dictionary)
dictionary_domain.add_dictionary(second_dictionary)
dictionary_domain.add_dictionary(third_dictionary)

output_dir = os.path.join("kh_samples", "create_dictionary_domain")
dictionary_file_path = os.path.join(output_dir, "dict_from_scratch.kdic")

# Create the output directory if needed
if not os.path.isdir(output_dir):
os.mkdir(output_dir)

# Writes the dictionary domain to a file
with open(dictionary_file_path, "w") as report_file:
report_file_writer = KhiopsOutputWriter(report_file)
dictionary_domain.write(report_file_writer)


def detect_data_table_format():
"""Detects the format of a data table with and without a dictionary file

Expand Down Expand Up @@ -1987,6 +2067,7 @@ def build_deployed_dictionary():
exported_samples = [
get_khiops_version,
build_dictionary_from_data_table,
create_dictionary_domain,
detect_data_table_format,
check_database,
export_dictionary_files,
Expand Down
Loading