Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pythainlp/tools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
20 changes: 18 additions & 2 deletions pythainlp/tools/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
Generic support functions for PyThaiNLP.
"""

import sys
import warnings


Expand All @@ -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).
Expand All @@ -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
)
)
Loading