Skip to content

Text

natural_sort_key(s, _nsre=re.compile('([a-zA-Z]+)(\\d+)'))

Utility function for sorting strings of the form A1, B_2, A12 etc. so that the suffixes will be in numeric order rather than lexicographical order.

Parameters:

Name Type Description Default
s str

the string whose sort key to determine

required

Returns:

Type Description
tuple

the (str, int) natural sort key for the provided string

Source code in chmpy/util/text.py
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
def natural_sort_key(s: str, _nsre=re.compile(r"([a-zA-Z]+)(\d+)")):
    """
    Utility function for sorting strings of the form A1, B_2, A12
    etc. so that the suffixes will be in numeric order rather than
    lexicographical order.

    Args:
        s (str): the string whose sort key to determine

    Returns:
        tuple: the (str, int) natural sort key for the provided string
    """
    m = _nsre.match(s)
    if not m:
        return s
    c, i = m.groups(0)
    return (c, int(i))

overline(x)

Add a unicode overline modifier to the provided string.

Parameters:

Name Type Description Default
x str

the string to be overlined

required

Returns:

Type Description
str

str: the overlined string

Source code in chmpy/util/text.py
88
89
90
91
92
93
94
95
96
97
98
99
def overline(x: str) -> str:
    """
    Add a unicode overline modifier
    to the provided string.

    Args:
        x (str): the string to be overlined

    Returns:
        str: the overlined string
    """
    return f"\u0305{x}"

subscript(x)

Convert the provided string to its subscript equivalent in unicode

Parameters:

Name Type Description Default
x str

the string to be converted

required

Returns:

Type Description
str

str: the converted string

Source code in chmpy/util/text.py
74
75
76
77
78
79
80
81
82
83
84
85
def subscript(x: str) -> str:
    """
    Convert the provided string to its subscript
    equivalent in unicode

    Args:
        x (str): the string to be converted

    Returns:
        str: the converted string
    """
    return SUBSCRIPT_MAP.get(x, x)