diff options
author | Nikita Slyusarev <nslus@yandex-team.com> | 2022-02-10 16:46:53 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:46:53 +0300 |
commit | 469afdc4e2587bf62ecdd096b75a0baa444c4012 (patch) | |
tree | 49e222ea1c5804306084bb3ae065bb702625360f /library/python/strings/strings.py | |
parent | cd77cecfc03a3eaf87816af28a33067c4f0cdb59 (diff) | |
download | ydb-469afdc4e2587bf62ecdd096b75a0baa444c4012.tar.gz |
Restoring authorship annotation for Nikita Slyusarev <nslus@yandex-team.com>. Commit 2 of 2.
Diffstat (limited to 'library/python/strings/strings.py')
-rw-r--r-- | library/python/strings/strings.py | 100 |
1 files changed, 50 insertions, 50 deletions
diff --git a/library/python/strings/strings.py b/library/python/strings/strings.py index cb903be611..5bfddfe78a 100644 --- a/library/python/strings/strings.py +++ b/library/python/strings/strings.py @@ -1,17 +1,17 @@ -import locale -import logging +import locale +import logging import six -import sys +import sys import codecs - + import library.python.func - + logger = logging.getLogger(__name__) - + DEFAULT_ENCODING = 'utf-8' -ENCODING_ERRORS_POLICY = 'replace' - +ENCODING_ERRORS_POLICY = 'replace' + def left_strip(el, prefix): """ @@ -20,29 +20,29 @@ def left_strip(el, prefix): if el.startswith(prefix): return el[len(prefix):] return el - - -# Explicit to-text conversion + + +# Explicit to-text conversion # Chooses between str/unicode, i.e. six.binary_type/six.text_type -def to_basestring(value): +def to_basestring(value): if isinstance(value, (six.binary_type, six.text_type)): - return value - try: + return value + try: if six.PY2: return unicode(value) else: return str(value) - except UnicodeDecodeError: - try: - return str(value) - except UnicodeEncodeError: - return repr(value) -to_text = to_basestring - - + except UnicodeDecodeError: + try: + return str(value) + except UnicodeEncodeError: + return repr(value) +to_text = to_basestring + + def to_unicode(value, from_enc=DEFAULT_ENCODING): if isinstance(value, six.text_type): - return value + return value if isinstance(value, six.binary_type): if six.PY2: return unicode(value, from_enc, ENCODING_ERRORS_POLICY) @@ -51,23 +51,23 @@ def to_unicode(value, from_enc=DEFAULT_ENCODING): return six.text_type(value) -# Optional from_enc enables transcoding +# Optional from_enc enables transcoding def to_str(value, to_enc=DEFAULT_ENCODING, from_enc=None): if isinstance(value, six.binary_type): - if from_enc is None or to_enc == from_enc: - # Unknown input encoding or input and output encoding are the same - return value - value = to_unicode(value, from_enc=from_enc) + if from_enc is None or to_enc == from_enc: + # Unknown input encoding or input and output encoding are the same + return value + value = to_unicode(value, from_enc=from_enc) if isinstance(value, six.text_type): - return value.encode(to_enc, ENCODING_ERRORS_POLICY) + return value.encode(to_enc, ENCODING_ERRORS_POLICY) return six.binary_type(value) - - + + def _convert_deep(x, enc, convert, relaxed=True): - if x is None: - return None + if x is None: + return None if isinstance(x, (six.text_type, six.binary_type)): - return convert(x, enc) + return convert(x, enc) if isinstance(x, dict): return {convert(k, enc): _convert_deep(v, enc, convert, relaxed) for k, v in six.iteritems(x)} if isinstance(x, list): @@ -82,15 +82,15 @@ def _convert_deep(x, enc, convert, relaxed=True): def unicodize_deep(x, enc=DEFAULT_ENCODING, relaxed=True): return _convert_deep(x, enc, to_unicode, relaxed) - - + + def stringize_deep(x, enc=DEFAULT_ENCODING, relaxed=True): return _convert_deep(x, enc, to_str, relaxed) - - + + @library.python.func.memoize() -def locale_encoding(): - try: +def locale_encoding(): + try: loc = locale.getdefaultlocale()[1] if loc: codecs.lookup(loc) @@ -98,17 +98,17 @@ def locale_encoding(): except LookupError as e: logger.debug('Cannot get system locale: %s', e) return None - except ValueError as e: + except ValueError as e: logger.warn('Cannot get system locale: %s', e) - return None - - -def fs_encoding(): - return sys.getfilesystemencoding() - - -def guess_default_encoding(): - enc = locale_encoding() + return None + + +def fs_encoding(): + return sys.getfilesystemencoding() + + +def guess_default_encoding(): + enc = locale_encoding() return enc if enc else DEFAULT_ENCODING |