<commit_before>from sqlalchemy.orm import sessionmaker

from delivery.models.db_models import DeliveryOrder


class BaseDeliveriesRepository(object):
    """
    Provides interface that needs to be supported by all subclasses
    """

    def get_delivery_orders_for_source(self, source_directory):
        raise NotImplementedError("Must be implemented by subclass")

    def get_ongoing_delivery_order_for_source(self, source_directory):
        raise NotImplementedError("Must be implemented by subclass")

    def get_delivery_order_by_id(self, delivery_order_id):
        raise NotImplementedError("Must be implemented by subclass")

    def get_delivery_orders(self):
        raise NotImplementedError("Must be implemented by subclass")

    def add_delivery_order(self, delivery_order):
        raise NotImplementedError("Must be implemented by subclass")


class DatabaseBasedDeliveriesRepository(BaseDeliveriesRepository):

    def __init__(self, session_factory):
        self.session = session_factory()

    def get_delivery_orders_for_source(self, source_directory):
        return self.session.query(DeliveryOrder).filter(DeliveryOrder.delivery_source == source_directory).all()

    def get_delivery_order_by_id(self, delivery_order_id):
        return self.session.query(DeliveryOrder).filter(DeliveryOrder.id == delivery_order_id).one()

    def get_delivery_orders(self):
        return self.session.query(DeliveryOrder).all()

    def create_delivery_order(self, delivery_source, delivery_project, delivery_status, staging_order_id):<commit_msg>Better handling of no found delivery with id<commit_after>
from sqlalchemy.orm.exc import NoResultFound


class BaseDeliveriesRepository(object):
    """
    Provides interface that needs to be supported by all subclasses
    """

    def get_delivery_orders_for_source(self, source_directory):
        raise NotImplementedError("Must be implemented by subclass")

    def get_ongoing_delivery_order_for_source(self, source_directory):
        raise NotImplementedError("Must be implemented by subclass")

    def get_delivery_order_by_id(self, delivery_order_id):
        raise NotImplementedError("Must be implemented by subclass")

    def get_delivery_orders(self):
        raise NotImplementedError("Must be implemented by subclass")

    def add_delivery_order(self, delivery_order):
        raise NotImplementedError("Must be implemented by subclass")


class DatabaseBasedDeliveriesRepository(BaseDeliveriesRepository):

    def __init__(self, session_factory):
        self.session = session_factory()

    def get_delivery_orders_for_source(self, source_directory):
        return self.session.query(DeliveryOrder).filter(DeliveryOrder.delivery_source == source_directory).all()

    def get_delivery_order_by_id(self, delivery_order_id):
        """
        Get the delivery order matching the given id
        :param delivery_order_id: to search for
        :return: the matching delivery order, or None, if no order was found matchin id
        """
        try:
            return self.session.query(DeliveryOrder).filter(DeliveryOrder.id == delivery_order_id).one()
        except NoResultFound:
            return None

    def get_delivery_orders(self):
        return self.session.query(DeliveryOrder).all()

    def create_delivery_order(self, delivery_source, delivery_project, delivery_status, staging_order_id):
{'fraction_non_alphanumeric': 0.07214369846878681, 'fraction_numerical': 0.0, 'mean_word_length': 4.523577235772358, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '6262876', 'n_tokens_mistral': 944, 'n_tokens_neox': 903, 'n_words': 221}