Skip to content

Commit ca27fac

Browse files
committed
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
1 parent 9a5fb90 commit ca27fac

File tree

2 files changed

+32
-6
lines changed

2 files changed

+32
-6
lines changed

cmd2/utils.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import collections
66
import os
77
from typing import Any, List, Optional, Union
8+
import unicodedata
89

910
from . import constants
1011

@@ -110,7 +111,7 @@ def which(editor: str) -> Optional[str]:
110111

111112

112113
def is_text_file(file_path: str) -> bool:
113-
"""Returns if a file contains only ASCII or UTF-8 encoded text
114+
"""Returns if a file contains only ASCII or UTF-8 encoded text.
114115
115116
:param file_path: path to the file being checked
116117
: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:
147148

148149

149150
def remove_duplicates(list_to_prune: List) -> List:
150-
"""
151-
Removes duplicates from a list while preserving order of the items
151+
"""Removes duplicates from a list while preserving order of the items.
152+
152153
:param list_to_prune: the list being pruned of duplicates
153154
:return: The pruned list
154155
"""
@@ -159,10 +160,19 @@ def remove_duplicates(list_to_prune: List) -> List:
159160
return list(temp_dict.keys())
160161

161162

162-
def alphabetical_sort(list_to_sort: List[str]) -> List[str]:
163+
def norm_fold(astr: str) -> str:
164+
"""Normalize and casefold Unicode strings for saner comparisons.
165+
166+
:param astr: input unicode string
167+
:return: a normalized and case-folded version of the input string
163168
"""
164-
Sorts a list of strings alphabetically
169+
return unicodedata.normalize('NFC', astr).casefold()
170+
171+
172+
def alphabetical_sort(list_to_sort: List[str]) -> List[str]:
173+
"""Sorts a list of strings alphabetically.
174+
165175
:param list_to_sort: the list being sorted
166176
:return: the sorted list
167177
"""
168-
return sorted(list_to_sort, key=str.casefold)
178+
return sorted(list_to_sort, key=norm_fold)

tests/test_utils.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# coding=utf-8
2+
"""
3+
Unit/functional testing for cmd2/utils.py module.
4+
5+
Copyright 2018 Todd Leonhardt <[email protected]>
6+
Released under MIT license, see LICENSE file
7+
"""
8+
from colorama import Fore
9+
import cmd2.utils as cu
10+
11+
12+
def test_strip_ansi():
13+
base_str = 'Hello, world!'
14+
ansi_str = Fore.GREEN + base_str + Fore.RESET
15+
assert base_str != ansi_str
16+
assert base_str == cu.strip_ansi(ansi_str)

0 commit comments

Comments
 (0)