Khiops Native Interface (KNI) Python wrapper using ctypes.
This module provides a Python interface to the Khiops Native Interface (KNI) C library, allowing direct deployment of Khiops models without temporary files.
class KNIError(Exception)Exception raised for KNI errors.
class KNI()Python wrapper for Khiops Native Interface using ctypes.
8 MB
def __init__(library_path: str | bytes | Path | None = None)Initialize the KNI wrapper.
Arguments:
library_path- Optional path to the KNI shared library (str, bytes, or pathlib.Path). If None, attempts to locate it automatically.
def get_version() -> intGet KNI version as integer (10*major + minor).
def get_full_version() -> strGet KNI full version string.
def set_log_file_name(log_file_name: str | bytes) -> NoneSet the log file name for error messages.
Arguments:
log_file_name- Path to log file (str or bytes, empty string for no logging)
Raises:
KNIError- If setting log file failsTypeError- If log_file_name is not str or bytes
def open_stream(dictionary_file_path: str | bytes | Path,
dictionary_name: str | bytes,
header_line: str | bytes,
field_separator: str | bytes = "\t") -> intOpen a KNI stream for recoding.
Arguments:
dictionary_file_path- Path to the dictionary file (str, bytes, or pathlib.Path)dictionary_name- Name of the dictionary to use (str or bytes)header_line- Header line with field names (str or bytes)field_separator- Character used to separate fields (str or bytes, default: tab)
Returns:
Stream handle (positive integer)
Raises:
KNIError- If opening stream failsTypeError- If arguments have invalid types
def close_stream(stream_handle: int) -> NoneClose a KNI stream.
Arguments:
stream_handle- Handle returned by open_stream
Raises:
KNIError- If closing stream failsTypeError- If stream_handle is not int
def recode_stream_record(stream_handle: int,
input_record: str | bytes,
max_output_length: int | None = None) -> strRecode an input record using the stream's dictionary.
Arguments:
stream_handle- Handle returned by open_stream (int)input_record- Input record (str or bytes)max_output_length- Maximum output buffer size (int, default: KNI_MaxRecordLength)
Returns:
Recoded output string
Raises:
KNIError- If recoding failsTypeError- If arguments have invalid types
def set_secondary_header_line(stream_handle: int, data_path: str | bytes,
header_line: str | bytes) -> NoneSet the header line of a secondary table (multi-table only).
Arguments:
stream_handle- Handle returned by open_streamdata_path- Data path identifying the secondary table (str or bytes)header_line- Header line with field names (str or bytes)
Raises:
KNIError- If setting secondary header failsTypeError- If arguments have invalid types
def set_external_table(stream_handle: int, data_root: str | bytes,
data_path: str | bytes,
data_table_file_name: str | bytes) -> NoneSet the name of a data file for an external table (multi-table only).
Arguments:
stream_handle- Handle returned by open_streamdata_root- Root dictionary of the external table (str or bytes)data_path- Data path for secondary external tables (str or bytes, empty for root)data_table_file_name- Path to the external table data file (str or bytes)
Raises:
KNIError- If setting external table failsTypeError- If arguments have invalid types
def finish_opening_stream(stream_handle: int) -> NoneFinish opening a stream (multi-table only).
Must be called after all secondary headers and external tables are set.
Arguments:
stream_handle- Handle returned by open_stream
Raises:
KNIError- If finishing opening stream failsTypeError- If stream_handle is not int
def set_secondary_input_record(stream_handle: int, data_path: str | bytes,
input_record: str | bytes) -> NoneSet a secondary input record for multi-table recoding.
All secondary records must be set before recoding the primary record.
Arguments:
stream_handle- Handle returned by open_streamdata_path- Data path identifying the secondary table (str or bytes)input_record- Secondary input record (str or bytes)
Raises:
KNIError- If setting secondary input record failsTypeError- If arguments have invalid types
def get_stream_max_memory() -> intGet the maximum amount of memory (in MB) for stream opening.
Returns:
Maximum memory in MB
def set_stream_max_memory(max_mb: int) -> intSet the maximum amount of memory (in MB) for stream opening.
Arguments:
max_mb- Maximum memory in MB
Returns:
Accepted value (bounded by system limits)
@staticmethod
def get_error_message(error_code: int) -> strGet a human-readable error message for an error code.
Arguments:
error_code- KNI error code
Returns:
Error message string