<commit_before>from datetime import datetime, timedelta

from rest_framework.exceptions import ParseError
import yaml

def parse_date(date_param):
    """
    Parse the date string parameter
    :param date_param:
        Either a keyword:
            'today', 'tomorrow'
        or a string in the format 'dd/mm/yy'
    :return:
        datetime object
    """
    if not date_param:
        return None
    elif date_param == "today":
        from_date = datetime.today().date()
    elif date_param == "tomorrow":
        from_date = datetime.today().date() + timedelta(1)
    else:
        try:
            # TODO should probably test multiple formats, especially YYYY
            from_date = datetime.strptime(date_param, "%d/%m/%y")
        except Exception as e:
            # catch the exception and raise an API exception instead, which the user will see
            raise ParseError(e.message)
            # TODO should raised a more specialised error than rest framework.
    return from_date


def read_yaml_param(fname, key):
    fullname = fname + ".yaml"
    try:
        stream = open(fullname, "r")
        doc = yaml.load(stream)
        stream.close()
        value = doc[key]
    except IOError:
        print "Failed to load file:", fullname, ".yaml"
        return ""
    except KeyError:
        print "Failed to find key", key, "in file", fullname, ".yaml"
        value = ""

    return value<commit_msg>amend log messages.<commit_after>from datetime import datetime, timedelta

from rest_framework.exceptions import ParseError
import yaml

def parse_date(date_param):
    """
    Parse the date string parameter
    :param date_param:
        Either a keyword:
            'today', 'tomorrow'
        or a string in the format 'dd/mm/yy'
    :return:
        datetime object
    """
    if not date_param:
        return None
    elif date_param == "today":
        from_date = datetime.today().date()
    elif date_param == "tomorrow":
        from_date = datetime.today().date() + timedelta(1)
    else:
        try:
            # TODO should probably test multiple formats, especially YYYY
            from_date = datetime.strptime(date_param, "%d/%m/%y")
        except Exception as e:
            # catch the exception and raise an API exception instead, which the user will see
            raise ParseError(e.message)
            # TODO should raised a more specialised error than rest framework.
    return from_date


def read_yaml_param(fname, key):
    fullname = fname + ".yaml"
    try:
        stream = open(fullname, "r")
        doc = yaml.load(stream)
        stream.close()
        value = doc[key]
    except IOError:
        print "Failed to load file:", fullname
        return ""
    except KeyError:
        print "Failed to find key", key, "in file", fullname
        value = ""

    return value
{'fraction_non_alphanumeric': 0.09036568213783404, 'fraction_numerical': 0.0007032348804500703, 'mean_word_length': 2.2626146788990824, 'pattern_counts': {'":': 4, '<': 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': '4697370', 'n_tokens_mistral': 798, 'n_tokens_neox': 777, 'n_words': 271}