Skip to content

khiops.core.internals.io

Submodule of khiops.core.internals

Classes to handle Khiops specific I/O

CLASS DESCRIPTION
KhiopsJSONObject

Represents the contents of a Khiops JSON file

KhiopsOutputWriter

Output writer with additional services to handle Khiops special encodings

FUNCTION DESCRIPTION
encode_file_path

Encodes a file path

flexible_json_load

Loads flexibly a JSON file

Classes

KhiopsJSONObject(json_data=None)

Represents the contents of a Khiops JSON file

PARAMETER DESCRIPTION
json_data

Python dictionary representing the data of a Khiops JSON file. If None or empty it returns an empty object.

TYPE: dict DEFAULT: None

RAISES DESCRIPTION
KhiopsJSONError

If the JSON data is invalid.

ATTRIBUTE DESCRIPTION
tool

Name of the Khiops tool that generated the file.

TYPE: str

version

Version of the Khiops tool that generated the file.

TYPE: str

khiops_encoding

Custom encoding used by Khiops in the file. Valid values: - None : for backwards compatibility - "ascii": ASCII encoding - "ansi": ANSI encoding - "utf8": UTF-8 encoding - "mixed_ansi_utf8": Mixed characters from UTF-8 and ANSI but no collision. - "colliding_ansi_utf8" : Colliding characters from UTF-8 and ANSI.

TYPE: (str, optional)

sub_tool

Identifies the tool that originated the JSON file. Used by tools of the Khiops family such as PataText or Enneade.

TYPE: (str, optional)

json_key_sort_spec

Dictionary that specifies the order of the keys in the Khiops JSON report. Its values are None, except when they are dictionaries themselves.

Note

This is a class attribute that can be set in subclasses, to specify a key order when serializing the report in a JSON file, via the write_khiops_json_file method.

TYPE: (dict, optional)

json_data

Python dictionary extracted from the Khiops JSON report file. Deprecated will be removed in Khiops 12.

TYPE: dict

See class docstring

METHOD DESCRIPTION
create_output_file_writer

Creates an output file with the proper encoding settings

to_dict

Serialize object instance to the Khiops JSON format

write_khiops_json_file

Write the JSON data of the object to a Khiops JSON file

Methods:

create_output_file_writer(stream)

Creates an output file with the proper encoding settings

PARAMETER DESCRIPTION
stream

An output stream object.

TYPE: IOBase

RETURNS DESCRIPTION
KhiopsOutputWriter

An output file object.

to_dict()

Serialize object instance to the Khiops JSON format

write_khiops_json_file(json_file_path, _ensure_ascii=False)

Write the JSON data of the object to a Khiops JSON file

The JSON keys are sorted according to the KhiopsJSONObject.json_key_sort_spec class attribute, if set. Otherwise, the JSON keys are not sorted.

PARAMETER DESCRIPTION
json_file_path

Path to the Khiops JSON file.

TYPE: str

_ensure_ascii

If True, then non-ASCII characters in the report are escaped. Otherwise, they are dumped as-is.

TYPE: bool DEFAULT: False

KhiopsOutputWriter(stream, force_ansi=False, ansi_unicode_chars=None)

Output writer with additional services to handle Khiops special encodings

PARAMETER DESCRIPTION
stream

A writable output stream. Special text transformations in buffers inheriting from io.TextIOBase are ignored.

TYPE: IOBase

force_ansi

All output written will be transformed back ANSI characters in that range that were recoded to UTF-8.

TYPE: bool DEFAULT: False

ansi_unicode_chars

A list of UTF-8 characters with equivalents in the ANSI 128-256 range which will be encoded back to ANSI when writing with force_ansi is True. By default all UTF-8 equivalents of the ANSI 128-256 will be encoded back.

TYPE: list of str DEFAULT: None

Functions:

encode_file_path(file_path)

Encodes a file path

This is custom path encoding for Khiops scenarios that is platform dependent. The encoding is done only if file_path is of type str.

PARAMETER DESCRIPTION
file_path

The path of a file.

TYPE: str or bytes

RETURNS DESCRIPTION
bytes

If file_path is str - In Windows : The path decoded to UTF-8 excepting the "ANSI" Unicode characters. - In Linux/Unix/Mac : The path decoded to UTF-8. If file_path is bytes: It just returns the input file_path

RAISES DESCRIPTION
TypeError

If file_path is not str or bytes

flexible_json_load(json_file_path)

Loads flexibly a JSON file

First it tries a vanilla read, then if that fails it warns and then loads the files replacing the errors.

PARAMETER DESCRIPTION
json_file_path

Path of the Khiops JSON file.

TYPE: str

RETURNS DESCRIPTION
dict

The in-memory representation of the JSON file.