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:

Name Type Description
tuple

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

Source code in chmpy/util/text.py
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:

Name Type Description
str str

the overlined string

Source code in chmpy/util/text.py
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:

Name Type Description
str str

the converted string

Source code in chmpy/util/text.py
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)