def filter_hosts_by_os(hosts, os_family=None, os_gen=None):
    """
    This function creates a list of NmapHost objects which matches the given filters. You either can specify an os
    family or a os generation or both. The parameter os_family has to be an exact string like "Windows" or "Linux" and
    the parameter os_gen should be a string like "7", "8" or "2.4.X".

    :param hosts: as list of NmapHost objects
    :type hosts: list of NmapHost objects

    :param os_family: the os family filter string
    :type os_family: str

    :param os_gen: the os generation filter string
    :type os_gen: str

    :return: list of NmapHost objects
    """
    if os_family is None and os_gen is None:
        return hosts
    result = []
    for host in hosts:
        os_matches = host.os_match_probabilities()
        found = False
        for match in os_matches:
            for osc in match.osclasses:
                if os_family is not None and os_gen is not None:
                    if osc.osgen == os_gen and osc.osfamily == os_family:
                        found = True
                        break
                elif os_gen is not None:
                    if osc.osgen == os_gen:
                        found = True
                elif os_family is not None:
                    if osc.osfamily == os_family:
                        found = True
        if found:
            result.append(host)
    return result
