<commit_before># coding=utf-8
import navitiacommon.response_pb2 as response_pb2
from functools import partial
from datetime import datetime, timedelta
import logging


#compute the duration to get to the transport plus the transfers duration
def get_nontransport_duration(journey):
    sections = journey.sections
    current_duration = 0
    for section in sections:
        if section.type == response_pb2.STREET_NETWORK \
                or section.type == response_pb2.TRANSFER:
            current_duration += section.duration
    return current_duration


def has_fall_back_mode(journey, mode):
    return any(s.type == response_pb2.STREET_NETWORK and
            s.street_network.mode == mode for s in journey.sections)


def has_car(journey):
    return has_fall_back_mode(journey, response_pb2.Car)


def has_no_car(journey):
    return not has_fall_back_mode(journey, response_pb2.Car)


def has_bike(journey):
    return has_fall_back_mode(journey, response_pb2.Bike)


    # check if the journey has not public transport section
def non_pt_journey(journey):
    has_pt = all(section.type == response_pb2.STREET_NETWORK
                 for section in journey.sections)
    return has_pt


def min_from_criteria(journey_list, criteria):
    best = None
    for journey in journey_list:
        if best is None:
            best = journey
            continue
        for crit in criteria:
            val = crit(journey, best)
            #we stop at the first criterion that answers
            if val == 0:  # 0 means the criterion cannot decide
                continue
            if val > 0:
                best = journey
            break
    return best


# The caracteristic consists in 2 things :
# - a list of constraints
#   the constraints filter a sublist of journey
# - a list of optimisation critera
#   the criteria select the best journey in the sublist
class trip_carac:
    def __init__(self, constraints, criteria):
        self.constraints = constraints
        self.criteria = criteria


class and_filters:
    def __init__(self, filters):
        self.filters = filters

    def __call__(self, value):
        for f in self.filters:
            if not f(value):
                return False
        return True


class and_functors:
    def __init__(self, filters):
        self.filters = filters

    def __call__(self, v1, v2):
        for f in self.filters:
            val = f(v1, v2)
            if val != 0:
                return val
        return 1


def get_arrival_datetime(journey):
    return datetime.strptime(journey.arrival_date_time, "%Y%m%dT%H%M%S")


def get_departure_datetime(journey):
    return datetime.strptime(journey.arrival_date_time, "%Y%m%dT%H%M%S")


def choose_standard(journeys, best_criteria):
    standard = None
    for journey in journeys:
        car = has_car(journey)
        if standard is None or has_car(standard) and not car:
            standard = journey  # the standard shouldnt use the car if possible
            continue
        if not car and best_criteria(journey, standard) > 0:
            standard = journey
    return standard


#comparison of 2 fields. 0=>equality, 1=>1 better than 2
def compare_minus(field_1, field_2):
    if field_1 == field_2:
        return 0
    elif field_1 < field_2:
        return 1
    else:
        return -1

#criteria
transfers_crit = lambda j_1, j_2: compare_minus(j_1.nb_transfers, j_2.nb_transfers)


def arrival_crit(j_1, j_2):
    return j_1.arrival_date_time < j_2.arrival_date_time


def departure_crit(j_1, j_2):
    return j_1.departure_date_time > j_2.departure_date_time


def nonTC_crit(j_1, j_2):
    duration1 = get_nontransport_duration(j_1)
    duration2 = get_nontransport_duration(j_2)
    return compare_minus(duration1, duration2)


def duration_crit(j_1, j_2):
    return compare_minus(j_1.duration, j_2.duration)


def qualifier_one(journeys, request_type):

    if not journeys:
        logging.info("no journeys to qualify")
        return

    #The request type is made from the datetime_represents params of the request
    #If it's departure, we want the earliest arrival time
    #If it's arrival, we want the tardiest departure time
    best_crit = departure_crit if request_type == "arrival" else arrival_crit

    standard = choose_standard(journeys, and_functors([
        best_crit,
        duration_crit,<commit_msg>Jormungandr: fix best_crit in qualifier<commit_after># coding=utf-8
import navitiacommon.response_pb2 as response_pb2
from functools import partial
from datetime import datetime, timedelta
import logging


#compute the duration to get to the transport plus the transfers duration
def get_nontransport_duration(journey):
    sections = journey.sections
    current_duration = 0
    for section in sections:
        if section.type == response_pb2.STREET_NETWORK \
                or section.type == response_pb2.TRANSFER:
            current_duration += section.duration
    return current_duration


def has_fall_back_mode(journey, mode):
    return any(s.type == response_pb2.STREET_NETWORK and
            s.street_network.mode == mode for s in journey.sections)


def has_car(journey):
    return has_fall_back_mode(journey, response_pb2.Car)


def has_no_car(journey):
    return not has_fall_back_mode(journey, response_pb2.Car)


def has_bike(journey):
    return has_fall_back_mode(journey, response_pb2.Bike)


    # check if the journey has not public transport section
def non_pt_journey(journey):
    has_pt = all(section.type == response_pb2.STREET_NETWORK
                 for section in journey.sections)
    return has_pt


def min_from_criteria(journey_list, criteria):
    best = None
    for journey in journey_list:
        if best is None:
            best = journey
            continue
        for crit in criteria:
            val = crit(journey, best)
            #we stop at the first criterion that answers
            if val == 0:  # 0 means the criterion cannot decide
                continue
            if val > 0:
                best = journey
            break
    return best


# The caracteristic consists in 2 things :
# - a list of constraints
#   the constraints filter a sublist of journey
# - a list of optimisation critera
#   the criteria select the best journey in the sublist
class trip_carac:
    def __init__(self, constraints, criteria):
        self.constraints = constraints
        self.criteria = criteria


class and_filters:
    def __init__(self, filters):
        self.filters = filters

    def __call__(self, value):
        for f in self.filters:
            if not f(value):
                return False
        return True


class and_functors:
    def __init__(self, filters):
        self.filters = filters

    def __call__(self, v1, v2):
        for f in self.filters:
            val = f(v1, v2)
            if val != 0:
                return val
        return 1


def get_arrival_datetime(journey):
    return datetime.strptime(journey.arrival_date_time, "%Y%m%dT%H%M%S")


def get_departure_datetime(journey):
    return datetime.strptime(journey.arrival_date_time, "%Y%m%dT%H%M%S")


def choose_standard(journeys, best_criteria):
    standard = None
    for journey in journeys:
        car = has_car(journey)
        if standard is None or has_car(standard) and not car:
            standard = journey  # the standard shouldnt use the car if possible
            continue
        if not car and best_criteria(journey, standard) > 0:
            standard = journey
    return standard


#comparison of 2 fields. 0=>equality, 1=>1 better than 2
def compare_minus(field_1, field_2):
    if field_1 == field_2:
        return 0
    elif field_1 < field_2:
        return 1
    else:
        return -1

#criteria
transfers_crit = lambda j_1, j_2: compare_minus(j_1.nb_transfers, j_2.nb_transfers)


def arrival_crit(j_1, j_2):
    return j_1.arrival_date_time < j_2.arrival_date_time


def departure_crit(j_1, j_2):
    return j_1.departure_date_time > j_2.departure_date_time


def nonTC_crit(j_1, j_2):
    duration1 = get_nontransport_duration(j_1)
    duration2 = get_nontransport_duration(j_2)
    return compare_minus(duration1, duration2)


def duration_crit(j_1, j_2):
    return compare_minus(j_1.duration, j_2.duration)


def qualifier_one(journeys, request_type):

    if not journeys:
        logging.info("no journeys to qualify")
        return

    #The request type is made from the datetime_represents params of the request
    #If it's arrival, we want the earliest arrival time
    #If it's departure, we want the tardiest departure time
    best_crit = arrival_crit if request_type == "arrival" else departure_crit

    standard = choose_standard(journeys, and_functors([
        best_crit,
        duration_crit,
{'fraction_non_alphanumeric': 0.06781399589696832, 'fraction_numerical': 0.013676772281741509, 'mean_word_length': 2.840262582056893, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '249832', 'n_tokens_mistral': 2757, 'n_tokens_neox': 2648, 'n_words': 803}