<commit_before># coding=utf-8
from __future__ import absolute_import

import wrapt
import inspect

from functools import wraps

from ulog._base import LogLevel


class UnknownArgumentException(Exception):
    def __init__(self, argument):
        self._argument = argument

    def __str__(self):
        return 'Unknown argument %s' % self._argument

    def __unicode__(self):
        return self.__str__()


class NoArgumentProvided(Exception):
    def __init__(self, argument, argument_position):
        self._argument = argument
        self._argument_position = argument_position

    def __str__(self):
        return "Caller didn't provide a required positional parameter '%s' at index %d" % (
            self._argument, self._argument_position)


def get_argument(argument, func, args, kwargs):
    if argument in kwargs:
        return kwargs[argument]
    else:
        argspec = inspect.getargspec(func)
        if argument in argspec.args:
            argument_index = argspec.args.index(argument)
            if len(args) > argument_index:
                return args[argument_index]
            if argspec.defaults is not None:
                defaults_index = argument_index - len(argspec.args) + len(argspec.defaults)
                if 0 <= defaults_index < len(argspec.defaults):
                    return argspec.defaults[defaults_index]
            raise NoArgumentProvided(argument=argument, argument_position=argument_index)
        else:
            raise UnknownArgumentException(argument=argument)


class ULog(object):
    DEFAULT_PARAMETER_FORMAT = "\n%s: %s"

    def __init__(self, logger, log_level=LogLevel.Error):
        self._logger = logger
        self._log_level = log_level
        self._parameter_format = self.DEFAULT_PARAMETER_FORMAT

    def log_exception(self,
                      msg='Call: "{callable_name}" raised exception:',
                      log_level=LogLevel.Error,
                      traceback=True):

        @wrapt.decorator
        def decorator(wrapped, instance, args, kwargs):
            if self._should_log(log_level):
                try:
                    result = wrapped(*args, **kwargs)
                except Exception as ex:
                    log_message = msg.format(**{'callable_name': wrapped.__name__, 'exception': ex})
                    self._log(log_level, log_message, traceback)
                    raise

                return result

            else:
                return wrapped(*args, **kwargs)

        return decorator

    def log_args(self,
                 msg='Call: "{callable_name}" with arguments:',
                 arguments=None,
                 log_level=LogLevel.Debug):

        @wrapt.decorator
        def decorator(wrapped, instance, args, kwargs):
            if self._should_log(log_level):
                log_message = msg.format(**{'callable_name': wrapped.__name__})
                if not arguments or len(arguments) == 0:
                    log_message += self._format_all_parameters(func=wrapped, args=args, kwargs=kwargs)
                else:
                    log_message += self._format_selected_params(arguments=arguments, func=wrapped, args=args,
                                                                kwargs=kwargs)
                self._log(log_level, log_message)

                result = wrapped(*args, **kwargs)

                return result

            else:
                return wrapped(*args, **kwargs)

        return decorator

    def log_return(self, msg='Call: "{callable_name}" returned value: "{return_value}"', log_level=LogLevel.Debug):

        @wrapt.decorator
        def decorator(wrapped, instance, args, kwargs):
            if self._should_log(log_level):
                result = wrapped(*args, **kwargs)

                log_message = msg.format(**{'callable_name': wrapped.__name__, 'return_value': result})
                self._log(log_level, log_message)

                return result

            else:
                return wrapped(*args, **kwargs)

        return decorator

    def _log(self, level, msg, traceback=False):<commit_msg>Changed the format of exception log.<commit_after># coding=utf-8
from __future__ import absolute_import

import wrapt
import inspect

from ulog._base import LogLevel


class UnknownArgumentException(Exception):
    def __init__(self, argument):
        self._argument = argument

    def __str__(self):
        return 'Unknown argument %s' % self._argument

    def __unicode__(self):
        return self.__str__()


class NoArgumentProvided(Exception):
    def __init__(self, argument, argument_position):
        self._argument = argument
        self._argument_position = argument_position

    def __str__(self):
        return "Caller didn't provide a required positional parameter '%s' at index %d" % (
            self._argument, self._argument_position)


def get_argument(argument, func, args, kwargs):
    if argument in kwargs:
        return kwargs[argument]
    else:
        argspec = inspect.getargspec(func)
        if argument in argspec.args:
            argument_index = argspec.args.index(argument)
            if len(args) > argument_index:
                return args[argument_index]
            if argspec.defaults is not None:
                defaults_index = argument_index - len(argspec.args) + len(argspec.defaults)
                if 0 <= defaults_index < len(argspec.defaults):
                    return argspec.defaults[defaults_index]
            raise NoArgumentProvided(argument=argument, argument_position=argument_index)
        else:
            raise UnknownArgumentException(argument=argument)


class ULog(object):
    DEFAULT_PARAMETER_FORMAT = "\n%s: %s"

    def __init__(self, logger, log_level=LogLevel.Error):
        self._logger = logger
        self._log_level = log_level
        self._parameter_format = self.DEFAULT_PARAMETER_FORMAT

    def log_exception(self,
                      msg='Call: "{callable_name}" raised exception of type {exception_type}',
                      log_level=LogLevel.Error,
                      traceback=True):

        @wrapt.decorator
        def decorator(wrapped, instance, args, kwargs):
            if self._should_log(log_level):
                try:
                    result = wrapped(*args, **kwargs)
                except Exception as ex:
                    context = {'callable_name': wrapped.__name__,
                               'exception_type': type(ex),
                               'exception': ex}
                    log_message = msg.format(**context)
                    self._log(log_level, log_message, traceback)
                    raise

                return result

            else:
                return wrapped(*args, **kwargs)

        return decorator

    def log_args(self,
                 msg='Call: "{callable_name}" called with arguments',
                 arguments=None,
                 log_level=LogLevel.Debug):

        @wrapt.decorator
        def decorator(wrapped, instance, args, kwargs):
            if self._should_log(log_level):
                context = {
                    'callable_name': wrapped.__name__
                }
                log_message = msg.format(**context)

                if not arguments or len(arguments) == 0:
                    log_message += self._format_all_parameters(func=wrapped, args=args, kwargs=kwargs)
                else:
                    log_message += self._format_selected_params(arguments=arguments, func=wrapped, args=args,
                                                                kwargs=kwargs)
                self._log(log_level, log_message)

                result = wrapped(*args, **kwargs)

                return result

            else:
                return wrapped(*args, **kwargs)

        return decorator

    def log_return(self, msg='Call: "{callable_name}" returned value "{return_value}"', log_level=LogLevel.Debug):

        @wrapt.decorator
        def decorator(wrapped, instance, args, kwargs):
            if self._should_log(log_level):
                result = wrapped(*args, **kwargs)
                context = {
                    'callable_name': wrapped.__name__,
                    'return_value': result
                }
                log_message = msg.format(**context)
                self._log(log_level, log_message)

                return result

            else:
                return wrapped(*args, **kwargs)

        return decorator

    def _log(self, level, msg, traceback=False):
{'fraction_non_alphanumeric': 0.08678892652727485, 'fraction_numerical': 0.0007008527041233501, 'mean_word_length': 1.9211873080859774, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7005165', 'n_tokens_mistral': 2228, 'n_tokens_neox': 2115, 'n_words': 514}