<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-

# Hive Netius System
# Copyright (c) 2008-2017 Hive Solutions Lda.
#
# This file is part of Hive Netius System.
#
# Hive Netius System is free software: you can redistribute it and/or modify
# it under the terms of the Apache License as published by the Apache
# Foundation, either version 2.0 of the License, or (at your option) any
# later version.
#
# Hive Netius System is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# Apache License for more details.
#
# You should have received a copy of the Apache License along with
# Hive Netius System. If not, see <http://www.apache.org/licenses/>.

__author__ = "<NAME> <<EMAIL>>"
""" The author(s) of the module """

__version__ = "1.0.0"
""" The version of the module """

__revision__ = "$LastChangedRevision$"
""" The revision number of the module """

__date__ = "$LastChangedDate$"
""" The last change date of the module """

__copyright__ = "Copyright (c) 2008-2017 Hive Solutions Lda."
""" The copyright for the module """

__license__ = "Apache License, Version 2.0"
""" The license for the module """

import sys

import inspect
import functools

from . import legacy

class Future(object):

    def __init__(self, loop = None):
        self.status = 0
        self._loop = loop
        self._result = None
        self._exception = None
        self.cleanup()

    def __iter__(self):
        if not self.done(): yield self

    def cleanup(self):
        self.done_callbacks = []
        self.partial_callbacks = []
        self.ready_callbacks = []
        self.closed_callbacks = []

    def cancelled(self):
        return self.status == 2

    def running(self):
        return self.status == 0

    def done(self):
        return self.status == 1

    def result(self):
        return self._result

    def exception(self, timeout = None):
        return self._exception

    def partial(self, value):
        self._partial_callbacks(value)

    def add_done_callback(self, function):
        self.done_callbacks.append(function)

    def add_partial_callback(self, function):
        self.partial_callbacks.append(function)

    def add_ready_callback(self, function):
        self.ready_callbacks.append(function)

    def add_closed_callback(self, function):
        self.closed_callbacks.append(function)

    def approve(self, cleanup = True):
        self.set_result(None, cleanup = cleanup)

    def cancel(self, cleanup = True):
        self.set_exception(None, cleanup = cleanup)

    def set_result(self, result, cleanup = True, force = False):
        if not force and not self.running(): return
        self.status = 1
        self._result = result
        self._done_callbacks()
        cleanup and self.cleanup()

    def set_exception(self, exception, cleanup = True, force = False):
        if not force and not self.running(): return
        self.status = 2
        self._exception = exception
        self._done_callbacks()
        cleanup and self.cleanup()

    @property
    def ready(self):
        ready = True
        for callback in self.ready_callbacks: ready &= callback()
        return ready

    @property
    def closed(self):
        closed = False
        for callback in self.closed_callbacks: closed |= callback()
        return closed

    def _done_callbacks(self):
        for callback in self.done_callbacks: callback(self)

    def _partial_callbacks(self, value):
        for callback in self.partial_callbacks: callback(self, value)

    def _wrap(self, future):
        self.status = future.status
        self.done_callbacks = future.done_callbacks
        self.partial_callbacks = future.partial_callbacks
        self.ready_callbacks = future.ready_callbacks
        self.closed_callbacks = future.closed_callbacks
        self._loop = future._loop
        self._result = future._result
        self._exception = future._result<commit_msg>better callback calling<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-

# Hive Netius System
# Copyright (c) 2008-2017 Hive Solutions Lda.
#
# This file is part of Hive Netius System.
#
# Hive Netius System is free software: you can redistribute it and/or modify
# it under the terms of the Apache License as published by the Apache
# Foundation, either version 2.0 of the License, or (at your option) any
# later version.
#
# Hive Netius System is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# Apache License for more details.
#
# You should have received a copy of the Apache License along with
# Hive Netius System. If not, see <http://www.apache.org/licenses/>.

__author__ = "<NAME> <<EMAIL>>"
""" The author(s) of the module """

__version__ = "1.0.0"
""" The version of the module """

__revision__ = "$LastChangedRevision$"
""" The revision number of the module """

__date__ = "$LastChangedDate$"
""" The last change date of the module """

__copyright__ = "Copyright (c) 2008-2017 Hive Solutions Lda."
""" The copyright for the module """

__license__ = "Apache License, Version 2.0"
""" The license for the module """

import sys

import inspect
import functools

from . import legacy

class Future(object):

    def __init__(self, loop = None):
        self.status = 0
        self._loop = loop
        self._result = None
        self._exception = None
        self.cleanup()

    def __iter__(self):
        if not self.done(): yield self

    def cleanup(self):
        self.done_callbacks = []
        self.partial_callbacks = []
        self.ready_callbacks = []
        self.closed_callbacks = []

    def cancelled(self):
        return self.status == 2

    def running(self):
        return self.status == 0

    def done(self):
        return self.status == 1

    def result(self):
        return self._result

    def exception(self, timeout = None):
        return self._exception

    def partial(self, value):
        self._partial_callbacks(value)

    def add_done_callback(self, function):
        self.done_callbacks.append(function)

    def add_partial_callback(self, function):
        self.partial_callbacks.append(function)

    def add_ready_callback(self, function):
        self.ready_callbacks.append(function)

    def add_closed_callback(self, function):
        self.closed_callbacks.append(function)

    def approve(self, cleanup = True):
        self.set_result(None, cleanup = cleanup)

    def cancel(self, cleanup = True):
        self.set_exception(None, cleanup = cleanup)

    def set_result(self, result, cleanup = True, force = False):
        if not force and not self.running(): return
        self.status = 1
        self._result = result
        self._done_callbacks(cleanup = cleanup)

    def set_exception(self, exception, cleanup = True, force = False):
        if not force and not self.running(): return
        self.status = 2
        self._exception = exception
        self._done_callbacks(cleanup = cleanup)

    @property
    def ready(self):
        ready = True
        for callback in self.ready_callbacks: ready &= callback()
        return ready

    @property
    def closed(self):
        closed = False
        for callback in self.closed_callbacks: closed |= callback()
        return closed

    def _done_callbacks(self, cleanup = False, delayed = True):
        if not self.done_callbacks: return
        if delayed and self._loop: return self._loop.delay(
            lambda: self._done_callbacks(
                cleanup = cleanup,
                delayed = False
            ),
            immediately = True
        )
        for callback in self.done_callbacks: callback(self)
        if cleanup: self.cleanup()

    def _partial_callbacks(self, value, delayed = True):
        if not self.partial_callbacks: return
        if delayed and self._loop: return self._loop.delay(
            lambda: self._partial_callbacks(delayed = False),
            immediately = True
        )
        for callback in self.partial_callbacks: callback(self, value)

    def _wrap(self, future):
        self.status = future.status
        self.done_callbacks = future.done_callbacks
        self.partial_callbacks = future.partial_callbacks
        self.ready_callbacks = future.ready_callbacks
        self.closed_callbacks = future.closed_callbacks
        self._loop = future._loop
        self._result = future._result
        self._exception = future._result
{'fraction_non_alphanumeric': 0.09267216351178725, 'fraction_numerical': 0.006967831842991523, 'mean_word_length': 2.759057180270624, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '5297561', 'n_tokens_mistral': 2434, 'n_tokens_neox': 2337, 'n_words': 779}