|
4 | 4 | """ |
5 | 5 | import re |
6 | 6 | import string |
| 7 | +from functools import wraps |
7 | 8 |
|
8 | 9 | from ..helpers.termsize import get_terminal_size |
9 | 10 |
|
10 | 11 |
|
11 | | -def _natural_key(text): |
| 12 | +def _natural_key(case_sensitive=False): |
12 | 13 | """ Key computation function for considering keys in a natural way. |
13 | 14 | |
14 | 15 | :param text: text to be used for computing the key |
15 | 16 | """ |
16 | | - tokens = [] |
17 | | - for s in re.split(r"(\d+|\D+)", text): |
18 | | - tokens.append(int(s) if s.isdigit() else s.lower()) |
19 | | - return tokens |
| 17 | + def _wrapper(text): |
| 18 | + return [(0, int(t)) if t.isdigit() else (1, t if case_sensitive else t.lower()) for t in \ |
| 19 | + re.split(r"(\d+|\D+)", text)] |
| 20 | + return _wrapper |
20 | 21 | string.natural_key = _natural_key |
21 | 22 |
|
22 | 23 |
|
23 | | -def sort_natural(strings): |
| 24 | +def sort_natural(strings, case_sensitive=False): |
24 | 25 | """ Simple function to sort a list of strings with numbers inside. |
25 | 26 | |
26 | 27 | :param strings: list of strings |
27 | 28 | """ |
28 | | - strings.sort(key=_natural_key) |
| 29 | + strings.sort(key=_natural_key(case_sensitive)) |
29 | 30 | string.sort_natural = sort_natural |
30 | 31 |
|
31 | 32 |
|
32 | | -def sorted_natural(lst): |
| 33 | +def sorted_natural(lst, case_sensitive=False): |
33 | 34 | """ Simple function to return a sorted list of strings with numbers inside. |
34 | 35 | |
35 | 36 | :param strings: list of strings |
36 | 37 | :return: list of strings sorted based on numbers inside |
37 | 38 | """ |
38 | | - return sorted(lst, key=_natural_key) |
| 39 | + return sorted(lst, key=_natural_key(case_sensitive)) |
39 | 40 | string.sorted_natural = sorted_natural |
40 | 41 |
|
41 | 42 |
|
|
0 commit comments