Add Python examples for KNI usage#1
Open
bruno-at-orange wants to merge 8 commits into
Open
Conversation
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
popescu-v
reviewed
May 12, 2026
popescu-v
reviewed
May 12, 2026
popescu-v
reviewed
May 12, 2026
popescu-v
reviewed
May 12, 2026
popescu-v
reviewed
May 12, 2026
popescu-v
reviewed
May 12, 2026
popescu-v
reviewed
May 12, 2026
popescu-v
reviewed
May 12, 2026
popescu-v
reviewed
May 12, 2026
popescu-v
reviewed
May 12, 2026
popescu-v
reviewed
May 12, 2026
popescu-v
reviewed
May 12, 2026
popescu-v
reviewed
May 12, 2026
popescu-v
reviewed
May 12, 2026
popescu-v
reviewed
May 12, 2026
popescu-v
reviewed
May 12, 2026
popescu-v
reviewed
May 12, 2026
popescu-v
reviewed
May 12, 2026
popescu-v
reviewed
May 12, 2026
popescu-v
reviewed
May 12, 2026
popescu-v
reviewed
May 12, 2026
popescu-v
requested changes
May 12, 2026
popescu-v
left a comment
There was a problem hiding this comment.
See the comments.
Generally, I would:
- type-check the input arguments of the Python API systematically, via
is_instanceand raiseTypeErrorin case of erroneous types. - favor raising exceptions rather than returning non-zero error codes in the Python API, to achieve a "least-surprise" API for Python users.
- systematically use
argparsefor 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
cf855eb to
83f9580
Compare
popescu-v
reviewed
May 18, 2026
popescu-v
reviewed
May 18, 2026
popescu-v
reviewed
May 18, 2026
popescu-v
reviewed
May 18, 2026
popescu-v
reviewed
May 18, 2026
popescu-v
reviewed
May 18, 2026
popescu-v
reviewed
May 18, 2026
popescu-v
reviewed
May 18, 2026
popescu-v
reviewed
May 18, 2026
popescu-v
reviewed
May 18, 2026
popescu-v
reviewed
May 19, 2026
popescu-v
reviewed
May 19, 2026
popescu-v
reviewed
May 19, 2026
|
|
||
| ## Requirements | ||
|
|
||
| - Python 3.9 or later |
There was a problem hiding this comment.
I would just require Python 3.10 and later, because 3.9 is not supported anymore.
a6ac308 to
0849cd0
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 thekhiopsrepositoryFeatures:
Requirements: Python 3.6+ and KNI shared library installed