<commit_before><commit_msg>Initial commit (raw implementation)<commit_after>from bson import ObjectId


def fetch(collection, filter, last_oid_generation_time=None):
    if last_oid_generation_time is not None:
        last_oid = ObjectId.from_datetime(last_oid_generation_time)
        filter.update({"_id": {"$gte": last_oid}})
    return collection.find(filter)


def filter_duplicates(cursor, ids):
    for doc in cursor:
        if doc["_id"] not in ids:
            yield doc


def mongotail(collection, filter=None):
    if filter is None:
        filter = {}
    last_oid_generation_time = None
    last_batch_ids = set()
    while True:
        cursor = fetch(collection, filter, last_oid_generation_time)
        for doc in filter_duplicates(cursor, last_batch_ids):
            last_batch_ids = set()
            oid = doc["_id"]
            last_batch_ids.add(oid)
            last_oid_generation_time = doc["_id"].generation_time
            yield doc
{'fraction_non_alphanumeric': 0.08921161825726141, 'fraction_numerical': 0.0, 'mean_word_length': 3.071729957805907, 'pattern_counts': {'":': 2, '<': 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': '2071278', 'n_tokens_mistral': 297, 'n_tokens_neox': 283, 'n_words': 69}