<commit_before>try:
    from builtins import object
except ImportError:
    # python2
    pass
from functools import partial
from collections import defaultdict, OrderedDict
from six import string_types

import logging
logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler())


def listify(obj):
    if obj is None:
        return []
    else:
        return obj if isinstance(obj, (list, type(None))) else [obj]


class State(object):

    def __init__(self, name, on_enter=None, on_exit=None,
                 ignore_invalid_triggers=False):
        """
        Args:
            name (string): The name of the state
            on_enter (string, list): Optional callable(s) to trigger when a
                state is entered. Can be either a string providing the name of
                a callable, or a list of strings.
            on_exit (string, list): Optional callable(s) to trigger when a
                state is exited. Can be either a string providing the name of a
                callable, or a list of strings.
            ignore_invalid_triggers (Boolean): Optional flag to indicate if
                unhandled/invalid triggers should raise an exception

        """
        self.name = name
        self.ignore_invalid_triggers = ignore_invalid_triggers
        self.on_enter = listify(on_enter) if on_enter else []
        self.on_exit = listify(on_exit) if on_exit else []

    def enter(self, event_data):
        """ Triggered when a state is entered. """
        for oe in self.on_enter:
            event_data.machine.callback(
                getattr(event_data.model, oe), event_data)
        logger.info("Entered state %s", self.name)

    def exit(self, event_data):
        """ Triggered when a state is exited. """
        for oe in self.on_exit:
            event_data.machine.callback(
                getattr(event_data.model, oe), event_data)
        logger.info("Exited state %s", self.name)

    def add_callback(self, trigger, func):
        """ Add a new enter or exit callback.
        Args:
            trigger (string): The type of triggering event. Must be one of
                'enter' or 'exit'.
            func (string): The name of the callback function.
        """
        callback_list = getattr(self, 'on_' + trigger)
        callback_list.append(func)


class Transition(object):

    class Condition(object):

        def __init__(self, func, target=True):
            self.func = func
            self.target = target

        def check(self, event_data):
            """ Check whether the condition passes.
            Args:
                event_data (EventData): An EventData instance to pass to the
                condition (if event sending is enabled) or to extract arguments
                from (if event sending is disabled). Also contains the data
                model attached to the current machine which is used to invoke
                the condition.
            """
            predicate = getattr(event_data.model, self.func)
            if event_data.machine.send_event:
                return predicate(event_data) == self.target
            else:
                return predicate(
                    *event_data.args, **event_data.kwargs) == self.target

    def __init__(self, source, dest, conditions=None, unless=None, before=None,
                 after=None):
        """
        Args:
            source (string): The name of the source State.
            dest (string): The name of the destination State.
            conditions (string, list): Condition(s) that must pass in order for
                the transition to take place. Either a string providing the
                name of a callable, or a list of callables. For the transition
                to occur, ALL callables must return True.
            unless (string, list): Condition(s) that must return False in order
                for the transition to occur. Behaves just like conditions arg
                otherwise.
            before (string or list): callbacks to trigger before the
                transition.
            after (string or list): callbacks to trigger after the transition.
        """
        self.source = source
        self.dest = dest
        self.before = [] if before is None else listify(before)
        self.after = [] if after is None else listify(after)

        self.conditions = []
        if conditions is not None:
            for c in listify(conditions):
                self.conditions.append(self.Condition(c))
        if unless is not None:
            for u in listify(unless):
                self.conditions.append(self.Condition(u, target=False))

    def execute(self, event_data):
        """ Execute the transition.
        Args:
            event: An instance of class EventData.
        """
        logger.info("Initiating transition from state %s to state %s...",
                    self.source, self.dest)
        machine = event_data.machine
        for c in self.conditions:
            if not c.check(event_data):
                logger.info("Transition condition failed: %s() does not " +
                            "return %s. Transition halted.", c.func, c.target)
                return False

        for func in self.before:
            machine.callback(getattr(event_data.model, func), event_data)
            logger.info("Executing callback '%s' before transition.", func)
        machine.get_state(self.source).exit(event_data)
        machine.set_state(self.dest)
        event_data.update()
        machine.get_state(self.dest).enter(event_data)
        for func in self.after:
            machine.callback(getattr(event_data.model, func), event_data)
            logger.info("Executed callback '%s' after transition.", func)
        return True

    def add_callback(self, trigger, func):<commit_msg>Fixes string formatting syntax inside logger calls<commit_after>try:
    from builtins import object
except ImportError:
    # python2
    pass
from functools import partial
from collections import defaultdict, OrderedDict
from six import string_types

import logging
logger = logging.getLogger(__name__)
logger.addHandler(logging.NullHandler())


def listify(obj):
    if obj is None:
        return []
    else:
        return obj if isinstance(obj, (list, type(None))) else [obj]


class State(object):

    def __init__(self, name, on_enter=None, on_exit=None,
                 ignore_invalid_triggers=False):
        """
        Args:
            name (string): The name of the state
            on_enter (string, list): Optional callable(s) to trigger when a
                state is entered. Can be either a string providing the name of
                a callable, or a list of strings.
            on_exit (string, list): Optional callable(s) to trigger when a
                state is exited. Can be either a string providing the name of a
                callable, or a list of strings.
            ignore_invalid_triggers (Boolean): Optional flag to indicate if
                unhandled/invalid triggers should raise an exception

        """
        self.name = name
        self.ignore_invalid_triggers = ignore_invalid_triggers
        self.on_enter = listify(on_enter) if on_enter else []
        self.on_exit = listify(on_exit) if on_exit else []

    def enter(self, event_data):
        """ Triggered when a state is entered. """
        for oe in self.on_enter:
            event_data.machine.callback(
                getattr(event_data.model, oe), event_data)
        logger.info("Entered state %s" % self.name)

    def exit(self, event_data):
        """ Triggered when a state is exited. """
        for oe in self.on_exit:
            event_data.machine.callback(
                getattr(event_data.model, oe), event_data)
        logger.info("Exited state %s" % self.name)

    def add_callback(self, trigger, func):
        """ Add a new enter or exit callback.
        Args:
            trigger (string): The type of triggering event. Must be one of
                'enter' or 'exit'.
            func (string): The name of the callback function.
        """
        callback_list = getattr(self, 'on_' + trigger)
        callback_list.append(func)


class Transition(object):

    class Condition(object):

        def __init__(self, func, target=True):
            self.func = func
            self.target = target

        def check(self, event_data):
            """ Check whether the condition passes.
            Args:
                event_data (EventData): An EventData instance to pass to the
                condition (if event sending is enabled) or to extract arguments
                from (if event sending is disabled). Also contains the data
                model attached to the current machine which is used to invoke
                the condition.
            """
            predicate = getattr(event_data.model, self.func)
            if event_data.machine.send_event:
                return predicate(event_data) == self.target
            else:
                return predicate(
                    *event_data.args, **event_data.kwargs) == self.target

    def __init__(self, source, dest, conditions=None, unless=None, before=None,
                 after=None):
        """
        Args:
            source (string): The name of the source State.
            dest (string): The name of the destination State.
            conditions (string, list): Condition(s) that must pass in order for
                the transition to take place. Either a string providing the
                name of a callable, or a list of callables. For the transition
                to occur, ALL callables must return True.
            unless (string, list): Condition(s) that must return False in order
                for the transition to occur. Behaves just like conditions arg
                otherwise.
            before (string or list): callbacks to trigger before the
                transition.
            after (string or list): callbacks to trigger after the transition.
        """
        self.source = source
        self.dest = dest
        self.before = [] if before is None else listify(before)
        self.after = [] if after is None else listify(after)

        self.conditions = []
        if conditions is not None:
            for c in listify(conditions):
                self.conditions.append(self.Condition(c))
        if unless is not None:
            for u in listify(unless):
                self.conditions.append(self.Condition(u, target=False))

    def execute(self, event_data):
        """ Execute the transition.
        Args:
            event: An instance of class EventData.
        """
        logger.info("Initiating transition from state %s to state %s...",
                    self.source, self.dest)
        machine = event_data.machine
        for c in self.conditions:
            if not c.check(event_data):
                logger.info("Transition condition failed: %s() does not " +
                            "return %s. Transition halted.", c.func, c.target)
                return False

        for func in self.before:
            machine.callback(getattr(event_data.model, func), event_data)
            logger.info("Executing callback '%s' before transition." % func)
        machine.get_state(self.source).exit(event_data)
        machine.set_state(self.dest)
        event_data.update()
        machine.get_state(self.dest).enter(event_data)
        for func in self.after:
            machine.callback(getattr(event_data.model, func), event_data)
            logger.info("Executed callback '%s' after transition." % func)
        return True

    def add_callback(self, trigger, func):
{'fraction_non_alphanumeric': 0.08088047094957768, 'fraction_numerical': 0.00017063390495691494, 'mean_word_length': 1.9819384380564742, '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': '3582657', 'n_tokens_mistral': 3035, 'n_tokens_neox': 2923, 'n_words': 1059}