<commit_before>'''
Utilities to help write functional code in Python and enhance existing functions.
- log_debug: add debug-level log statements before and after calling the provided function.
- try_catch: avoid the provided function to raise exception by handling and returning them.
'''

import logging
from threading import current_thread


def log_debug(func, *args, **kwargs):
    ''' Wrap call of provided function with debug log statements. '''
    logging.debug('Starting "%s" in thread %s...', func.__name__, current_thread())
    results = func(*args, **kwargs)
    logging.debug('Successfully finished "%s" in thread %s.', func.__name__, current_thread())
    return results


def try_catch(func, *args, **kwargs):
    '''
    Wrap call of provided function with try/except block and debug log statements.
    If an instance of 'StandardError' (or one of its subclasses) is thrown by 'func',
    it is caught, and the exception object itself is return as a result.
    '''
    try:
        return log_debug(func, *args, **kwargs)
    except StandardError as exc:
        logging.debug('Error in "%s" in thread %s: %s', func.__name__, current_thread(), exc)
        return exc<commit_msg>try_catch now handles Exception again, as StandardError does not exist in Python3.<commit_after>'''
Utilities to help write functional code in Python and enhance existing functions.
- log_debug: add debug-level log statements before and after calling the provided function.
- try_catch: avoid the provided function to raise exception by handling and returning them.
'''

import logging
from threading import current_thread


def log_debug(func, *args, **kwargs):
    ''' Wrap call of provided function with debug log statements. '''
    logging.debug('Starting "%s" in thread %s...', func.__name__, current_thread())
    results = func(*args, **kwargs)
    logging.debug('Successfully finished "%s" in thread %s.', func.__name__, current_thread())
    return results


def try_catch(func, *args, **kwargs):
    '''
    Wrap call of provided function with try/except block and debug log statements.
    If an instance of 'Exception' (or one of its subclasses) is thrown by 'func',
    it is caught, and the exception object itself is return as a result.
    '''
    try:
        return log_debug(func, *args, **kwargs)
    except Exception as exc:
        logging.debug('Error in "%s" in thread %s: %s', func.__name__, current_thread(), exc)
        return exc
{'fraction_non_alphanumeric': 0.10012210012210013, 'fraction_numerical': 0.000407000407000407, 'mean_word_length': 3.886679920477137, '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': '5131600', 'n_tokens_mistral': 641, 'n_tokens_neox': 634, 'n_words': 292}