Skip to content

utils

Various utiles used from the projects.

get_base_path()

Allows exposing the base path.

Source code in cat/utils.py
def get_base_path():
    """Allows exposing the base path."""
    return 'cat/'

get_base_url()

Allows exposing the base url.

Source code in cat/utils.py
def get_base_url():
    """Allows exposing the base url."""
    secure = os.getenv('CORE_USE_SECURE_PROTOCOLS', '')
    if secure != '':
        secure = 's'
    cat_host = os.getenv('CORE_HOST', 'localhost')
    cat_port = os.getenv('CORE_PORT', '1865')
    return f'http{secure}://{cat_host}:{cat_port}/'

get_plugins_path()

Allows exposing the plugins' path.

Source code in cat/utils.py
def get_plugins_path():
    """Allows exposing the plugins' path."""
    return os.path.join(get_base_path(), 'plugins/')

get_static_path()

Allows exposing the static files' path.

Source code in cat/utils.py
def get_static_path():
    """Allows exposing the static files' path."""
    return os.path.join(get_base_path(), 'static/')

get_static_url()

Allows exposing the static server url.

Source code in cat/utils.py
def get_static_url():
    """Allows exposing the static server url."""
    return get_base_url() + 'static/'

to_camel_case(text)

Format string to camel case.

Takes a string of words separated by either hyphens or underscores and returns a string of words in camel case.

Parameters:

Name Type Description Default
text str

String of hyphens or underscores separated words.

required

Returns:

Type Description
str

Camel case formatted string.

Source code in cat/utils.py
def to_camel_case(text: str) -> str:
    """Format string to camel case.

    Takes a string of words separated by either hyphens or underscores and returns a string of words in camel case.

    Parameters
    ----------
    text : str
        String of hyphens or underscores separated words.

    Returns
    -------
    str
        Camel case formatted string.
    """
    s = text.replace("-", " ").replace("_", " ").capitalize()
    s = s.split()
    if len(text) == 0:
        return text
    return s[0] + "".join(i.capitalize() for i in s[1:])

verbal_timedelta(td)

Convert a timedelta in human form.

The function takes a timedelta and converts it to a human-readable string format.

Parameters:

Name Type Description Default
td timedelta

Difference between two dates.

required

Returns:

Type Description
str

Human-readable string of time difference.

Notes

This method is used to give the Language Model information time information about the memories retrieved from the vector database.

Examples:

>>> print(verbal_timedelta(timedelta(days=2, weeks=1))
'One week and two days ago'
Source code in cat/utils.py
def verbal_timedelta(td: timedelta) -> str:
    """Convert a timedelta in human form.

    The function takes a timedelta and converts it to a human-readable string format.

    Parameters
    ----------
    td : timedelta
        Difference between two dates.

    Returns
    -------
    str
        Human-readable string of time difference.

    Notes
    -----
    This method is used to give the Language Model information time information about the memories retrieved from
    the vector database.

    Examples
    --------
    >>> print(verbal_timedelta(timedelta(days=2, weeks=1))
    'One week and two days ago'
    """

    if td.days != 0:
        abs_days = abs(td.days)
        if abs_days > 7:
            abs_delta = "{} weeks".format(td.days // 7)
        else:
            abs_delta = "{} days".format(td.days)
    else:
        abs_minutes = abs(td.seconds) // 60
        if abs_minutes > 60:
            abs_delta = "{} hours".format(abs_minutes // 60)
        else:
            abs_delta = "{} minutes".format(abs_minutes)
    if td < timedelta(0):
        return "{} ago".format(abs_delta)
    else:
        return "{} ago".format(abs_delta)