Skip to content

utils.py

colorize(value, is_warning) ¤

Utility to set a color for the output string when it exceeds the threshold.

Parameters:

Name Type Description Default
value str

String to be output.

required
is_warning bool

Whether it exceeds the threshold.

required

Returns:

Type Description
str

colorized string output

Source code in django_query_capture/utils.py
def colorize(value: str, is_warning: bool) -> str:
    """
    Utility to set a color for the output string when it exceeds the threshold.

    Args:
        value: String to be output.
        is_warning: Whether it exceeds the threshold.

    Returns:
        colorized string output
    """
    if is_warning:
        return termcolors.make_style(fg=get_config()["PRINT_THRESHOLDS"]["COLOR"])(  # type: ignore
            value
        )
    return value

get_stack_prefix(captured_query) ¤

Utilities that help you output call stacks consistently in CapturedQuery.

Parameters:

Name Type Description Default
captured_query CapturedQuery

CapturedQuery

required
Source code in django_query_capture/utils.py
def get_stack_prefix(captured_query: CapturedQuery) -> str:
    """
    Utilities that help you output call stacks consistently in [CapturedQuery][capture.CapturedQuery].
    Args:
        captured_query: [CapturedQuery][capture.CapturedQuery]
    """
    return f'[{captured_query["function_name"]}, {captured_query["file_name"]}:{captured_query["line_no"]}]'

truncate_string(value, length) ¤

Parameters:

Name Type Description Default
value str

String to be output.

required
length int

Number of strings to output.

required

Returns:

Type Description
str

truncated string

Source code in django_query_capture/utils.py
def truncate_string(value: str, length: int) -> str:
    """

    Args:
        value: String to be output.
        length: Number of strings to output.

    Returns:
        truncated string
    """
    return (value[:length] + "..") if len(value) > length else value