Skip to content

Add Python examples for KNI usage#1

Open
bruno-at-orange wants to merge 8 commits into
mainfrom
add-python-src
Open

Add Python examples for KNI usage#1
bruno-at-orange wants to merge 8 commits into
mainfrom
add-python-src

Conversation

@bruno-at-orange
Copy link
Copy Markdown
Member

@bruno-at-orange bruno-at-orange commented May 12, 2026

Add Python implementations demonstrating single-table and multi-table recoding with the Khiops Native Interface (KNI).

New files:

  • python/KNI.py: Complete ctypes wrapper for KhiopsNativeInterface library with automatic library discovery (KNI_HOME, system paths) Moved to the khiops repository
  • python/KNIRecodeFile.py: Single-table recoding example
  • python/KNIRecodeMTFiles.py: Multi-table recoding example with support for secondary tables and external tables

Features:

  • Cross-platform support (Windows, Linux, macOS)
  • Flexible library loading with multiple search strategies
  • Complete API coverage including multi-table operations
  • Command-line interface with argparse for multi-table example
  • Error handling with descriptive messages

Requirements: Python 3.6+ and KNI shared library installed

Add Python implementations demonstrating single-table and multi-table
recoding with the Khiops Native Interface (KNI).

New files:
- python/KNI.py: Complete ctypes wrapper for KhiopsNativeInterface library
  with automatic library discovery (KNI_HOME, system paths)
- python/KNIRecodeFile.py: Single-table recoding example
- python/KNIRecodeMTFiles.py: Multi-table recoding example with support for
  secondary tables and external tables

Features:
- Cross-platform support (Windows, Linux, macOS)
- Flexible library loading with multiple search strategies
- Complete API coverage including multi-table operations
- Command-line interface with argparse for multi-table example
- Error handling with descriptive messages

Requirements: Python 3.6+ and KNI shared library installed
@bruno-at-orange bruno-at-orange requested a review from popescu-v May 12, 2026 07:49
Comment thread python/KNI.py Outdated
Comment thread python/KNI.py Outdated
Comment thread python/KNI.py Outdated
Comment thread python/KNI.py Outdated
Comment thread python/KNI.py Outdated
Comment thread python/KNI.py Outdated
Comment thread python/KNI.py Outdated
Comment thread python/KNI.py Outdated
Comment thread python/KNI.py Outdated
Comment thread python/KNI.py Outdated
Comment thread python/KNI.py Outdated
Comment thread python/KNIRecodeFile.py Outdated
Comment thread python/KNIRecodeFile.py Outdated
Comment thread python/KNIRecodeFile.py Outdated
Comment thread python/KNIRecodeFile.py Outdated
Comment thread python/KNIRecodeFile.py Outdated
Comment thread python/KNIRecodeFile.py Outdated
Comment thread python/KNIRecodeMTFiles.py Outdated
Comment thread README.md
Comment thread README.md
Comment thread README.md Outdated
Copy link
Copy Markdown

@popescu-v popescu-v left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See the comments.
Generally, I would:

  1. type-check the input arguments of the Python API systematically, via is_instance and raise TypeError in case of erroneous types.
  2. favor raising exceptions rather than returning non-zero error codes in the Python API, to achieve a "least-surprise" API for Python users.
  3. systematically use argparse for dealing with input arguments to the example scripts.

This commit modernizes the Python KNI wrapper to follow Pythonic conventions
by using exceptions for error handling instead of C-style return codes.

Changes to KNI.py:
- Add KNIError exception class with error_code attribute
- Refactor all methods to raise KNIError on failure instead of returning error codes
- open_stream() now returns stream_handle directly (raises on error)
- recode_stream_record() now returns output string directly (no tuple)
- Add comprehensive type checking for all method parameters
- Extend all string parameters to accept both str and bytes types
- Simplify library path resolution to use KNI_HOME/ only

Changes to KNIRecodeFile.py:
- Import KNIError from KNI module
- Remove all error code checking (if ret_code != KNI.KNI_OK)
- Simplify to direct method calls with exception handling
- Fix field separator bug: change "\\t" to "\t" for proper tab character
- Add comprehensive exception handling in main()

Changes to KNIRecodeMTFiles.py:
- Import KNIError from KNI module
- Remove all error code checking throughout
- Simplify stream setup and record processing loops
- Clean exception handling throughout

Benefits:
- More Pythonic and easier to read
- Cleaner code with less boilerplate
- Better integration with Python's try/except patterns
- Type safety with parameter validation
- Support for both str and bytes in string parameters
Comment thread python/KNI.py Outdated
Comment thread python/KNI.py Outdated
Comment thread python/KNI.py Outdated
Comment thread python/KNI.py Outdated
Comment thread python/KNI.py Outdated
Comment thread python/KNI.py Outdated
Comment thread python/KNI.py Outdated
Comment thread python/KNI.py Outdated
Comment thread python/KNI.py Outdated
Comment thread python/KNI.py Outdated
Comment thread python/KNIRecodeFile.py Outdated
Comment thread python/KNIRecodeMTFiles.py Outdated
Comment thread README.md Outdated

## Requirements

- Python 3.9 or later
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would just require Python 3.10 and later, because 3.9 is not supported anymore.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems, this has not been pushed.

Copy link
Copy Markdown

@popescu-v popescu-v left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few pending comments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants