<commit_before>import os, os.path
import logging
import time
import errno
import hashlib
import tempfile
from contextlib import contextmanager
from functools import wraps


def reducehashdict(dict, keys):
    """pull the given keys out of the dictionary, return the reduced
    dictionary and the sha1 hash of that set of key values.
    """
    outdict= {}
    h = hashlib.sha1()
    #We go through in sorted order to ensure stability of the ordering
    #between runs.
    for k in sorted(keys):
        val = dict.get(k)
        if val is not None:
            h.update(k)
            h.update(str(val))
            outdict[k]=val

    if len(outdict):
        return outdict,h.hexdigest()
    else:
        return outdict,None

def reducedict(dict, keys):
    out = {}
    for k in keys:
        if dict.has_key(k):
            out[k] = dict[k]
    return out


def ensure_dir(dir, logger=None):
    if not os.path.exists(dir):
        try:
            if logger: logger.debug("Making dir: %s", dir)
            os.makedirs(dir)
            if logger: logger.info("Created dir: %s", dir)
        except OSError, e:
            #not entirely sure why we are getting errors,
            #http://docs.python.org/library/os.path.html#os.path.exists
            #says this could be related to not being able to call
            #os.stat, but I can do so from the command line python
            #just fine.
            if os.path.exists(dir):
                if logger: logger.debug("Erred, already exists: e.errno: %s",e.errno)
                return
            else:
                raise<commit_msg>hashdict: just hash a dictionary in a repeatable manner.<commit_after>import os, os.path
import logging
import time
import errno
import hashlib
import tempfile
from contextlib import contextmanager
from functools import wraps


def reducehashdict(dict, keys):
    """pull the given keys out of the dictionary, return the reduced
    dictionary and the sha1 hash of that set of key values.
    """
    outdict= {}
    h = hashlib.sha1()
    #We go through in sorted order to ensure stability of the ordering
    #between runs.
    for k in sorted(keys):
        val = dict.get(k)
        if val is not None:
            h.update(k)
            h.update(str(val))
            outdict[k]=val

    if len(outdict):
        return outdict,h.hexdigest()
    else:
        return outdict,None

def reducedict(dict, keys):
    out = {}
    for k in keys:
        if dict.has_key(k):
            out[k] = dict[k]
    return out

def hashdict(dict):
    h = hashlib.sha1()
    for k in sorted(dict.keys()):
        val = dict.get(k)
        if val is not None:
            h.update(k)
            h.update(str(val))
    return h.hexdigest()
        


def ensure_dir(dir, logger=None):
    if not os.path.exists(dir):
        try:
            if logger: logger.debug("Making dir: %s", dir)
            os.makedirs(dir)
            if logger: logger.info("Created dir: %s", dir)
        except OSError, e:
            #not entirely sure why we are getting errors,
            #http://docs.python.org/library/os.path.html#os.path.exists
            #says this could be related to not being able to call
            #os.stat, but I can do so from the command line python
            #just fine.
            if os.path.exists(dir):
                if logger: logger.debug("Erred, already exists: e.errno: %s",e.errno)
                return
            else:
                raise
{'fraction_non_alphanumeric': 0.09313725490196079, 'fraction_numerical': 0.0014417531718569781, 'mean_word_length': 1.9573742540494459, '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': '4975883', 'n_tokens_mistral': 1031, 'n_tokens_neox': 1007, 'n_words': 358}