From 1650f24e051de7227577b820787b73fdda698bed Mon Sep 17 00:00:00 2001 From: Arthit Suriyawongkul Date: Sat, 2 Nov 2024 08:36:44 +0000 Subject: [PATCH] Add pythainlp.tools.safe_print Handle Unicode print on console --- pythainlp/tools/__init__.py | 3 ++- pythainlp/tools/core.py | 20 ++++++++++++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/pythainlp/tools/__init__.py b/pythainlp/tools/__init__.py index 0dd01f9d7..a96d39c2f 100644 --- a/pythainlp/tools/__init__.py +++ b/pythainlp/tools/__init__.py @@ -6,10 +6,11 @@ "get_full_data_path", "get_pythainlp_data_path", "get_pythainlp_path", + "safe_print", "warn_deprecation", ] -from pythainlp.tools.core import warn_deprecation +from pythainlp.tools.core import safe_print, warn_deprecation from pythainlp.tools.path import ( PYTHAINLP_DEFAULT_DATA_DIR, get_full_data_path, diff --git a/pythainlp/tools/core.py b/pythainlp/tools/core.py index a9efc14ba..11da241b1 100644 --- a/pythainlp/tools/core.py +++ b/pythainlp/tools/core.py @@ -5,6 +5,7 @@ Generic support functions for PyThaiNLP. """ +import sys import warnings @@ -13,8 +14,7 @@ def warn_deprecation( replacing_func: str = "", version: str = "", ): - """ - Warn about the deprecation of a function. + """Warn about the deprecation of a function. :param str deprecated_func: Name of the deprecated function. :param str replacing_func: Name of the function to use instead (optional). @@ -28,3 +28,19 @@ def warn_deprecation( if replacing_func: message += f" Please use '{replacing_func}' instead." warnings.warn(message, DeprecationWarning, stacklevel=2) + + +def safe_print(text: str): + """Print text to console, handling UnicodeEncodeError. + + :param text: Text to print. + :type text: str + """ + try: + print(text) + except UnicodeEncodeError: + print( + text.encode(sys.stdout.encoding, errors="replace").decode( + sys.stdout.encoding + ) + )