<commit_before>import argparse

from concurrent.futures import ThreadPoolExecutor, Future
from functools import partial
from abc import ABCMeta, abstractmethod

from xii import error
from xii.store import HasStore
from xii.entity import Entity


# move me to util if time is ready
def in_parallel(worker_count, objects, executor):
    try:
        pool = ThreadPoolExecutor(worker_count)
        futures = map(partial(pool.submit, executor), objects)

        # handle possible errors
        errors = filter(None, map(Future.exception, futures))

        if errors:
            for err in errors:
                raise err
    finally:
        pool.shutdown(wait=False)


class Command(Entity, HasStore):
    __meta__ = ABCMeta

    name = ["invalidcommand"]
    help = "No help given"

    def __init__(self, args, tpls, store):
        Entity.__init__(self, self.name[0], store, parent=None, templates=tpls)
        self._args = args

    # store() is already defined by Entity

    @abstractmethod
    def run(self):
        pass

    def add_component(self, component):
        self.add_child(component)

    def get_component(self, name):
        return self.get_child(name)

    def each_component(self, action, reverse=False):
        try:
            if self.get("global/parallel", True):
                count = self.get("global/workers", 3)
                for name, group in self._grouped_components():
                    self.say("{}ing {}s..".format(action, name))
                    in_parallel(count, group, lambda o: o.run(action))
            else:<commit_msg>Add reverse option to parallel exectution<commit_after>import argparse

from concurrent.futures import ThreadPoolExecutor, Future
from functools import partial
from abc import ABCMeta, abstractmethod

from xii import error
from xii.store import HasStore
from xii.entity import Entity


# move me to util if time is ready
def in_parallel(worker_count, objects, executor):
    try:
        pool = ThreadPoolExecutor(worker_count)
        futures = map(partial(pool.submit, executor), objects)

        # handle possible errors
        errors = filter(None, map(Future.exception, futures))

        if errors:
            for err in errors:
                raise err
    finally:
        pool.shutdown(wait=False)


class Command(Entity, HasStore):
    __meta__ = ABCMeta

    name = ["invalidcommand"]
    help = "No help given"

    def __init__(self, args, tpls, store):
        Entity.__init__(self, self.name[0], store, parent=None, templates=tpls)
        self._args = args

    # store() is already defined by Entity

    @abstractmethod
    def run(self):
        pass

    def add_component(self, component):
        self.add_child(component)

    def get_component(self, name):
        return self.get_child(name)

    def each_component(self, action, reverse=False):
        try:
            if reverse:
                groups = reversed(self._grouped_components())
            else:
                groups = self._grouped_components()

            if self.get("global/parallel", True):
                count = self.get("global/workers", 3)
                for name, group in groups:
                    self.say("{}ing {}s..".format(action, name))
                    in_parallel(count, group, lambda o: o.run(action))
            else:
{'fraction_non_alphanumeric': 0.09172932330827067, 'fraction_numerical': 0.0012030075187969924, 'mean_word_length': 2.4537902388369677, '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': '7276424', 'n_tokens_mistral': 938, 'n_tokens_neox': 903, 'n_words': 273}