Skip to content

strings

String manipulation functions.

add_spaces_before_caps(string)

Adds spaces before capital letters in a string if there is a lower-case letter following it.

Parameters:

Name Type Description Default
string str

The string to add spaces to.

required

Returns:

Type Description
str

The string with spaces added before capital letters.

Examples:

>>> add_spaces_before_caps("HelloWorld")
'Hello World'
>>> add_spaces_before_caps("HelloWorldAGAIN")
'Hello World AGAIN'
Source code in src/nhssynth/common/strings.py
def add_spaces_before_caps(string: str) -> str:
    """
    Adds spaces before capital letters in a string if there is a lower-case letter following it.

    Args:
        string: The string to add spaces to.

    Returns:
        The string with spaces added before capital letters.

    Examples:
        >>> add_spaces_before_caps("HelloWorld")
        'Hello World'
        >>> add_spaces_before_caps("HelloWorldAGAIN")
        'Hello World AGAIN'
    """
    return " ".join(re.findall(r"[a-z]?[A-Z][a-z]+|[A-Z]+(?=[A-Z][a-z]|\b)", string))

format_timedelta(start, finish)

Calculate and prettily format the difference between two calls to time.time().

Parameters:

Name Type Description Default
start float

The start time.

required
finish float

The finish time.

required

Returns:

Type Description
str

A string containing the time difference in a human-readable format.

Source code in src/nhssynth/common/strings.py
def format_timedelta(start: float, finish: float) -> str:
    """
    Calculate and prettily format the difference between two calls to `time.time()`.

    Args:
        start: The start time.
        finish: The finish time.

    Returns:
        A string containing the time difference in a human-readable format.
    """
    total = datetime.timedelta(seconds=finish - start)
    hours, remainder = divmod(total.seconds, 3600)
    minutes, seconds = divmod(remainder, 60)

    if total.days > 0:
        delta_str = f"{total.days}d {hours}h {minutes}m {seconds}s"
    elif hours > 0:
        delta_str = f"{hours}h {minutes}m {seconds}s"
    elif minutes > 0:
        delta_str = f"{minutes}m {seconds}s"
    else:
        delta_str = f"{seconds}s"
    return delta_str