docs for muutils v0.8.8
View Source on GitHub

muutils.console_unicode


 1import locale
 2
 3
 4def get_console_safe_str(
 5    default: str,
 6    fallback: str,
 7) -> str:
 8    """Determine a console-safe string based on the preferred encoding.
 9
10    This function attempts to encode a given `default` string using the system's preferred encoding.
11    If encoding is successful, it returns the `default` string; otherwise, it returns a `fallback` string.
12
13    # Parameters:
14     - `default : str`
15        The primary string intended for use, to be tested against the system's preferred encoding.
16     - `fallback : str`
17        The alternative string to be used if `default` cannot be encoded in the system's preferred encoding.
18
19    # Returns:
20     - `str`
21        Either `default` or `fallback` based on whether `default` can be encoded safely.
22
23    # Usage:
24
25    ```python
26    >>> get_console_safe_str("café", "cafe")
27    "café"  # This result may vary based on the system's preferred encoding.
28    ```
29    """
30    try:
31        default.encode(locale.getpreferredencoding())
32        return default
33    except UnicodeEncodeError:
34        return fallback

def get_console_safe_str(default: str, fallback: str) -> str:
 5def get_console_safe_str(
 6    default: str,
 7    fallback: str,
 8) -> str:
 9    """Determine a console-safe string based on the preferred encoding.
10
11    This function attempts to encode a given `default` string using the system's preferred encoding.
12    If encoding is successful, it returns the `default` string; otherwise, it returns a `fallback` string.
13
14    # Parameters:
15     - `default : str`
16        The primary string intended for use, to be tested against the system's preferred encoding.
17     - `fallback : str`
18        The alternative string to be used if `default` cannot be encoded in the system's preferred encoding.
19
20    # Returns:
21     - `str`
22        Either `default` or `fallback` based on whether `default` can be encoded safely.
23
24    # Usage:
25
26    ```python
27    >>> get_console_safe_str("café", "cafe")
28    "café"  # This result may vary based on the system's preferred encoding.
29    ```
30    """
31    try:
32        default.encode(locale.getpreferredencoding())
33        return default
34    except UnicodeEncodeError:
35        return fallback

Determine a console-safe string based on the preferred encoding.

This function attempts to encode a given default string using the system's preferred encoding. If encoding is successful, it returns the default string; otherwise, it returns a fallback string.

Parameters:

  • default : str The primary string intended for use, to be tested against the system's preferred encoding.
  • fallback : str The alternative string to be used if default cannot be encoded in the system's preferred encoding.

Returns:

  • str Either default or fallback based on whether default can be encoded safely.

Usage:

>>> get_console_safe_str("café", "cafe")
"café"  # This result may vary based on the system's preferred encoding.