Sorting `key` function for performing a natural sort on a collection of strings See https://en.wikipedia.org/wiki/Natural_sort_order Parameters ---------- s : str A string that is an element of the collection being sorted Returns ------- tuple[str or i
(s: str)
| 1551 | |
| 1552 | |
| 1553 | def natural_sort_key(s: str) -> list[str | int]: |
| 1554 | """ |
| 1555 | Sorting `key` function for performing a natural sort on a collection of |
| 1556 | strings |
| 1557 | |
| 1558 | See https://en.wikipedia.org/wiki/Natural_sort_order |
| 1559 | |
| 1560 | Parameters |
| 1561 | ---------- |
| 1562 | s : str |
| 1563 | A string that is an element of the collection being sorted |
| 1564 | |
| 1565 | Returns |
| 1566 | ------- |
| 1567 | tuple[str or int] |
| 1568 | Tuple of the parts of the input string where each part is either a |
| 1569 | string or an integer |
| 1570 | |
| 1571 | Examples |
| 1572 | -------- |
| 1573 | >>> a = ['f0', 'f1', 'f2', 'f8', 'f9', 'f10', 'f11', 'f19', 'f20', 'f21'] |
| 1574 | >>> sorted(a) |
| 1575 | ['f0', 'f1', 'f10', 'f11', 'f19', 'f2', 'f20', 'f21', 'f8', 'f9'] |
| 1576 | >>> sorted(a, key=natural_sort_key) |
| 1577 | ['f0', 'f1', 'f2', 'f8', 'f9', 'f10', 'f11', 'f19', 'f20', 'f21'] |
| 1578 | """ |
| 1579 | return [int(part) if part.isdigit() else part for part in re.split(r"(\d+)", s)] |
| 1580 | |
| 1581 | |
| 1582 | def parse_bytes(s: float | str) -> int: |
no test coverage detected
searching dependent graphs…