<commit_before>import contextlib, os, tempfile
from . import log

DEFAULT_PID_FILENAME = os.path.join(tempfile.gettempdir(), 'bp_pid_file.txt')


@contextlib.contextmanager
def pid_context(pid_filename):
    """
    For the duration of this context manager, put the PID for this process into
    `pid_filename`, and then remove the file at the end.
    """
    if os.path.exists(pid_filename):
        contents = open(pid_filename).read(16)
        log.warning('pid_filename %s already exists with contents %s...',
                    pid_filename, contents)

    with open(pid_filename, 'w') as fp:
        fp.write(str(os.getpid()))
        fp.write('\n')

    try:
        yield
    finally:
        try:
            os.remove(pid_filename)
        except Exception as e:
            log.error('Got an exception %s deleting the pid_filename %s',
                      e, pid_filename)


def get_pid(pid_filename):
    """
    Return the integer PID for the current bp process, or raise an exception if<commit_msg>Use DEFAULT_PID_FILENAME as default in pid_context functions<commit_after>import contextlib, os, tempfile
from . import log

DEFAULT_PID_FILENAME = os.path.join(tempfile.gettempdir(), 'bp_pid_file.txt')


@contextlib.contextmanager
def pid_context(pid_filename=DEFAULT_PID_FILENAME):
    """
    For the duration of this context manager, put the PID for this process into
    `pid_filename`, and then remove the file at the end.
    """
    if os.path.exists(pid_filename):
        contents = open(pid_filename).read(16)
        log.warning('pid_filename %s already exists with contents %s...',
                    pid_filename, contents)

    with open(pid_filename, 'w') as fp:
        fp.write(str(os.getpid()))
        fp.write('\n')

    try:
        yield
    finally:
        try:
            os.remove(pid_filename)
        except Exception as e:
            log.error('Got an exception %s deleting the pid_filename %s',
                      e, pid_filename)


def get_pid(pid_filename=DEFAULT_PID_FILENAME):
    """
    Return the integer PID for the current bp process, or raise an exception if
{'fraction_non_alphanumeric': 0.09523809523809523, 'fraction_numerical': 0.001885902876001886, 'mean_word_length': 2.6776429809358753, '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': '2339149', 'n_tokens_mistral': 640, 'n_tokens_neox': 613, 'n_words': 187}