diff options
author | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-08-02 20:54:37 -0700 |
---|---|---|
committer | Todd Leonhardt <todd.leonhardt@gmail.com> | 2018-08-02 20:54:41 -0700 |
commit | ca27fac72bf8e0b5885587b354b8fcb889f8ee79 (patch) | |
tree | 3437bd70131981ca74139760ef4b29a5517aea11 | |
parent | 9a5fb90f18e90dbecf3a33337bbda36403b2256d (diff) | |
download | cmd2-git-ca27fac72bf8e0b5885587b354b8fcb889f8ee79.tar.gz |
Addeded utility function for combining unicode normalization and casefolding into a single step
Also:
- Updated the alphabetical sort utility function to use this
- Started adding explicit unit tests for functions in utils.py
-rw-r--r-- | cmd2/utils.py | 22 | ||||
-rw-r--r-- | tests/test_utils.py | 16 |
2 files changed, 32 insertions, 6 deletions
diff --git a/cmd2/utils.py b/cmd2/utils.py index d8bdf08d..02956f6b 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -5,6 +5,7 @@ import collections import os from typing import Any, List, Optional, Union +import unicodedata from . import constants @@ -110,7 +111,7 @@ def which(editor: str) -> Optional[str]: def is_text_file(file_path: str) -> bool: - """Returns if a file contains only ASCII or UTF-8 encoded text + """Returns if a file contains only ASCII or UTF-8 encoded text. :param file_path: path to the file being checked :return: True if the file is a text file, False if it is binary. @@ -147,8 +148,8 @@ def is_text_file(file_path: str) -> bool: def remove_duplicates(list_to_prune: List) -> List: - """ - Removes duplicates from a list while preserving order of the items + """Removes duplicates from a list while preserving order of the items. + :param list_to_prune: the list being pruned of duplicates :return: The pruned list """ @@ -159,10 +160,19 @@ def remove_duplicates(list_to_prune: List) -> List: return list(temp_dict.keys()) -def alphabetical_sort(list_to_sort: List[str]) -> List[str]: +def norm_fold(astr: str) -> str: + """Normalize and casefold Unicode strings for saner comparisons. + + :param astr: input unicode string + :return: a normalized and case-folded version of the input string """ - Sorts a list of strings alphabetically + return unicodedata.normalize('NFC', astr).casefold() + + +def alphabetical_sort(list_to_sort: List[str]) -> List[str]: + """Sorts a list of strings alphabetically. + :param list_to_sort: the list being sorted :return: the sorted list """ - return sorted(list_to_sort, key=str.casefold) + return sorted(list_to_sort, key=norm_fold) diff --git a/tests/test_utils.py b/tests/test_utils.py new file mode 100644 index 00000000..55ff018b --- /dev/null +++ b/tests/test_utils.py @@ -0,0 +1,16 @@ +# coding=utf-8 +""" +Unit/functional testing for cmd2/utils.py module. + +Copyright 2018 Todd Leonhardt <todd.leonhardt@gmail.com> +Released under MIT license, see LICENSE file +""" +from colorama import Fore +import cmd2.utils as cu + + +def test_strip_ansi(): + base_str = 'Hello, world!' + ansi_str = Fore.GREEN + base_str + Fore.RESET + assert base_str != ansi_str + assert base_str == cu.strip_ansi(ansi_str) |