<commit_before>from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import six
from jsonschema import validate as js_validate
import warnings
import uuid
import time as ttime
import pandas as pd
from ..utils import sanitize_np, apply_to_dict_recursively


class DatumNotFound(Exception):
    """
    Raised if a Datum id is not found.
    """
    def __init__(self, datum_id, *args):
        super().__init__(*args)
        self.datum_id = datum_id

    def __str__(self):
        return f"No datum found with datum id {self.datum_id}"


class EventDatumNotFound(Exception):
    """
    Raised if an Event document is found to have an unknown Datum id.
    """
    def __init__(self, event_uid, datum_id, *args):
        super().__init__(*args)
        self.event_uid = event_uid
        self.datum_id = datum_id

    def __str__(self):
        return (
            f"Event with uid {self.event_uid} references "
            f"unknown Datum with datum id {self.datum_id}"
        )


def doc_or_uid_to_uid(doc_or_uid):
    """Given Document or uid return the uid

    Parameters
    ----------
    doc_or_uid : dict or str
        If str, then assume uid and pass through, if not, return
        the 'uid' field

    Returns
    -------
    uid : str
        A string version of the uid of the given document

    """
    if not isinstance(doc_or_uid, six.string_types):
        doc_or_uid = doc_or_uid['uid']
    return doc_or_uid


def _get_datum_from_datum_id(col, datum_id, datum_cache, logger):
    try:
        datum = datum_cache[datum_id]
    except KeyError:
        # find the current document
        edoc = col.find_one({'datum_id': datum_id})
        if edoc is None:
            raise DatumNotFound(datum_id=datum_id)<commit_msg>replace __str__() with optional message parameter to __init__()<commit_after>from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

import six
from jsonschema import validate as js_validate
import warnings
import uuid
import time as ttime
import pandas as pd
from ..utils import sanitize_np, apply_to_dict_recursively


class DatumNotFound(Exception):
    """
    Raised if a Datum id is not found.
    """
    def __init__(self, datum_id, msg=None, *args):
        if msg is None:
            msg = f"No datum found with datum id {datum_id}"
        super().__init__(msg, *args)
        self.datum_id = datum_id


class EventDatumNotFound(Exception):
    """
    Raised if an Event document is found to have an unknown Datum id.
    """
    def __init__(self, event_uid, datum_id, msg=None, *args):
        if msg is None:
            msg = (
                f"Event with uid {event_uid} references "
                f"unknown Datum with datum id {datum_id}"
            )
        super().__init__(msg, *args)
        self.event_uid = event_uid
        self.datum_id = datum_id


def doc_or_uid_to_uid(doc_or_uid):
    """Given Document or uid return the uid

    Parameters
    ----------
    doc_or_uid : dict or str
        If str, then assume uid and pass through, if not, return
        the 'uid' field

    Returns
    -------
    uid : str
        A string version of the uid of the given document

    """
    if not isinstance(doc_or_uid, six.string_types):
        doc_or_uid = doc_or_uid['uid']
    return doc_or_uid


def _get_datum_from_datum_id(col, datum_id, datum_cache, logger):
    try:
        datum = datum_cache[datum_id]
    except KeyError:
        # find the current document
        edoc = col.find_one({'datum_id': datum_id})
        if edoc is None:
            raise DatumNotFound(datum_id=datum_id)
{'fraction_non_alphanumeric': 0.08444203095302742, 'fraction_numerical': 0.0, 'mean_word_length': 2.4787535410764874, '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': '6241758', 'n_tokens_mistral': 1174, 'n_tokens_neox': 1144, 'n_words': 354}