import typing
import fnmatch


def filter_by_patterns(
    name: str,
    include_patterns: typing.Optional[typing.List[str]] = None,
    exclude_patterns: typing.Optional[typing.List[str]] = None,
):
    """
    Decide if a name should be included, given a set of include/exclude patterns. These are unix-like patterns,
    implemented with Python's fnmatch https://docs.python.org/3/library/fnmatch.html.

    Exclude patterns override include ones.

    :param name: The name to be filtered
    :param include_patterns: A list of wildcard patterns to match the name you want to include
    :param exclude_patterns: A list of wildcard patterns to match the name you want to exclude
    :return: True if it should be included, False otherwise
    """

    def _is_a_pattern(patterns: typing.List[str]):
        for pattern in patterns:
            if fnmatch.fnmatch(name, pattern):
                return True
        return False

    is_included = True
    is_excluded = False
    if include_patterns:
        is_included = _is_a_pattern(include_patterns)
    if exclude_patterns:
        is_excluded = _is_a_pattern(exclude_patterns)
    if is_excluded or not is_included:
        return False
    return True