コード例 #1
0
def refine_with_housing_criteria(flats_list, constraint):
    """
    Filter a list of flats according to criteria.

    Housings posts websites tend to return broader results that what was
    actually asked for. Then, we should filter out the list to match the
    user criteria, and avoid exposing unwanted flats.

    :param flats_list: A list of flats dict to filter.
    :param constraint: The constraint that the ``flats_list`` should satisfy.
    :param config: A config dict.
    :return: A tuple of flats to keep and flats to delete.
    """
    # For each flat, the associated `is_ok` value indicate whether it should be
    # kept or discarded.
    is_ok = [True for _ in flats_list]

    for i, flat in enumerate(flats_list):
        # Check postal code
        postal_code = flat["flatisfy"].get("postal_code", None)
        if (postal_code and postal_code not in constraint["postal_codes"]):
            LOGGER.info("Postal code for flat %s is out of range.", flat["id"])
            is_ok[i] = is_ok[i] and False

        # Check time_to
        for place_name, time in flat["flatisfy"].get("time_to", {}).items():
            time = time["time"]
            is_within_interval = tools.is_within_interval(
                time, *(constraint["time_to"][place_name]["time"]))
            if not is_within_interval:
                LOGGER.info("Flat %s is too far from place %s: %ds.",
                            flat["id"], place_name, time)
            is_ok[i] = is_ok[i] and is_within_interval

        # Check other fields
        for field in ["area", "cost", "rooms", "bedrooms"]:
            interval = constraint[field]
            is_within_interval = tools.is_within_interval(
                flat.get(field, None), *interval)
            if not is_within_interval:
                LOGGER.info("%s for flat %s is out of range.",
                            field.capitalize(), flat["id"])
            is_ok[i] = is_ok[i] and is_within_interval

        # Check number of pictures
        print(constraint['minimum_pictures'], len(flat['photos']))
        if not (constraint['minimum_pictures'] <= len(flat['photos'])):
            is_ok[i] = False

    return ([flat for i, flat in enumerate(flats_list) if is_ok[i]],
            [flat for i, flat in enumerate(flats_list) if not is_ok[i]])
コード例 #2
0
def refine_with_details_criteria(flats_list, constraint):
    """
    Filter a list of flats according to the criteria which require the full
    details to be fetched. These include minimum number of photos and terms
    that should appear in description.

    .. note ::

        This has to be done in a separate function and not with the other
        criterias as photos and full description are only fetched in the second
        pass.

    :param flats_list: A list of flats dict to filter.
    :param constraint: The constraint that the ``flats_list`` should satisfy.
    :return: A tuple of flats to keep and flats to delete.
    """
    # For each flat, the associated `is_ok` value indicate whether it should be
    # kept or discarded.
    is_ok = [True for _ in flats_list]

    for i, flat in enumerate(flats_list):
        # Check number of pictures
        has_enough_photos = tools.is_within_interval(
            len(flat.get('photos', [])), constraint['minimum_nb_photos'], None)
        if not has_enough_photos:
            LOGGER.info(
                "Flat %s only has %d photos, it should have at least %d.",
                flat["id"], len(flat['photos']),
                constraint['minimum_nb_photos'])
            is_ok[i] = False

        has_all_good_terms_in_description = True
        if constraint["description_should_contain"]:
            has_all_good_terms_in_description = all(
                term in flat['text']
                for term in constraint["description_should_contain"])

        has_a_bad_term_in_description = False
        if constraint["description_should_not_contain"]:
            has_a_bad_term_in_description = any(
                term in flat['text']
                for term in constraint["description_should_not_contain"])

        if (not has_all_good_terms_in_description
                or has_a_bad_term_in_description):
            LOGGER.info(
                ("Description for flat %s does not contain all the required "
                 "terms, or contains a blacklisted term."), flat["id"])
            is_ok[i] = False

    return ([flat for i, flat in enumerate(flats_list) if is_ok[i]],
            [flat for i, flat in enumerate(flats_list) if not is_ok[i]])
コード例 #3
0
ファイル: __init__.py プロジェクト: Phyks/Flatisfy
def refine_with_details_criteria(flats_list, constraint):
    """
    Filter a list of flats according to the criteria which require the full
    details to be fetched. These include minimum number of photos and terms
    that should appear in description.

    .. note ::

        This has to be done in a separate function and not with the other
        criterias as photos and full description are only fetched in the second
        pass.

    :param flats_list: A list of flats dict to filter.
    :param constraint: The constraint that the ``flats_list`` should satisfy.
    :return: A tuple of flats to keep and flats to delete.
    """
    # For each flat, the associated `is_ok` value indicate whether it should be
    # kept or discarded.
    is_ok = [True for _ in flats_list]

    for i, flat in enumerate(flats_list):
        # Check number of pictures
        has_enough_photos = tools.is_within_interval(
            len(flat.get("photos", [])), constraint["minimum_nb_photos"], None)
        if not has_enough_photos:
            LOGGER.info(
                "Flat %s only has %d photos, it should have at least %d.",
                flat["id"],
                len(flat["photos"]),
                constraint["minimum_nb_photos"],
            )
            is_ok[i] = False

        for term in constraint["description_should_contain"]:
            if isinstance(term,
                          str) and term.lower() not in flat["text"].lower():
                LOGGER.info(
                    ("Description for flat %s does not contain required term '%s'."
                     ),
                    flat["id"],
                    term,
                )
                is_ok[i] = False
            elif isinstance(term, list) and all(
                    x.lower() not in flat["text"].lower() for x in term):
                LOGGER.info(
                    ("Description for flat %s does not contain any of required terms '%s'."
                     ),
                    flat["id"],
                    term,
                )
                is_ok[i] = False
        for term in constraint["description_should_not_contain"]:
            if term.lower() in flat["text"].lower():
                LOGGER.info(
                    ("Description for flat %s contains blacklisted term '%s'."
                     ),
                    flat["id"],
                    term,
                )
                is_ok[i] = False

    return (
        [flat for i, flat in enumerate(flats_list) if is_ok[i]],
        [flat for i, flat in enumerate(flats_list) if not is_ok[i]],
    )