def patsy_categorical_term(
        term,
        levels=None,
        coding=None,
        quote=True):
    """Returns categorical term for patsy.
        Optionally specify levels, coding, and quote the term
    :param term: str
        name of the categorical variable
    :param levels: list(str) or None
        levels for the categorical variable
    :param coding: str
        A valid coding. E.g. Treatment, Sum, Diff, Poly
        https://patsy.readthedocs.io/en/latest/API-reference.html#handling-categorical-data
    :param quote: bool
        whether to quote the term. Useful if there is a space or "." in the term
    :return: str
        categorical factor for patsy model formula
    """
    if quote:
        term = f"Q('{term}')"
    # constructs the string for the patsy model term
    string = f"C({term}"
    if coding is not None:
        string += f", {coding}"
    if levels is not None:
        string += f", levels={levels}"
    string += ")"
    return string