From 519283d460e4797ca1376348b63f737749ccc016 Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Wed, 1 Aug 2018 03:08:22 -0400 Subject: Added matches_sorted member to support custom sorting order of tab-completion matches Made all sorting alphabetical Fixed case where extra slash was printing when tab completing users on Windows --- cmd2/utils.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'cmd2/utils.py') diff --git a/cmd2/utils.py b/cmd2/utils.py index d03e7f6f..d8afd922 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -144,3 +144,16 @@ def is_text_file(file_path: str) -> bool: pass return valid_text_file + + +def remove_duplicates(list_to_prune: List) -> List: + """ + 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 + """ + temp_dict = collections.OrderedDict() + for item in list_to_prune: + temp_dict[item] = None + + return list(temp_dict.keys()) -- cgit v1.2.1 From 47f963e4912ce8e765cf0f724e5d19f2cecd4bce Mon Sep 17 00:00:00 2001 From: Kevin Van Brunt Date: Wed, 1 Aug 2018 12:00:08 -0400 Subject: Added alphabetical_sort() function --- cmd2/utils.py | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'cmd2/utils.py') diff --git a/cmd2/utils.py b/cmd2/utils.py index d8afd922..d8bdf08d 100644 --- a/cmd2/utils.py +++ b/cmd2/utils.py @@ -157,3 +157,12 @@ def remove_duplicates(list_to_prune: List) -> List: temp_dict[item] = None return list(temp_dict.keys()) + + +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) -- cgit v1.2.1 From ca27fac72bf8e0b5885587b354b8fcb889f8ee79 Mon Sep 17 00:00:00 2001 From: Todd Leonhardt Date: Thu, 2 Aug 2018 20:54:37 -0700 Subject: 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 --- cmd2/utils.py | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) (limited to 'cmd2/utils.py') 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) -- cgit v1.2.1