Skip to content

Latest commit

 

History

History
339 lines (204 loc) · 7.37 KB

File metadata and controls

339 lines (204 loc) · 7.37 KB

Table of Contents

kni

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.

KNIError Objects

class KNIError(Exception)

Exception raised for KNI errors.

KNI Objects

class KNI()

Python wrapper for Khiops Native Interface using ctypes.

KNI_MaxRecordLength

8 MB

__init__

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.

get_version

def get_version() -> int

Get KNI version as integer (10*major + minor).

get_full_version

def get_full_version() -> str

Get KNI full version string.

set_log_file_name

def set_log_file_name(log_file_name: str | bytes) -> None

Set 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 fails
  • TypeError - If log_file_name is not str or bytes

open_stream

def open_stream(dictionary_file_path: str | bytes | Path,
                dictionary_name: str | bytes,
                header_line: str | bytes,
                field_separator: str | bytes = "\t") -> int

Open 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 fails
  • TypeError - If arguments have invalid types

close_stream

def close_stream(stream_handle: int) -> None

Close a KNI stream.

Arguments:

  • stream_handle - Handle returned by open_stream

Raises:

  • KNIError - If closing stream fails
  • TypeError - If stream_handle is not int

recode_stream_record

def recode_stream_record(stream_handle: int,
                         input_record: str | bytes,
                         max_output_length: int | None = None) -> str

Recode 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 fails
  • TypeError - If arguments have invalid types

set_secondary_header_line

def set_secondary_header_line(stream_handle: int, data_path: str | bytes,
                              header_line: str | bytes) -> None

Set the header line of a secondary table (multi-table only).

Arguments:

  • stream_handle - Handle returned by open_stream
  • data_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 fails
  • TypeError - If arguments have invalid types

set_external_table

def set_external_table(stream_handle: int, data_root: str | bytes,
                       data_path: str | bytes,
                       data_table_file_name: str | bytes) -> None

Set the name of a data file for an external table (multi-table only).

Arguments:

  • stream_handle - Handle returned by open_stream
  • data_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 fails
  • TypeError - If arguments have invalid types

finish_opening_stream

def finish_opening_stream(stream_handle: int) -> None

Finish 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 fails
  • TypeError - If stream_handle is not int

set_secondary_input_record

def set_secondary_input_record(stream_handle: int, data_path: str | bytes,
                               input_record: str | bytes) -> None

Set 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_stream
  • data_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 fails
  • TypeError - If arguments have invalid types

get_stream_max_memory

def get_stream_max_memory() -> int

Get the maximum amount of memory (in MB) for stream opening.

Returns:

Maximum memory in MB

set_stream_max_memory

def set_stream_max_memory(max_mb: int) -> int

Set the maximum amount of memory (in MB) for stream opening.

Arguments:

  • max_mb - Maximum memory in MB

Returns:

Accepted value (bounded by system limits)

get_error_message

@staticmethod
def get_error_message(error_code: int) -> str

Get a human-readable error message for an error code.

Arguments:

  • error_code - KNI error code

Returns:

Error message string